virtualenv簡介和安裝
virtualenv是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 ... rtualenv-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 ... rtualenv-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后,就可以創建隔離環境的Python項目了,下面以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/p ... y2.py3-none-any.whl (91kB)
100% |████████████████████████████████| 92kB 138kB/s
Collecting idna<2.8,>=2.5 (from requests)
Downloading https://files.pythonhosted.org/p ... y2.py3-none-any.whl (58kB)
100% |████████████████████████████████| 61kB 52kB/s
Collecting urllib3<1.24,>=1.21.1 (from requests)
Downloading https://files.pythonhosted.org/p ... y2.py3-none-any.whl (133kB)
100% |████████████████████████████████| 143kB 29kB/s
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Downloading https://files.pythonhosted.org/p ... y2.py3-none-any.whl (133kB)
100% |████████████████████████████████| 143kB 53kB/s
Collecting certifi>=2017.4.17 (from requests)
Downloading https://files.pythonhosted.org/p ... y2.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: me@kennethreitz.org
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
1
ok,這樣基本的隔離Python開發環境就被創建出來啦~關于virtualenv暫就講到這里,還有些比較方便的功能有待大家共同探索^ ^
參考
?_? A non-magical introduction to Pip and Virtualenv for Python beginners
?_? Requirements Files
備注:Python2.x和3.x的site-packages目錄
Python2.7:/Library/Python/2.7/site-packages
Python3.6:/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
本文版權歸黑馬程序員JavaEE學院所有,歡迎轉載,轉載請注明作者出處。謝謝!
作者:黑馬程序員JavaEE培訓學院
首發:http://java.itheima.com/