您的位置:首页 > 编程语言 > Python开发

python虚拟环境virtualenv创建及配置

2017-12-25 11:06 741 查看
python虚拟环境virtualenv创建及配置

virtualenv

用于创建一个隔离的python环境。可以有效解决版本冲突问题

eric@userver:~$ python --version
Python 2.7.12
eric@userver:~$ python2 --version
Python 2.7.12
eric@userver:~$ python3 --version
Python 3.5.2
eric@userver:~$ python3.6 --version
Python 3.6.3
eric@userver:~$


安装

eric@userver:~$ sudo pip install virtualenv

#如果报错 Command : pip can't found 输入

eric@userver:~$ sudo apt-get install python-pip python-dev build-essential


创建虚拟环境–指定版本进行创建

eric@userver:~$ virtualenv -p python3.6 env3.6
Running virtualenv with interpreter /usr/bin/python3.6
Using base prefix '/usr'
New python executable in /home/eric/env3.6/bin/python3.6
Also creating executable in /home/eric/env3.6/bin/python
Installing setuptools, pip, wheel...done.


启动和退出虚拟环境

#启用虚拟环境

eric@userver:~$ source env3.6/bin/activate
(env3.6) eric@userver:~$ #可以看到$PATH已经改变为env3.6

#退出虚拟环境

(env3.6) eric@userver:~$ deactivate
eric@userver:~$






virtualenvwrapper

virtualenvwrapper 是一个基于virtualenv的工具,可以将所有的虚拟环境集中起来管理。

安装

$ sudo pip install virtualenvwrapper


初始配置

#直接使用命令启动

$ source /usr/local/bin/virtualenvwrapper.sh

#添加到shell启动文件~/.bash或者~/.profile

#pyton virtual configure

export VIRTUALENV_USE_DISTRIBUTE=1 #总是使用pip/distribute
export WORKON_HOME=$HOME/.local/virtualenvs# 所有虚拟环境存储的目录
if [ -e $HOME/.local/bin/virtualenvwrapper.sh ];then
source $HOME/.local/bin/virtualenvwrapper.sh
else if [ -e /usr/local/bin/virtualenvwrapper.sh ];then
source /usr/local/bin/virtualenvwrapper.sh
fi
fi
export PIP_VIRTUALENV_BASE=$WORKON_HOME
export PIP_RESPECT_VIRTUALENV=true

#设置virtualenv别名

alias vte='virtualenv'
alias mkvte='mkvirtualenv'
alias deact='deactivate'
alias rmvte='rmvirtualenv'

#使配置生效

source ~/.bashrc




使用方法

1、帮助命令,查看所有 virtualenvwrapper –help

2、创建基本环境:
mkvirtualenv [环境名]


3、激活环境:
workon [环境名]


4、删除环境:
rmvirtualenv [环境名]


4、退出环境:
deactivate


5、列出所有环境:
workon


创建python3.6环境

$ mkvte -p python3.6 my_env3.6
Running virtualenv with interpreter /usr/bin/python3.6
Using base prefix '/usr'
New python executable in /home/eric/.local/virtualenvs/my_env3.6/bin/python3.6
Also creating executable in /home/eric/.local/virtualenvs/my_env3.6/bin/python
Installing setuptools, pip, wheel...
done.


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: