Usage of Virtualenv

A tool to create isolated Python environments.

Posted by Dusign on 2019-12-11
Words 802 and Reading Time 3 Minutes
Viewed Times

Virtualenv is a tool to create isolated python environments. It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).

pyenv

pyenv 是一个 Python 的版本管理工具,它可以改变全局的 Python 版本,可以在 MAC 中同时安装多个版本的 Python,设置目录级别的 Python 版本,同时它还能创建和管理 Python 虚拟环境。

安装方法如下

1
2
3
brew update
brew install pyenv
pyenv -v

Python 安装

1
pyenv install PYTHON_VERSION

安装 Python时出现 ERROR

1
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

解决办法
1
xcode-select --install

Pyenv 常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 查看所有已安装的版本
pyenv versions

# 查看所有命令
pyenv commands

# 设置或显示本地的 python 版本
pyenv local

# 设置或显示全局的 python 版本
pyenv global

# 切换回系统版本
pyenv global system

# 解除 local 设置
pyenv local --unset

# 解除 shell 设置
pyenv shell --unset

# 设置或显示 shell 指定的 python 版本(本次会画)
pyenv shell

# 安装指定的 python 版本
pyenv install PYTHON_VERSION

# 卸载指定的 python 版本
pyenv uninstall PYTHON_VERSION

# 显示当前的 python 版本及其本地路径
pyenv version

# 显示安装路径
pyenv which

# 查看可安装的 python 版本
pyenv install --list

# 更新 pyenv
brew upgrade pyenv

如果上面的命令不成功,则需要配置一下环境变量

1
2
3
4
5
6
7
export PYENV_ROOT=~/.pyenv
export PATH=$PYENV_ROOT/shims:$PATH

# init pyenv
if which pyenv > /dev/null;
then eval "$(pyenv init -)";
fi

pyenv-virtualenv

Pyenv-virtualenv 是一款 pyenv 插件,可以用来搭建基于不同 Python 版本、虚拟且独立的 Python 环境,可以使每个项目环境与其他项目独立开来,保持环境的干净,解决包冲突问题。

安装方法

1
brew install pyenv-virtualenv

配置环境变量,在 ~/.bash_profile 中加入

1
2
3
4
# init pyenv-virtualenv
if which pyenv-virtualenv-init > /dev/null;
then eval "$(pyenv virtualenv-init -)";
fi

使用方法如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 使用当前版本创建 virtualenv
pyenv virtualenv NAME

# 创建指定版本的 virtualenv
pyenv virtualenv PYTHON_VERSION NAME

# 查看已创建的 virtualenv
pyenv versions

# 激活已创建的 virtualenv
pyenv activate NAME

# 停用已创建的 virtualenv
pyenv deactivate

# 自动激活 virtualenv
pyenv local NAME

# 取消自动激活
pyenv local --unset

# 删除 virtualenv
pyenv uninstall NAME

virtualenv

Virtualenv 通过创建一个虚拟化的 python 运行环境,将我们所需的依赖安装进去的,不同项目之间相互不干扰。

安装 virtualenv

1
pip3 install virtualenv

安装 virtualenvwrapper,virtualenvwrapper 可以管理不同的 python 环境

1
pip3 install virtualenvwrapper  # windows 为 virtualenvwrapper-win

linux 下建立软连接

1
ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv

linux 中添加环境变量

1
2
3
4
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
# 虚拟环境存储的目录,手动建立
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/python3/bin/virtualenvwrapper.sh

linux 执行命令让环境变量生效

1
source ~/.bashrc

mac 下环境变量配置

1
2
3
4
export VIRTUALENVWRAPPER_PYTHON=  # 命令 which python3 查询出来的路径
# 虚拟环境存储的目录
export WORKON_HOME=$HOME/.virtualenvs
source # 命令 which virtualenvwrapper.sh 查询出来的路径

mac 执行命令让环境变量生效

1
source ~/.bash_profile

virtualenv 相关命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 创建虚拟环境
mkvirtualenv NAME
mkvirtualenv NAME --python=python3.8

# 删除虚拟环境
rmvirtualenv NAME

# 查看所有虚拟环境
workon

# 进入虚拟环境
workon NAME

# 退出虚拟环境
deactivate

# 在虚拟环境中安装相关包
workon NAME
pip3 install PACKAGE_NAME

# 卸载虚拟环境中的相关包
workon NAME
pip3 uninstall PACKAGE_NAME

# 查看安装的包
pip3 list


If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !

...

...

00:00
00:00