搭建Python獨立環境:virtualenv的應用

NO IMAGE

virtualenv是什麼?它是為了方便Python獨立開發應運而生的一款虛擬化項整合管理工具,通俗的說就是在和系統本身的環境不衝突的前提下獨立出一塊隔離的空間,把它留給新專案的依賴環境——就是為了解決不同版本、不同型別的Python依賴衝突而誕生的!


virtualenv簡介和安裝

virtual是Github上的一個專案,按照它的原話就是『Python虛擬環境的構建者』,這是它的專案地址:Virtual Python Environment builder

這裡簡單介紹下它的工作原理:virtualenv把執行Python程式必須的基本環境,包括二進位制Python自身、Python標準庫、pip安裝器,以及至關重要的site-packages目錄,全部拷貝一份到一個完全隔離的目錄下,這個目錄就是你的獨立開環境

在專案首頁最下面有安裝教程的超連結:Installation,你可以簡單敲pip命令進行安裝,這是最簡便的方法

pip install virtualenv

另外還用第二種方法:把專案整個當下來,然後手動安裝

# Use virtualenv globally
$ curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-X.X.tar.gz
$ tar xvfz virtualenv-X.X.tar.gz
$ cd virtualenv-X.X
$ [sudo] python setup.py install
# Use virtualenv locally
$ curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-X.X.tar.gz
$ tar xvfz virtualenv-X.X.tar.gz
$ cd virtualenv-X.X
$ python virtualenv.py myVE

事實上你需要全域性安裝的Python套件只有兩個,即pip和virtualenv,如果 把virtualenv包括的pip算進去,那其實只要安裝一個virtualenv已足矣


使用virtualenv建立獨立專案

在安裝好virtualenv後,就可以建立隔離環境的Pythonl專案了,下面以demo這個專案為例

1. 建立專案根目錄,命名為demo並進入該目錄

➜  ~ mkdir demo
➜  ~ cd demo
➜  demo 

2. 使用virtualenv命令,建議加上–no-site-packages選項

➜  demo virtualenv --no-site-packages venv
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/macbookpro/demo/venv/bin/python3.6
Also creating executable in /Users/macbookpro/demo/venv/bin/python
Installing setuptools, pip, wheel...done.

3. 如果想要手動指定Python版本,請使用–python=$PYTHON_VERSION選項

➜  demo virtualenv --python=python2.7 --no-site-packages venv1
Running virtualenv with interpreter /usr/local/bin/python2.7
New python executable in /Users/macbookpro/demo/venv1/bin/python2.7
Also creating executable in /Users/macbookpro/demo/venv1/bin/python
Installing setuptools, pip, wheel...done.

4. 你甚至可以指定系統沒有安裝的Python版本,比如我這裡的Python3.7,但是絕對不要想當然的填寫Pyhton版本號否則會報錯“doesn’t exist”

➜  demo virtualenv --python=python3.7 --no-site-packages venv2
Running virtualenv with interpreter /usr/local/bin/python3.7
Using base prefix '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7'
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/virtualenv.py:1041: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
New python executable in /Users/macbookpro/demo/venv2/bin/python3.7
Also creating executable in /Users/macbookpro/demo/venv2/bin/python
Installing setuptools, pip, wheel...done.
➜  demo virtualenv --python=python4.0 --no-site-packages venv3
The path python4.0 (from --python=python4.0) does not exist

5. 在新建的venv目錄下,重點關注bin目錄,它包括了pip管理器和python對應版本的直譯器Pythonx.x.

➜  venv ls
bin                include            lib                pip-selfcheck.json
➜  venv cd bin
➜  bin ls
activate         easy_install     pip3.6           python3.6
activate.csh     easy_install-3.6 python           wheel
activate.fish    pip              python-config
activate_this.py pip3             python3
➜  demo venv/bin/pip install requests
Collecting requests
Downloading https://files.pythonhosted.org/packages/65/47/7e02164a2a3db50ed6d8a6ab1d6d60b69c4c3fdf57a284257925dfc12bda/requests-2.19.1-py2.py3-none-any.whl (91kB)
100% |████████████████████████████████| 92kB 138kB/s 
Collecting idna<2.8,>=2.5 (from requests)
Downloading https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl (58kB)
100% |████████████████████████████████| 61kB 52kB/s 
Collecting urllib3<1.24,>=1.21.1 (from requests)
Downloading https://files.pythonhosted.org/packages/bd/c9/6fdd990019071a4a32a5e7cb78a1d92c53851ef4f56f62a3486e6a7d8ffb/urllib3-1.23-py2.py3-none-any.whl (133kB)
100% |████████████████████████████████| 143kB 29kB/s 
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
100% |████████████████████████████████| 143kB 53kB/s 
Collecting certifi>=2017.4.17 (from requests)
Downloading https://files.pythonhosted.org/packages/7c/e6/92ad559b7192d846975fc916b65f667c7b8c3a32bea7372340bfe9a15fa5/certifi-2018.4.16-py2.py3-none-any.whl (150kB)
100% |████████████████████████████████| 153kB 41kB/s 
Installing collected packages: idna, urllib3, chardet, certifi, requests
Successfully installed certifi-2018.4.16 chardet-3.0.4 idna-2.7 requests-2.19.1 urllib3-1.23

6. 用virtual內建pip安裝的包被放在lib目錄下

➜  demo venv/bin/pip show requests
Name: requests
Version: 2.19.1
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: [email protected]
License: Apache 2.0
Location: /Users/macbookpro/demo/venv/lib/python3.6/site-packages
Requires: idna, certifi, chardet, urllib3
Required-by:

7. 為了減少打字,可啟用activate檔案,這樣原來的venv/bin/pip就要寫pip,代替了系統原來的pip命令,不用時啟用deactivate檔案即可

➜  demo source venv/bin/activate
(venv) ➜  demo pip list
Package    Version  
---------- ---------
certifi    2018.4.16
chardet    3.0.4    
idna       2.7      
pip        10.0.1   
requests   2.19.1   
setuptools 40.0.0   
urllib3    1.23     
wheel      0.31.1   
(venv) ➜  demo pip uninstall requests
Uninstalling requests-2.19.1:
Would remove:
/Users/macbookpro/demo/venv/lib/python3.6/site-packages/requests-2.19.1.dist-info/*
/Users/macbookpro/demo/venv/lib/python3.6/site-packages/requests/*
Proceed (y/n)? y
Successfully uninstalled requests-2.19.1
(venv) ➜  demo pip list
Package    Version  
---------- ---------
certifi    2018.4.16
chardet    3.0.4    
idna       2.7      
pip        10.0.1   
setuptools 40.0.0   
urllib3    1.23     
wheel      0.31.1   
(venv) ➜  demo 
(venv) ➜  demo deactivate 
➜  demo 

8. pip和virtualenv合作使用起來很爽,pip使用-r引數指定requirement檔案

pip install -r requirements.txt

ok,這樣基本的隔離Python開發環境就被建立出來啦~關於virtualenv暫就講到這裡,還有些比較方便的功能有待大家共同探索^ ^

參考
ಥ_ಥ A non-magical introduction to Pip and Virtualenv for Python beginners
ಥ_ಥ Requirements Files