本文以Windows环境为例介绍Python虚拟环境的安装,Linux与之冲突的地方会标明。
virtualenv
virtualenvwrapper
- Python豆瓣镜像源的使用
一、virtualenv
virtualenv
优点
- 使不同应用开发环境独立
- 环境升级不影响其他应用,也不会影响全局的python环境
- 它可以防止系统中出现包管理混乱和版本的冲突
测试pip是否正常
C:Usersxxx>pip
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
config Manage local and global configuration.
search Search PyPI for packages.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
help Show help for commands.
General Options:
-h, --help Show help.
--isolated Run pip in an isolated mode, ignoring environment variables and user configuration.
-v, --verbose Give more output. Option is additive, and can be used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be used up to 3 times (corresponding to
WARNING, ERROR, and CRITICAL logging levels).
--log <path> Path to a verbose appending log.
--proxy <proxy> Specify a proxy in the form [user:passwd@]proxy.server:port.
--retries <retries> Maximum number of retries each connection should attempt (default 5 times).
--timeout <sec> Set the socket timeout (default 15 seconds).
--exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup,
(a)bort).
--trusted-host <hostname> Mark this host as trusted, even though it does not have valid or any HTTPS.
--cert <path> Path to alternate CA bundle.
--client-cert <path> Path to SSL client certificate, a single file containing the private key and the
certificate in PEM format.
--cache-dir <dir> Store the cache data in <dir>.
--no-cache-dir Disable the cache.
--disable-pip-version-check
Don't periodically check PyPI to determine whether a new version of pip is available for
download. Implied with --no-index.
--no-color Suppress colored output
C:Usersxxx>
安装virtualenv
C:Usersxxx>pip install virtualenv
...
C:Usersxxx>
注:Linux环境下,sudo apt-get install python-virtualenv
root@onefine-virtual-machine:~# sudo apt-get install python-virtualenv
...
root@onefine-virtual-machine:~#
创建virtualenv
默认情况安装在当前命令的运行目录下边,
virtualenv 名称
例如,在指定目录下创建一个名为ve_test_0
的虚拟环境:
D:>mkdir test
D:>cd test
D: est>virtualenv ve_test_0
Using base prefix 'c:\users\xxx\appdata\local\programs\python\python37'
New python executable in D: estve_test_0Scriptspython.exe
Installing setuptools, pip, wheel...
done.
D: est>
查看一下:
D: est>dir ve_test_0
D: estve_test_0 的目录
2019/01/18 21:24 <DIR> .
2019/01/18 21:24 <DIR> ..
2018/12/24 12:20 <DIR> Include
2019/01/18 21:24 <DIR> Lib
2019/01/18 21:24 <DIR> Scripts
2019/01/18 21:24 <DIR> tcl
0 个文件 0 字节
6 个目录 61,376,417,792 可用字节
D: est>
进入virtualenv
运行ve_test_0Scripts>dir
下面的activate.bat
文件
D: est>cd ve_test_0Scripts
D: estve_test_0Scripts>dir
驱动器 D 中的卷没有标签。
卷的序列号是 D644-E329
D: estve_test_0Scripts 的目录
2019/01/18 21:24 <DIR> .
2019/01/18 21:24 <DIR> ..
2019/01/18 21:24 2,186 activate
2019/01/18 21:24 758 activate.bat
2019/01/18 21:24 1,544 activate.ps1
2019/01/18 21:24 997 activate.xsh
2019/01/18 21:24 1,512 activate_this.py
2019/01/18 21:24 512 deactivate.bat
2019/01/18 21:24 102,775 easy_install-3.7.exe
2019/01/18 21:24 102,775 easy_install.exe
2019/01/18 21:24 102,757 pip.exe
2019/01/18 21:24 102,757 pip3.7.exe
2019/01/18 21:24 102,757 pip3.exe
2019/01/18 21:24 99,992 python.exe
2019/01/18 21:24 59,032 python3.dll
2019/01/18 21:24 3,771,032 python37.dll
2019/01/18 21:24 7,843,328 python37_d.dll
2019/01/18 21:24 69,632 python3_d.dll
2019/01/18 21:24 98,456 pythonw.exe
2019/01/18 21:24 135,680 python_d.exe
2019/01/18 21:24 102,753 wheel.exe
19 个文件 12,701,235 字节
2 个目录 61,376,417,792 可用字节
D: estve_test_0Scripts>activate.bat
(ve_test_0) D: estve_test_0Scripts>
注:Linux环境下的启动方式与其不同,是通过source activate
启动的(详下小试牛刀)。
查看当前虚拟环境中安装的库:pip list
(ve_test_0) D: estve_test_0Scripts>pip list
Package Version
---------- -------
pip 18.1
setuptools 40.6.3
wheel 0.32.3
(ve_test_0) D: estve_test_0Scripts>
退出虚拟环境:
(ve_test_0) D: estve_test_0Scripts>deactivate.bat
D: estve_test_0Scripts>
注:Linux环境下,退出是deactivate
。
小试牛刀
windows环境下使用Python2.7来新建虚拟环境完整过程
D:>cd test
D: est>virtualenv -p C:Python27python.exe ve_test_1
Running virtualenv with interpreter C:Python27python.exe
New python executable in D: estve_test_1Scriptspython.exe
Installing setuptools, pip, wheel...
done.
D: est>cd ve_test_1
D: estve_test_1>dir
驱动器 D 中的卷没有标签。
卷的序列号是 D644-E329
D: estve_test_1 的目录
2019/01/27 15:34 <DIR> .
2019/01/27 15:34 <DIR> ..
2019/01/27 14:55 <DIR> Include
2019/01/27 15:34 <DIR> Lib
2019/01/27 15:36 <DIR> Scripts
2019/01/27 15:34 <DIR> tcl
0 个文件 0 字节
6 个目录 27,907,104,768 可用字节
D: estve_test_1>cd Scripts
D: estve_test_1Scripts>activate.bat
(ve_test_1) D: estve_test_1Scripts>python
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(ve_test_1) D: estve_test_1Scripts>deactivate.bat
D: estve_test_1Scripts>
Linux环境下使用Python3新建虚拟环境完整过程
root@onefine-virtual-machine:~# cd
root@onefine-virtual-machine:~# pwd
/root
root@onefine-virtual-machine:~# mkdir test
root@onefine-virtual-machine:~# cd test
root@onefine-virtual-machine:~/test# virtualenv -p /usr/bin/python3 py3_test
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /root/test/py3_test/bin/python3
Also creating executable in /root/test/py3_test/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
root@onefine-virtual-machine:~/test# pwd
/root/test
root@onefine-virtual-machine:~/test# ls -al
总用量 12
drwxr-xr-x 3 root root 4096 1月 27 17:05 .
drwx------ 7 root root 4096 1月 27 17:05 ..
drwxr-xr-x 6 root root 4096 1月 27 17:05 py3_test
root@onefine-virtual-machine:~/test# cd py3_test/
root@onefine-virtual-machine:~/test/py3_test# python
Python 2.7.15+ (default, Oct 2 2018, 22:12:08)
[GCC 8.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
root@onefine-virtual-machine:~/test/py3_test# cd ..
root@onefine-virtual-machine:~/test# cd py3_test/bin/
root@onefine-virtual-machine:~/test/py3_test/bin# source activate
(py3_test) root@onefine-virtual-machine:~/test/py3_test/bin# python
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(py3_test) root@onefine-virtual-machine:~/test/py3_test/bin# deactivate
root@onefine-virtual-machine:~/test/py3_test/bin#
二、virtualenvwrapper
virtualenv创建的虚拟环境运行时需要知道安装时的目录,比较麻烦,我们可以使用另一个开发库——virtualenvwrapper
virtualenvwrapper
是一个基于virtualenv
之上的工具,它将所欲的虚拟环境统一管理。
安装
D: estve_test_0Scripts>pip install virtualenvwrapper-win
Collecting virtualenvwrapper-win
Downloading https://files.pythonhosted.org/packages/f5/23/4cba98733b9122219ce67177d745e4984b524b867cf3728eaa807ea21919/virtualenvwrapper-win-1.2.5.tar.gz
Collecting virtualenv (from virtualenvwrapper-win)
Using cached https://files.pythonhosted.org/packages/6a/d1/e0d142ce7b8a5c76adbfad01d853bca84c7c0240e35577498e20bc2ade7d/virtualenv-16.2.0-py2.py3-none-any.whl
Requirement already satisfied: setuptools>=18.0.0 in d: estve_test_0libsite-packages (from virtualenv->virtualenvwrapper-win) (40.6.3)
Building wheels for collected packages: virtualenvwrapper-win
Running setup.py bdist_wheel for virtualenvwrapper-win ... done
Stored in directory: C:UsersxxxAppDataLocalpipCachewheels47d1ae85075521c1fbe2edb6784af24ccc79af10bbb205cc711a689c
Successfully built virtualenvwrapper-win
Installing collected packages: virtualenv, virtualenvwrapper-win
Successfully installed virtualenv-16.2.0 virtualenvwrapper-win-1.2.5
D: estve_test_0Scripts>
注意:如果是在Linux环境下,直接pip install virtualenvwrapper
即可。
新建虚拟环境
mkvirtualenv 名称
virtualenvwrapper
会将所有新建的virtualenv
放在同一个目录下边:
D: estve_test_0Scripts>mkvirtualenv ve_test_1
C:UsersxxxEnvs is not a directory, creating
Using base prefix 'c:\users\xxx\appdata\local\programs\python\python37'
New python executable in C:UsersxxxEnvsve_test_1Scriptspython.exe
Installing setuptools, pip, wheel...
done.
(ve_test_1) D: estve_test_0Scripts>
可以看到,新建的virtualenv
被放在C:UsersxxxEnvs
下,并且安装完成之后就自动进入了新建的ve_test_1
虚拟环境下。
改变默认安装位置:
Windows环境下虚拟环境的默认安装位置是C:UsersxxxEnvs
,如果要想修改——比如想安装到E:Evns
目录下,只需新建环境变量WORKON_HOME
,变量值填E:Evns
,保存之后重启cmd。之后所有新建的虚拟环境都会在E:Evns
下面,将原来C:UsersxxxEnvs
下面的虚拟环境移动到这里,就可以正常使用。
查看一下安装目录:
D: estve_test_0Scripts>dir cd c:UsersxxxEnvsve_test_1Scripts
驱动器 D 中的卷没有标签。
卷的序列号是 D644-E329
D: estve_test_0Scripts 的目录
c:UsersxxxEnvsve_test_1Scripts 的目录
2019/01/18 21:48 <DIR> .
2019/01/18 21:48 <DIR> ..
2019/01/18 21:48 2,214 activate
2019/01/18 21:48 964 activate.bat
2019/01/18 21:48 1,544 activate.ps1
2019/01/18 21:48 1,011 activate.xsh
2019/01/18 21:48 1,512 activate_this.py
2019/01/18 21:48 603 deactivate.bat
2019/01/18 21:48 102,789 easy_install-3.7.exe
2019/01/18 21:48 102,789 easy_install.exe
2019/01/18 21:48 102,771 pip.exe
2019/01/18 21:48 102,771 pip3.7.exe
2019/01/18 21:48 102,771 pip3.exe
2019/01/18 21:48 99,992 python.exe
2019/01/18 21:48 59,032 python3.dll
2019/01/18 21:48 3,771,032 python37.dll
2019/01/18 21:48 7,843,328 python37_d.dll
2019/01/18 21:48 69,632 python3_d.dll
2019/01/18 21:48 98,456 pythonw.exe
2019/01/18 21:48 135,680 python_d.exe
2019/01/18 21:48 102,767 wheel.exe
19 个文件 12,701,658 字节
2 个目录 68,484,141,056 可用字节
D: estve_test_0Scripts>
D: estve_test_0Scripts>c:
C:UsersxxxEnvs>UsersxxxEnvsve_test_1Scriptsactivate.bat
(ve_test_1) C:UsersxxxEnvs>deactivate.bat
C:UsersxxxEnvs>dir UsersxxxEnvs
驱动器 C 中的卷是 OS
卷的序列号是 BE92-F6A8
C:UsersxxxEnvs 的目录
2019/01/18 21:48 <DIR> .
2019/01/18 21:48 <DIR> ..
2019/01/18 21:48 <DIR> ve_test_1
0 个文件 0 字节
3 个目录 68,478,033,920 可用字节
C:UsersxxxEnvs>
查看当前所有的虚拟环境
C:Usersxxx>workon
Pass a name to activate one of the following virtualenvs:
==============================================================================
ve_test_1
C:Usersxxx>
可见,这里列出的只是通过virtualenvwrapper
安装的。
注:如果提示workon命令不存在,重新打开命令行即可使用。
进入某一个已经安装好的虚拟环境:
C:Usersxxx>workon ve_test_1
(ve_test_1) C:Usersxxx>
(ve_test_1) C:Usersxxx>deactivate
C:Usersxxx>
查看当前虚拟环境中安装的开发包
C:Usersxxx>workon ve_test_1
(ve_test_1) C:Usersxxx>pip list
Package Version
---------- -------
pip 18.1
setuptools 40.6.3
wheel 0.32.3
(ve_test_1) C:Usersxxx>
在虚拟环境中安装开发包
(ve_test_1) C:Usersxxx>pip install requests
Collecting requests
Using cached https://files.pythonhosted.org/packages/7d/e3/20f3d364d6c8e5d2353c72a67778eb189176f08e873c9900e10c0287b84b/requests-2.21.0-py2.py3-none-any.whl
Collecting urllib3<1.25,>=1.21.1 (from requests)
Using cached https://files.pythonhosted.org/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Using cached https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests)
Using cached https://files.pythonhosted.org/packages/9f/e0/accfc1b56b57e9750eba272e24c4dddeac86852c2bebd1236674d7887e8a/certifi-2018.11.29-py2.py3-none-any.whl
Collecting idna<2.9,>=2.5 (from requests)
Using cached https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl
Installing collected packages: urllib3, chardet, certifi, idna, requests
Successfully installed certifi-2018.11.29 chardet-3.0.4 idna-2.8 requests-2.21.0 urllib3-1.24.1
(ve_test_1) C:Usersxxx>
查看一下:
(ve_test_1) C:Usersxxx>pip list
Package Version
---------- ----------
certifi 2018.11.29
chardet 3.0.4
idna 2.8
pip 18.1
requests 2.21.0
setuptools 40.6.3
urllib3 1.24.1
wheel 0.32.3
(ve_test_1) C:Usersxxx>
卸载虚拟环境中已经安装的包
(ve_test_1) C:Usersxxx>pip uninstall requests
Uninstalling requests-2.21.0:
Would remove:
c:usersxxxenvsve_test_1libsite-packages
equests-2.21.0.dist-info*
c:usersxxxenvsve_test_1libsite-packages
equests*
Proceed (y/n)? y
Successfully uninstalled requests-2.21.0
(ve_test_1) C:Usersxxx>
再次查看:
(ve_test_1) C:Usersxxx>pip list
Package Version
---------- ----------
certifi 2018.11.29
chardet 3.0.4
idna 2.8
pip 18.1
setuptools 40.6.3
urllib3 1.24.1
wheel 0.32.3
(ve_test_1) C:Usersxxx>deactivate
C:Usersxxx>
删除虚拟环境
命令:rmvirtualenv venvName
,不再演示。
小试牛刀
Python2.7虚拟环境下Scrapy的安装——Windows
C:Usersxxx>mkvirtualenv --python=C:Python27python.exe py27_test
Running virtualenv with interpreter C:Python27python.exe
New python executable in C:UsersxxxEnvspy27_testScriptspython.exe
Installing setuptools, pip, wheel...
done.
(py27_test) C:Usersxxx>python
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(py27_test) C:Usersxxx>pip install -i https://pypi.douban.com/simple scrapy
...
e_CAs
copying src wistedinternet estfake_CAs hing2.pem -> buildlib.win-amd64-2.7 wistedinternet estfake_CAs
copying src wistedmail est
fc822.message -> buildlib.win-amd64-2.7 wistedmail est
copying src wistedpython est\_deprecatetests.py.3only -> buildlib.win-amd64-2.7 wistedpython est
copying src wistedwordsiminstancemessenger.glade -> buildlib.win-amd64-2.7 wistedwordsim
copying src wistedwordsxishxpathparser.g -> buildlib.win-amd64-2.7 wistedwordsxish
running build_ext
building 'twisted.test.raiser' extension
error: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27
----------------------------------------
Command "c:usersonefineenvspy27_testscriptspython.exe -u -c "import setuptools, tokenize;__file__='c:\users\onefine\appdata\local\temp\pip-install-mbshz0\Twisted\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('
', '
');f.close();exec(compile(code, __file__, 'exec'))" install --record c:usersonefineappdatalocal emppip-record-byrhvcinstall-record.txt --single-version-externally-managed --compile --install-headers c:usersonefineenvspy27_testincludesitepython2.7Twisted" failed with error code 1 in c:usersonefineappdatalocal emppip-install-mbshz0Twisted
(py27_test) C:UsersONEFINE>
报错原因:Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27
,对应网站下载安装之后:
(py27_test) C:Usersxxx>pip install -i https://pypi.douban.com/simple scrapy
...
Building wheels for collected packages: Twisted
Building wheel for Twisted (setup.py) ... done
Stored in directory: C:UsersxxxAppDataLocalpipCachewheels d553bad90bba345bc7b6d7f4b4a1683aae2d291fa541b6c189e6c7b
Successfully built Twisted
Installing collected packages: Twisted, enum34, asn1crypto, ipaddress, pycparser, cffi, cryptography, pyOpenSSL, queuelib, cssselect, functools32, w3lib, parsel, pyasn1, pyasn1-modules, service-identity, scrapy
Successfully installed Twisted-18.9.0 asn1crypto-0.24.0 cffi-1.11.5 cryptography-2.5 cssselect-1.0.3 enum34-1.1.6 functools32-3.2.3.post2 ipaddress-1.0.22 parsel-1.5.1 pyOpenSSL-19.0.0 pyasn1-0.4.5 pyasn1-modules-0.2.4 pycparser-2.19 queuelib-1.5.0 scrapy-1.5.2 service-identity-18.1.0 w3lib-1.20.0
(py27_test) C:Usersxxx>pip list
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
Package Version
---------------- -----------
asn1crypto 0.24.0
attrs 18.2.0
Automat 0.7.0
cffi 1.11.5
constantly 15.1.0
cryptography 2.5
cssselect 1.0.3
enum34 1.1.6
functools32 3.2.3.post2
hyperlink 18.0.0
idna 2.8
incremental 17.5.0
ipaddress 1.0.22
lxml 4.3.0
parsel 1.5.1
pip 19.0.1
pyasn1 0.4.5
pyasn1-modules 0.2.4
pycparser 2.19
PyDispatcher 2.0.5
PyHamcrest 1.9.0
pyOpenSSL 19.0.0
queuelib 1.5.0
Scrapy 1.5.2
service-identity 18.1.0
setuptools 40.6.3
six 1.12.0
Twisted 18.9.0
w3lib 1.20.0
wheel 0.32.3
zope.interface 4.6.0
(py27_test) C:UsersONEFINE>deactivate
C:UsersONEFINE>
出错解决办法:
Python扩展包的非官方Windows二进制文件:https://www.lfd.uci.edu/~gohlke/pythonlibs/
Python包索引(PyPI)Python语言的软件存储库:https://pypi.org/
下载好对应的whl格式的包之安装:pip install + whl格式的安装包
新建Python3虚拟环境——Linux
root@onefine-virtual-machine:~/test/py3_test/bin# pip3 install virtualenvwrapper
Collecting virtualenvwrapper
Downloading https://files.pythonhosted.org/packages/2b/8c/3192e10913ad945c0f0fcb17e9b2679434a28ad58ee31ce0104cba3b1154/virtualenvwrapper-4.8.2-py2.py3-none-any.whl
Collecting stevedore (from virtualenvwrapper)
Downloading https://files.pythonhosted.org/packages/35/fa/8683fab2a6e15ecfe107996e56fab91e52fe3ec0b40ca9440a0e1ffe6892/stevedore-1.30.0-py2.py3-none-any.whl (42kB)
100% |████████████████████████████████| 51kB 40kB/s
Requirement already satisfied: virtualenv in /usr/lib/python3/dist-packages (from virtualenvwrapper)
Collecting virtualenv-clone (from virtualenvwrapper)
Downloading https://files.pythonhosted.org/packages/e3/d9/d9c56deb483c4d3289a00b12046e41428be64e8236fa210111a1f57cc42d/virtualenv_clone-0.5.1-py2.py3-none-any.whl
Collecting pbr!=2.1.0,>=2.0.0 (from stevedore->virtualenvwrapper)
Downloading https://files.pythonhosted.org/packages/f3/04/fddc1c2dd75b256eda4d360024692231a2c19a0c61ad7f4a162407c1ab58/pbr-5.1.1-py2.py3-none-any.whl (106kB)
100% |████████████████████████████████| 112kB 20kB/s
Requirement already satisfied: six>=1.10.0 in /usr/lib/python3/dist-packages (from stevedore->virtualenvwrapper)
Installing collected packages: pbr, stevedore, virtualenv-clone, virtualenvwrapper
Successfully installed pbr-5.1.1 stevedore-1.30.0 virtualenv-clone-0.5.1 virtualenvwrapper-4.8.2
root@onefine-virtual-machine:~/test/py3_test/bin#
配置Linux环境下的通过virtualenvwrapper,使用find
命令从根目录通过name
查找virtualenvwrapper
root@onefine-virtual-machine:~# sudo find / -name virtualenvwrapper.sh
find: ‘/run/user/1000/gvfs’: 权限不够
/usr/local/bin/virtualenvwrapper.sh
root@onefine-virtual-machine:~#
root@onefine-virtual-machine:~# whereis python
python: /usr/bin/python3.6m-config /usr/bin/python3.6m /usr/bin/python3.6 /usr/bin/python3.6-config /usr/bin/python2.7 /usr/bin/python /usr/lib/python3.7 /usr/lib/python3.6 /usr/lib/python2.7 /etc/python3.6 /etc/python2.7 /etc/python /usr/local/lib/python3.6 /usr/local/lib/python2.7 /usr/include/python3.6m /usr/include/python3.6 /usr/include/python3.7m /usr/share/python /usr/share/man/man1/python.1.gz
root@onefine-virtual-machine:~#
配置~/.bashrc
,底部添加配置
-
VIRTUALENVWRAPPER_PYTHON
:python全局路径,可通过whereis python
查询到export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.6
-
WORKON_HOME
:新建虚拟环境在$HOME/.virtualenvs
——用户主目录下的.virtualenvs
文件夹下面。export WORKON_HOME=$HOME/.virtualenvs
-
将上面查到的
virtualenvwrapper
的shell
文件路径/usr/local/bin/virtualenvwrapper.sh
加到source
后面source /usr/local/bin/virtualenvwrapper.sh
如下:
root@onefine-virtual-machine:/# cd ~
root@onefine-virtual-machine:~# ls -al
总用量 56
drwx------ 7 root root 4096 1月 27 17:05 .
drwxr-xr-x 24 root root 4096 12月 26 01:35 ..
-rw------- 1 root root 1327 1月 27 17:34 .bash_history
-rw-r--r-- 1 root root 3106 8月 7 06:35 .bashrc
drwx------ 3 root root 4096 1月 27 11:52 .cache
drwx------ 3 root root 4096 1月 25 17:36 .gnupg
drwxr-xr-x 5 root root 4096 1月 25 17:39 .ipython
-rw------- 1 root root 82 1月 27 10:44 .mysql_history
-rw-r--r-- 1 root root 148 8月 7 06:35 .profile
-rw------- 1 root root 53 1月 27 11:21 .python_history
drwxr-xr-x 3 root root 4096 1月 14 21:11 snap
drwxr-xr-x 3 root root 4096 1月 27 17:05 test
-rw------- 1 root root 4859 1月 27 10:21 .viminfo
root@onefine-virtual-machine:~# vim .bashrc
...
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.6
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
:wq
root@onefine-virtual-machine:~# cat ~/.bashrc
...
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.6
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
root@onefine-virtual-machine:~#
# 可能不需要新建这个文件夹
root@onefine-virtual-machine:~# mkdir .virtualenvs
# 不要忘记source
root@onefine-virtual-machine:~# source .bashrc
virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /root/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/get_env_details
root@onefine-virtual-machine:~# ls -al .virtualenvs
总用量 56
drwxr-xr-x 2 root root 4096 1月 27 19:05 .
drwx------ 9 root root 4096 1月 27 19:05 ..
-rwxr-xr-x 1 root root 135 1月 27 19:05 get_env_details
-rw-r--r-- 1 root root 96 1月 27 19:05 initialize
-rw-r--r-- 1 root root 73 1月 27 19:05 postactivate
-rw-r--r-- 1 root root 75 1月 27 19:05 postdeactivate
-rwxr-xr-x 1 root root 66 1月 27 19:05 postmkproject
-rw-r--r-- 1 root root 73 1月 27 19:05 postmkvirtualenv
-rwxr-xr-x 1 root root 110 1月 27 19:05 postrmvirtualenv
-rwxr-xr-x 1 root root 99 1月 27 19:05 preactivate
-rw-r--r-- 1 root root 76 1月 27 19:05 predeactivate
-rwxr-xr-x 1 root root 91 1月 27 19:05 premkproject
-rwxr-xr-x 1 root root 130 1月 27 19:05 premkvirtualenv
-rwxr-xr-x 1 root root 111 1月 27 19:05 prermvirtualenv
root@onefine-virtual-machine:~#
准备工作完成,开始新建Python3虚拟环境:
root@onefine-virtual-machine:~# mkvirtualenv py2_test
Running virtualenv with interpreter /usr/bin/python2
New python executable in /root/.virtualenvs/py2_test/bin/python2
Also creating executable in /root/.virtualenvs/py2_test/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
virtualenvwrapper.user_scripts creating /root/.virtualenvs/py2_test/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/py2_test/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/py2_test/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/py2_test/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/py2_test/bin/get_env_details
(py2_test) root@onefine-virtual-machine:~# python
Python 2.7.15+ (default, Oct 2 2018, 22:12:08)
[GCC 8.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(py2_test) root@onefine-virtual-machine:~# cd /
(py2_test) root@onefine-virtual-machine:/# cd ~/.virtualenvs/
(py2_test) root@onefine-virtual-machine:~/.virtualenvs# ls -al
总用量 60
drwxr-xr-x 3 root root 4096 1月 27 19:19 .
drwx------ 9 root root 4096 1月 27 19:05 ..
-rwxr-xr-x 1 root root 135 1月 27 19:05 get_env_details
-rw-r--r-- 1 root root 96 1月 27 19:05 initialize
-rw-r--r-- 1 root root 73 1月 27 19:05 postactivate
-rw-r--r-- 1 root root 75 1月 27 19:05 postdeactivate
-rwxr-xr-x 1 root root 66 1月 27 19:05 postmkproject
-rw-r--r-- 1 root root 73 1月 27 19:05 postmkvirtualenv
-rwxr-xr-x 1 root root 110 1月 27 19:05 postrmvirtualenv
-rwxr-xr-x 1 root root 99 1月 27 19:05 preactivate
-rw-r--r-- 1 root root 76 1月 27 19:05 predeactivate
-rwxr-xr-x 1 root root 91 1月 27 19:05 premkproject
-rwxr-xr-x 1 root root 130 1月 27 19:05 premkvirtualenv
-rwxr-xr-x 1 root root 111 1月 27 19:05 prermvirtualenv
drwxr-xr-x 6 root root 4096 1月 27 19:19 py2_test
(py2_test) root@onefine-virtual-machine:~/.virtualenvs# rm -rf py2_test/
(py2_test) root@onefine-virtual-machine:~/.virtualenvs# workon
# 这一步最好是先deactivate再rm,手误...
(py2_test) root@onefine-virtual-machine:~/.virtualenvs# deactivate
root@onefine-virtual-machine:~/.virtualenvs# workon
root@onefine-virtual-machine:~/.virtualenvs# cd /
root@onefine-virtual-machine:/# mkvirtualenv --python=/usr/bin/python3 py3_test
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /root/.virtualenvs/py3_test/bin/python3
Also creating executable in /root/.virtualenvs/py3_test/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
virtualenvwrapper.user_scripts creating /root/.virtualenvs/py3_test/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/py3_test/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/py3_test/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/py3_test/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/py3_test/bin/get_env_details
(py3_test) root@onefine-virtual-machine:/# workon
py3_test
(py3_test) root@onefine-virtual-machine:/# python
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>> exit()
(py3_test) root@onefine-virtual-machine:/# deactivate
root@onefine-virtual-machine:/#
注:注意新建Python2和Python3虚拟环境的不同方式。
三、Python豆瓣源
像Ubuntu的apt
和CentOS的yum
有镜像源一样,Python pip
也有。
豆瓣镜像地址:https://pypi.douban.com/simple/
在国内的强烈推荐豆瓣的镜像源:http://pypi.douban.com/simple/
,注意后面要有/simple
目录。
使用pip3安装Django:
root@onefine-virtual-machine:~# pip3 install django
Collecting django
Using cached https://files.pythonhosted.org/packages/36/50/078a42b4e9bedb94efd3e0278c0eb71650ed9672cdc91bd5542953bec17f/Django-2.1.5-py3-none-any.whl
Requirement already satisfied: pytz in /usr/lib/python3/dist-packages (from django)
Installing collected packages: django
Successfully installed django-2.1.5
root@onefine-virtual-machine:~#
改为使用豆瓣镜像源安装Django:
root@onefine-virtual-machine:~# pip3 uninstall django
...
Proceed (y/n)? y
Successfully uninstalled Django-2.1.5
root@onefine-virtual-machine:~#
root@onefine-virtual-machine:~# pip3 install -i https://pypi.douban.com/simple/ django
Collecting django
Downloading https://pypi.doubanio.com/packages/36/50/078a42b4e9bedb94efd3e0278c0eb71650ed9672cdc91bd5542953bec17f/Django-2.1.5-py3-none-any.whl (7.3MB)
100% |████████████████████████████████| 7.3MB 7.7MB/s
Requirement already satisfied: pytz in /usr/lib/python3/dist-packages (from django)
Installing collected packages: django
Successfully installed django-2.1.5
root@onefine-virtual-machine:~#
可以看到速度非常快。直接使用pip
的原理其实就是从Python的官方源https://pypi.org/
下载到本地,然后解包安装。使用指定镜像源很简单,用-i
指定就行了,就像上面一样。
两种方式
方式1:就像上面一样,使用 pip
命令安装扩展包时指定源。
方式2:配置文件
请参照:https://www.cnblogs.com/ZhangRuoXu/p/6370107.html