天天看點

base環境解除安裝python_Python常用指令

base環境解除安裝python_Python常用指令

本文主要總結分享Python使用過程中的一些小技巧,重點關注Python指令,不包括Python代碼相關的使用技巧。主要包括pip包管理、啟動HTTP服務、格式化Json字元串等等。

一、安裝依賴包

Pip指令 功能
pip list 列出已安裝的包
pip freeze > requirements.txt 導出已經安裝的依賴包到requirements.txt
pip install 安裝依賴包
pip install -r requirements.txt 安裝requirements.txt中的所有依賴包
pip uninstall 解除安裝依賴包
pip uninstall -r requirements.txt 解除安裝requirements.txt中的所有依賴包
pip install -U 更新依賴包
pip install -U pip 更新pip本身
pip show -f 顯示包所在的目錄
pip search 搜尋依賴包
pip list -o 查詢可以更新的依賴包
pip download django -d ./ –trusted-host mirrors.cloud.aliyuncs.com 下載下傳依賴包,不安裝
pip download -d ./ -r requirements.txt 下載下傳requirements.txt中的所有依賴包到本地
pip install django -i 

https://mirrors.aliyun.com/pypi/simple

指定鏡像源安裝依賴包

說明:

1、wheel 本質上是一個 zip 包格式,用于 python 子產品的安裝,它的出現是為了替代 Eggs。

pip也可以直接安裝wheel包。如果釋出子產品,推薦使用 wheel 格式。

2、國内pypi鏡像

阿裡:

https://mirrors.aliyun.com/pypi/simple

中國科學技術大學:

http://pypi.mirrors.ustc.edu.cn/simple/

3、指定全局安裝源

在unix和macos,配置檔案為:$HOME/.pip/pip.conf

在windows上,配置檔案為:%HOME%\pip\pip.ini

[global]

timeout = 6000

index-url = 

https://mirrors.aliyun.com/pypi/simple

二、啟動HTTP服務

當需要臨時分享或者傳遞比較大的檔案時,啟動一個HTTP服務,然後把HTTP連結發給對方,對方就可以通過網頁下載下傳檔案,很友善。

1)Python2啟動HTTP服務

python -m SimpleHTTPServer 端口

12
           
[[email protected] pkg]# python -m SimpleHTTPServer 9998Serving HTTP on 0.0.0.0 port 9998 ...
           

2)Python3啟動HTTP服務

python -m http.server 端口

12
           
C:\blog> python -m http.server 9998Serving HTTP on 0.0.0.0 port 9998 (http://0.0.0.0:9998/) ...
           

三、格式化接送字元串

在沒有格式化json字元串的工具的情況下,可以使用python自帶的json.tool子產品對json字元串快速進行格式化,友善簡單。

1)管道方式

12345678910
           
PS C:\test> echo '[ { "a":1, "b":2, "c":3, "d":4, "e":5 } ]' | python -m json.tool[    {        "a": 1,        "b": 2,        "c": 3,        "d": 4,        "e": 5    }]
           

2)檔案方式

12345678910
           
PS C:\test> python -m json.tool test.txt[    {        "a": 1,        "b": 2,        "c": 3,        "d": 4,        "e": 5    }]
           

四、列印sys.path内容

用python -m site列印目前python環境和site-packages相關調試資訊。python -m site作用是顯示sys.path的值内容,也就是python搜尋子產品的目錄,作用類似于linux下的PATH。

123456789101112
           
PS C:\test> python -m sitesys.path = [    'C:\\test',    'C:\\Users\\ying\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',    'C:\\Users\\ying\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',    'C:\\Users\\ying\\AppData\\Local\\Programs\\Python\\Python37\\lib',    'C:\\Users\\ying\\AppData\\Local\\Programs\\Python\\Python37',    'C:\\Users\\ying\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages',]USER_BASE: 'C:\\Users\\ying\\AppData\\Roaming\\Python' (doesn't exist)USER_SITE: 'C:\\Users\\ying\\AppData\\Roaming\\Python\\Python37\\site-packages' (doesn't exist)ENABLE_USER_SITE: True