在使用 Python 進行開發和部署的時候,常常會碰到Python版本或者依賴包或者對應版本不一樣致使各類意外狀況發生。python
本文將介紹如何經過 pyenv, virtualenv, pip三個工具來管理環境,以保證團隊內部,使用的Python版本以及使用的依賴包版本都高度一至,而且有很高的移植性。git
pyenv, virtualenv, pip 的安裝網上資料不少,這裏再也不贅述
Python 2.7.9 + 或 Python 3.4+ 以上版本都自帶 pip 工具github
~ » lsb_release -a x@1lin24 LSB Version: :core-4.1-amd64:core-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release 7.4.1708 (Core) Release: 7.4.1708 Codename: Core -------------------------------------------------------------------------------------------------------- ~ » pyenv -v x@1lin24 pyenv 1.2.14-5-g68a77df0 -------------------------------------------------------------------------------------------------------- ~ » pyenv versions x@1lin24 system * 3.6.9 (set by /home/x/.pyenv/version) 3.7.5 3.8.0 -------------------------------------------------------------------------------------------------------- ~ » pip --version x@1lin24 pip 18.1 from /home/x/.pyenv/versions/3.6.9/lib/python3.6/site-packages/pip (python 3.6) -------------------------------------------------------------------------------------------------------- ~ » virtualenv --version x@1lin24 16.7.7
在實踐以前,咱們先快速瞭解幾個工具的功能和基本用法vim
pyenv install --list
這個命令會列出全部pyenv收納的全部版本windows
pyenv install 3.6.9
從pyenv install --list
中找到須要的版本號,使用上面的命令安裝便可api
安裝過程須要花幾分鐘的時間服務器
pyenv versions
咱們能夠pyenv versions
列出的版本中,選擇想要的Python版本工具
pyenv global 3.6.9 pyenv local 3.6.9
上面兩個命令都是用於設定當前的Python版本號,不一樣之處在於:測試
virtualenv --no-site-packages .venv
這個命令會在當前目錄下生成一個 .venv 文件夾,用於存放虛擬環境相關的文件。正常狀況下這個目錄是咱們項目的根目錄。ui
須要說明的是,要生成乾淨的虛擬環境必定要加上--no-site-packages
,不然建立的虛擬環境仍然會包含系統環境下的一些依賴包。
.venv是咱們的虛擬環境的名稱,這裏想多說一句,之因此使用在環境面前加了一個點,是在 windows 和 Linux 下以點開頭的文件通常不會直接顯示出來,這樣在視覺會更舒服一些。
source .venv/bin/activate # windows 下使用 .\.venv\Scripts\activate.bat
建立好虛擬環境以後,還要激活才能進入到咱們剛剛建立的虛擬環境
deactivate
當要在其它項目上工做前或者其它不須要當前環境時,須要用上面的命令關閉虛擬環境
pip install package_name
只要把 package_name 替換成要安裝的包名便可
pip freeze
pip freeze > requirements.txt
將上面的內容重定向到一個文件裏面,習慣上我都把這個文件命令爲requirements.txt
pip install -r requirements.txt
這個命令的做用在於,當咱們把項目移交到別的機器時
能夠根據原來的配置安裝須要的依賴包,而不會出現遺漏或者多安裝的狀況。
講了咱們須要用到的幾個工具的基本使用以後,咱們來進行一次實戰
新建一個目錄 demo_python_env,並進入到這個目錄
~ » mkdir demo_python_env x@1lin24 -------------------------------------------------------------------------------------------------------------------------------------------------------- ~ » cd demo_python_env x@1lin24 --------------------------------------------------------------------------------------------------------------------------------------------------------
建立虛擬環境
~/demo_python_env » virtualenv --no-site-packages .venv x@1lin24 Using base prefix '/home/x/.pyenv/versions/3.6.9' New python executable in /home/x/demo_python_env/.venv/bin/python3.6 Also creating executable in /home/x/demo_python_env/.venv/bin/python Installing setuptools, pip, wheel... done. --------------------------------------------------------------------------------------------------------------------------------------------------------
激活虛擬環境
~/demo_python_env » source .venv/bin/activate x@1lin24 (.venv) --------------------------------------------------------------------------------------------------------------------------------------------------------
安裝 requests 依賴包
~/demo_python_env » pip install requests x@1lin24 Collecting requests Using cached https://files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 Using cached https://files.pythonhosted.org/packages/e0/da/55f51ea951e1b7c63a579c09dd7db825bb730ec1fe9c0180fc77bfb31448/urllib3-1.25.6-py2.py3-none-any.whl Collecting certifi>=2017.4.17 Using cached https://files.pythonhosted.org/packages/18/b0/8146a4f8dd402f60744fa380bc73ca47303cccf8b9190fd16a827281eac2/certifi-2019.9.11-py2.py3-none-any.whl Collecting idna<2.9,>=2.5 Using cached https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl Collecting chardet<3.1.0,>=3.0.2 Using cached https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl Installing collected packages: urllib3, certifi, idna, chardet, requests Successfully installed certifi-2019.9.11 chardet-3.0.4 idna-2.8 requests-2.22.0 urllib3-1.25.6 (.venv) --------------------------------------------------------------------------------------------------------------------------------------------------------
保存當前的依賴包列表到 requerements.txt 中
~/demo_python_env » pip freeze > requerements.txt x@1lin24 (.venv) --------------------------------------------------------------------------------------------------------------------------------------------------------
新建一個測試文件
~/demo_python_env » vim test.py x@1lin24 [1] + 31270 suspended vim test.py (.venv) --------------------------------------------------------------------------------------------------------------------------------------------------------
鍵入如下內容,這段代碼功能是向 github 發一個請求,並打印出響應狀態碼
import requests res = requests.get('https://api.github.com/events') print('** status_code = {} **'.format(res.status_code))
測試是否能正常
~/demo_python_env » python test.py x@1lin24 ** status_code = 200 ** (.venv) --------------------------------------------------------------------------------------------------------------------------------------------------------
關閉虛擬環境
~/demo_python_env » deactivate x@1lin24 -------------------------------------------------------------------------------------------------------------------------------------------------------
demo我提交到github上,你們能夠用於作第二步的練習 https://github.com/1lin24/demo_python_env
咱們假設,同事的代碼已經上傳到公司的git服務器,我先將代碼下載到個人機器上,並切換到這個目錄中
~ » git clone https://github.com/1lin24/demo_python_env.git x@1lin24 Cloning into 'demo_python_env'... remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (5/5), done. remote: Total 5 (delta 0), reused 5 (delta 0), pack-reused 0 Unpacking objects: 100% (5/5), done. -------------------------------------------------------------------------------------------------------------------------------------------------------- ~ » cd demo_python_env x@1lin24 --------------------------------------------------------------------------------------------------------------------------------------------------------
查看項目文件
~/demo_python_env(master) » ls -la x@1lin24 total 24 drwxrwxr-x 3 x x 4096 Nov 7 15:44 . drwx------ 11 x x 4096 Nov 7 15:44 .. drwxrwxr-x 8 x x 4096 Nov 7 15:44 .git -rw-rw-r-- 1 x x 1297 Nov 7 15:44 .gitignore -rw-rw-r-- 1 x x 77 Nov 7 15:44 requerements.txt -rw-rw-r-- 1 x x 125 Nov 7 15:44 test.py --------------------------------------------------------------------------------------------------------------------------------------------------------
建立虛擬環境
~/demo_python_env(master) » virtualenv --no-site-packages .venv x@1lin24 \Using base prefix '/home/x/.pyenv/versions/3.6.9' New python executable in /home/x/demo_python_env/.venv/bin/python3.6 Also creating executable in /home/x/demo_python_env/.venv/bin/python Installing setuptools, pip, wheel... done. --------------------------------------------------------------------------------------------------------------------------------------------------------
激活剛剛建立的虛擬環境
~/demo_python_env(master) » source .venv/bin/activate x@1lin24 (.venv) --------------------------------------------------------------------------------------------------------------------------------------------------------
安裝項目須要的依賴包
~/demo_python_env(master) » pip install -r requerements.txt x@1lin24 Collecting certifi==2019.9.11 Using cached https://files.pythonhosted.org/packages/18/b0/8146a4f8dd402f60744fa380bc73ca47303cccf8b9190fd16a827281eac2/certifi-2019.9.11-py2.py3-none-any.whl Collecting chardet==3.0.4 Using cached https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl Collecting idna==2.8 Using cached https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl Collecting requests==2.22.0 Using cached https://files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl Collecting urllib3==1.25.6 Using cached https://files.pythonhosted.org/packages/e0/da/55f51ea951e1b7c63a579c09dd7db825bb730ec1fe9c0180fc77bfb31448/urllib3-1.25.6-py2.py3-none-any.whl Installing collected packages: certifi, chardet, idna, urllib3, requests Successfully installed certifi-2019.9.11 chardet-3.0.4 idna-2.8 requests-2.22.0 urllib3-1.25.6 (.venv) --------------------------------------------------------------------------------------------------------------------------------------------------------
運行測試代碼
~/demo_python_env(master) » python test.py x@1lin24 ** status_code = 200 ** (.venv) --------------------------------------------------------------------------------------------------------------------------------------------------------
關閉虛擬環境
~/demo_python_env(master) » deactivate x@1lin24 --------------------------------------------------------------------------------------------------------------------------------------------------------
這是我本身使用的python環境管理方案,但願可以幫助到須要的人,若是你有更好的方案,請必定告訴我哦
pyenv 官方文檔
virtualenv 官方文檔
pip 官方文檔
歡迎指教,留言交流