Python安装虚拟环境 virtualenv 和 virtualenvwrapper

virtualenv 创建“独立”的Python运行环境的原理
把原始 Python 环境复制一份到 virtualenv 的环境,用命令 source venv/bin/activate 进入一个 virtualenv 环境时,virtualenv 会修改相关环境变量,让命令 python 和 pip 均指向当前的 virtualenv 环境。

VirtaulenvWrapper 是 virtualenv 的扩展包,用于更方便管理虚拟环境:

  • 将所有虚拟环境整合在一个目录下
  • 管理(新增,删除,复制)虚拟环境
  • 方便切换虚拟环境

  1. 安装 virtualenv
pip3 install virtualenv
  1. 安装 virtualenvwrapper
pip3 install virtualenvwrapper
  1. 配置 virtualenvwrapper 环境变量(基于用户环境变量~/.bashrc文件)
# Python VirtualEnv Environment
export VIRTUALENVWRAPPER_PYTHON=$PYTHON_HOME/bin/python3
export WORKON_HOME=/home/codedancing/ProEnv/PythonEnvs/
source /home/codedancing/ProEnv/Python/python3.7.6/bin/virtualenvwrapper.sh

编辑完成之后刷新环境变量:

source ~/.bashrc

出现下面的输出,则配置完成:

[codedancing@centos PythonEnvs]$ source ~/.bashrc
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/premkproject
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/postmkproject
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/initialize
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/predeactivate
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/preactivate
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/postactivate
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/get_env_details

WORKON_HOME : 指定虚拟环境存储目录
VIRTUALENVWRAPPER_PYTHON:指定 virtualenvwrapper 的 Python 解释器,不配置可能会导致 virtualenvwrapper 环境配置出现问题:

/usr/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks. 

If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
  1. 创建 Python 虚拟环境

指定虚拟环境名为 python3.7,执行 mkvirtualenv python3.7

[codedancing@centos PythonEnvs]$ mkvirtualenv python3.7
Using base prefix '/home/jialei/ProEnv/Python/python3.7.6'
New python executable in /home/codedancing/ProEnv/PythonEnvs/python3.7/bin/python3.7
Also creating executable in /home/codedancing/ProEnv/PythonEnvs/python3.7/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/python3.7/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/python3.7/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/python3.7/bin/preactivate
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/python3.7/bin/postactivate
virtualenvwrapper.user_scripts creating /home/codedancing/ProEnv/PythonEnvs/python3.7/bin/get_env_details

并自动切换到虚拟环境,查看虚拟环境列表:

(python3.7) [codedancing@centos PythonEnvs]$ lsvirtualenv
python3.7
=========

查看pip3:

(python3.7) [codedancing@centos PythonEnvs]$ pip3 -V
pip 19.3.1 from /home/codedancing/ProEnv/PythonEnvs/python3.7/lib/python3.7/site-packages/pip (python 3.7)

退出虚拟环境,查看 pip3:

(python3.7) [codedancing@centos PythonEnvs]$ deactivate
[codedancing@centos PythonEnvs]$ pip3 -V
pip 19.2.3 from /home/codedancing/ProEnv/Python/python3.7.6/lib/python3.7/site-packages/pip (python 3.7)
  1. 常用命令

所有的命令可使用:virtualenvwrapper --help 进行查看

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

删除环境:rmvirtualenv [环境名]

激活环境:workon [环境名]

退出环境:deactivate

列出所有环境:lsvirtualenv -b

发布了21 篇原创文章 · 获赞 6 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/codedancing/article/details/104016264