![](/style/images/good.png)
![](/style/images/bad.png)
virtualenv--python沙盒环境安装
source link: https://blog.51cto.com/u_13753753/5753582
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
virtualenv--python沙盒环境安装
精选 原创有人说:virtualenv、fabric 和 pip 是 pythoneer 的三大神器,今天学习安装下virtualenv
virtualenv可以搭建虚拟且独立的python环境,可以使每个项目环境与其他项目独立开来,保持环境的干净,解决包冲突问题
1.安装virtualenv
virtualenv实际上是一个python包,所以我们可以用easy_install或者pip安装或yum安装
(1).easy_install安装方式
easy_install virtualenv
(2).pip安装方式(我采用此方式安装)
easy_install pip
![virtualenv--python沙盒环境安装_python](https://s2.51cto.com/images/blog/202210/13110122_63477f8210c1652304.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184/format,webp)
![virtualenv--python沙盒环境安装_python_02](https://s2.51cto.com/images/blog/202210/13110139_63477f9350cb627126.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184/format,webp)
(3).yum安装方式(epel源)
2.安装指定的python
yum install openssl openssl-devel -y
下载python3.5.2
tar zxvf Python-3.5.2.tgz
cd /lost+found/Python-3.5.2
./configure --prefix=/usr/local/python3.5.2 --with-zlib
make && make install
下载python2.7.12
tar zxvf Python-2.7.12.tgz
cd /lost+found/Python-2.7.12
./configure --prefix=/usr/local/python2.7.12 --with-zlib
make && make install
设置环境变量(以下部分非必须操作)
vim /etc/profile
![virtualenv--python沙盒环境安装_虚拟环境_03](https://s2.51cto.com/images/blog/202210/13110610_634780a20cbd868880.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184/format,webp)
source /etc/profile
进入python2.7,此时敲入python,仍然会进入系统默认的python2.6.6,需要敲入python2.7,如下:
![virtualenv--python沙盒环境安装_python_04](https://s2.51cto.com/images/blog/202210/13110642_634780c237bf25649.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184/format,webp)
若果你想将Python的默认版本修改成Python2.7,按照以下步骤,系统的默认python放置在/usr/bin/python处(这个2.6是系统默认版本),因此此时需要建立软连接指向当前系统默认的python目录处,让系统使用新的python版本
查找默认python的位置
mv /usr/bin/python /usr/bin/python2.6-bak
ln -s /usr/local/python2.7.12/bin/python2.7 /usr/bin/python #删除软链接的放 rm /usr/bin/python
此时再输入python,即可进入python2.7.12
![virtualenv--python沙盒环境安装_virtualenv_05](https://s2.51cto.com/images/blog/202210/13110748_63478104557a168518.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184/format,webp)
3.创建python的虚拟环境
使用virtualenv命令创建python虚拟环境
![virtualenv--python沙盒环境安装_虚拟环境_06](https://s2.51cto.com/images/blog/202210/13110924_634781649f68559640.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184/format,webp)
执行后,在本地会生成一个与虚拟环境同名的文件夹,如果你的系统里安装有不同版本的python,可以使用--python参数指定虚拟环境的python版本
![virtualenv--python沙盒环境安装_虚拟环境_07](https://s2.51cto.com/images/blog/202210/13111021_6347819dbf1dd97249.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184/format,webp)
4.启动虚拟环境
source bin/activate
![virtualenv--python沙盒环境安装_virtualenv_08](https://s2.51cto.com/images/blog/202210/13111102_634781c697f2950887.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184/format,webp)
查看下虚拟环境的python版本
5.退出虚拟环境
6.使用virtualenvwrapper管理虚拟环境
virtualenvwrapper是virtualenv的扩展工具,可以方便的创建、删除、复制、切换不同的虚拟环境
(1).安装virtualenvwrapper
(2).创建一个文件夹,用于存放所有的虚拟环境
(3).设置环境变量,把下面两行添加到~/.bashrc里
export WORKON_HOME=~/workspaces
source /usr/bin/virtualenvwrapper.sh
![virtualenv--python沙盒环境安装_virtualenv_10](https://s2.51cto.com/images/blog/202210/13151441_6347bae1d771b18698.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184/format,webp)
source ~/.bashrc
(4).然后就可以使用virtualenvwrapper了,创建虚拟环境
此时可能会报错,错误信息为:LOG.addHandler(logging.NullHandler()) AttributeError: 'module' object has no attribute 'NullHandler'
解决方法如下:
卸载stevedore1.17.0
查看是否已经卸载成功
安装stevedore1.9.0
此时再次创建虚拟环境
![virtualenv--python沙盒环境安装_python_11](https://s2.51cto.com/images/blog/202210/13151822_6347bbbe683d476746.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184/format,webp)
(5).退出虚拟环境
(6).列出虚拟环境
(7).删除虚拟环境
(8).创建指定python版本的虚拟环境
mkvirtualenv env2 -p /usr/local/python3.5.2/bin/python3.5
![virtualenv--python沙盒环境安装_python_13](https://s2.51cto.com/images/blog/202210/13152102_6347bc5e3b6fa9076.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184/format,webp)
(9).切换虚拟环境
(10).查看环境安装了哪些包
![virtualenv--python沙盒环境安装_虚拟环境_14](https://s2.51cto.com/images/blog/202210/13152209_6347bca1e78d714049.png?x-oss-process=image/watermark,size_14,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=,x-oss-process=image/resize,m_fixed,w_1184/format,webp)
(11).进入当前环境的目录
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK