virtualenv虚拟环境搭建
注意:
对于虚拟环境搭建及使用,python3.4及以上版本均内部已支持,而对于python3.4以下版本仍需通过安装virtualenv三方库的方式进行使用。
针对python3.4及以上版本创建虚拟环境可以直接使用以下方法实现:
py -3 -m venv venv
其中-3
指以python3的版本为基础创建虚拟环境,-m venv
指执行虚拟环境创建,最后的venv
指创建的虚拟环境安装在当前目录的venv目录下。
1. 安装virtualenv
通过pip
命令安装virtualenv
[vagrant@localhost ~]$ sudo pip3 install virtualenv
Collecting virtualenv
Downloading https://files.pythonhosted.org/packages/88/b6/9f2e13a71e5a7cd458dcf4f24540a4bd39206cc6290e8393a48d8b95c11e/virtualenv-16.4.1-py2.py3-none-any.whl (2.0MB)
100% |████████████████████████████████| 2.0MB 548kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.4.1
可以通过-i
设置使用豆瓣源进行加速安装
[vagrant@localhost ~]$ sudo pip3 install -i https://pypi.doubanio.com/simple/ --trusted-host pypi.doubanio.com virtualenv
Looking in indexes: https://pypi.doubanio.com/simple/
Requirement already satisfied: virtualenv in /usr/local/lib/python3.6/lib/python3.6/site-packages (16.4.1)
创建软连接
[vagrant@localhost ~]$ sudo ln -s /usr/local/lib/python3.6/bin/virtualenv /usr/bin/virtualenv
2. 创建一个python虚拟环境
在项目目录下创建虚拟环境,通过--no-site-packages
设置创建虚拟环境时不复制系统python环境中的任何第三方包,新建虚拟环境放置在当前目录下的venv
目录下,通过-p
设置以哪个系统环境创建虚拟环境
[vagrant@localhost ~]$ mkdir -p ~/src/djangoCloudMusic
[vagrant@localhost ~]$ cd ~/src/djangoCloudMusic
[vagrant@localhost djangoCloudMusic]$ virtualenv -p python3 --no-site-packages venv
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr/local/lib/python3.6'
New python executable in /home/vagrant/src/djangoCloudMusic/venv/bin/python3
Also creating executable in /home/vagrant/src/djangoCloudMusic/venv/bin/python
Installing setuptools, pip, wheel...
done.
3. 启动、退出虚拟环境
启动虚拟环境
[vagrant@localhost djangoCloudMusic]$ source venv/bin/activate
查询虚拟环境位置
(venv) [vagrant@localhost djangoCloudMusic]$ which python
~/src/djangoCloudMusic/venv/bin/python
退出虚拟环境
(venv) [vagrant@localhost djangoCloudMusic]$ deactivate
[vagrant@localhost djangoCloudMusic]$
4. 设置autoenv,进入目录自动激活虚拟环境
安装autoenv
[vagrant@localhost djangoCloudMusic]$ sudo yum install perl-Digest-SHA
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package perl-Digest-SHA.x86_64 1:5.85-4.el7 will be installed
--> Processing Dependency: perl(Digest::base) for package: 1:perl-Digest-SHA-5.85-4.el7.x86_64
--> Running transaction check
---> Package perl-Digest.noarch 0:1.17-245.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
============================================================================================================================================================================= Package Arch Version Repository Size =============================================================================================================================================================================Installing:
perl-Digest-SHA x86_64 1:5.85-4.el7 base 58 k Installing for dependencies:
perl-Digest noarch 1.17-245.el7 base 23 k
Transaction Summary
=============================================================================================================================================================================Install 1 Package (+1 Dependent package)
Total download size: 81 k
Installed size: 138 k
Is this ok [y/d/N]: y
[vagrant@localhost djangoCloudMusic]$ sudo pip3 install autoenv
Collecting autoenv
Downloading https://files.pythonhosted.org/packages/8f/4f/e6cb9dba8138dee540041124ba17db398c5e5c5fa8a46ee4a24d24621d3a/autoenv-1.0.0.tar.gz
Installing collected packages: autoenv
Running setup.py install for autoenv ... done
Successfully installed autoenv-1.0.0
配置环境变量
[vagrant@localhost djangoCloudMusic]$ echo 'source /usr/local/lib/python3.6/bin/activate.sh' >> ~/.bash_profile
[vagrant@localhost djangoCloudMusic]$ cat ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
source /usr/local/lib/python3.6/bin/activate.sh
载入用户环境变量
[vagrant@localhost djangoCloudMusic]$ source ~/.bash_profile
新建配置文件,向配置文件中写入启动命令
[vagrant@localhost djangoCloudMusic]$ echo "source ~/src/djangoCloudMusic/venv/bin/activate" > .env
[vagrant@localhost djangoCloudMusic]$ cat .env
source ~/src/djangoCloudMusic/venv/bin/activate
退出该目录,重新进入目录即会自动加载虚拟环境
[vagrant@localhost djangoCloudMusic]$ cd ..
[vagrant@localhost src]$ cd djangoCloudMusic/
autoenv:
autoenv: WARNING:
autoenv: This is the first time you are about to source /home/vagrant/src/djangoCloudMusic/.env:
autoenv:
autoenv: --- (begin contents) ---------------------------------------
autoenv: source ~/src/djangoCloudMusic/venv/bin/activate
autoenv:
autoenv: --- (end contents) -----------------------------------------
autoenv:
autoenv: Are you sure you want to allow this? (y/N) y
(venv) [vagrant@localhost djangoCloudMusic]$