python virtual environment --virtualenv

virtualenv is a tool to create isolated Python environment. Virtualenv create a file that contains all the necessary executable files, and to use the required engineering Python package.

installation

pip install virtualenv

Basic use

  1. Create a virtual environment for a project:
$ cd my_project_dir
$ virtualenv venv  #venv为虚拟环境目录名,目录名自定义

virtualenv venv will be created in the current directory in a folder that contains Python executable file, and a copy pip library, so you can install additional package.

Virtual environment name (in this case is venv) is arbitrary; if the file name will be omitted are in the current directory.

In any directory you run the command, which creates a copy of Python, and place it on the file called venv.

You can choose to use a Python interpreter:

$ virtualenv -p /usr/bin/python2.7 venv    # -p参数指定Python解释器程序路径

This will use the /usr/bin/python2.7 Python interpreter.

  1. To start using a virtual environment, it needs to be activated:
$ source venv/bin/activate 

From now on, whatever you use pip install the package will be placed venv folder, and cut off from the global installation of Python. Usual installation package, such as:

$ pip install requests
  1. If you temporarily completed the work in a virtual environment, you can disable it:
$ . venv/bin/deactivate

It will return to the default Python interpreter, including installed library will return to the default.

To delete a virtual environment, simply delete its folder.

rm -rf venv

Here virtualenv some inconvenience, because virtual start, stop scripts in a specific folder, probably after some time, you may have a lot of virtual environments scattered throughout the system, you might forget their names or location.

virtualenvwrapper

Given virtualenv not easy centralized management of virtual environments, it is recommended to use direct virtualenvwrapper. virtualenvwrapper provides a set of command and make virtual work environment is facilitated. It put all your virtual environment in one place.

  1. Installation virtualenvwrapper (virtualenv make sure you have installed)
pip install virtualenvwrapper
pip install virtualenvwrapper-win  #Windows使用该命令
  1. After installation is complete, the following is written in ~ / .bashrc
export WORKON_HOME=~/Envs  #virtualenvwrapper存放虚拟环境目录
source /usr/local/bin/virtualenvwrapper.sh  #virtrualenvwrapper会安装到python的bin目录下
source ~/.bashrc    #读入配置文件,立即生效

virtualenvwrapper basic use

  1. Create a virtual environment
mkvirtualenv venv  

This will create a new virtual environment called venv in WORKON_HOME variable specified directory.

To specify python version, python interpreter can be specified by "--python"

mkvirtualenv --python=/usr/local/python3.5.3/bin/python venv
  1. Basic Commands
  • View the current directory virtual environment
[root@localhost ~]# workon
py2
py3
  • Switch to the virtual environment
[root@localhost ~]# workon py3
(py3) [root@localhost ~]# 
  • Exit Virtual Environment
(py3) [root@localhost ~]# deactivate
[root@localhost ~]# 
  • Delete the virtual environment
rmvirtualenv venv
5437343-841d8d00474ac607.png

Reproduced in: https: //www.jianshu.com/p/dd8e6763d2c5

Guess you like

Origin blog.csdn.net/weixin_34392906/article/details/91142096
Recommended