Virtualenv is a tool for creating an isolated python environment. VIRTUALENV Create a folder that contains all the necessary executables to use the packages required by the Python project.
Installation
Pip Install Virtualenv
Basic use
- Create a virtual environment for a project:
$ cd my_project_dir$ virtualenv venv #venv为虚拟环境目录名, directory name customization
virtualenv venv
A folder will be created in the current directory, containing the Python executable file and pip
a copy of the library so that the other packages can be installed. The name of the virtual environment (in this case venv
) can be arbitrary, and omitting the name will place the file in the current directory.
In any directory where you run the command, this creates a copy of Python and puts it in a file called venv
.
You can choose to use a Python interpreter:
$ virtualenv-p/usr/bin/python2.7 venv The #-p parameter specifies the Python interpreter program path
This will use /usr/bin/python2.7
the Python interpreter in.
- To get started with a virtual environment, it needs to be activated:
$ source Venv/bin/activate
从现在起,任何你使用pip安装的包将会放在 venv
folder, which is isolated from the globally installed Python.
Install packages as usual, such as:
$ pip Install requests
- If you have temporarily completed your work in a virtual environment, you can deactivate it:
$ . Venv/bin/deactivate
This will return to the system's default Python interpreter, which will also return to the default, including the installed libraries.
To delete a virtual environment, simply delete its folder. (Execution rm -rf venv
).
Virtualenv here is a little inconvenient, because virtual start, stop scripts are in a specific folder, perhaps after some time, you may have a lot of virtual environment scattered throughout the system, you may forget their name or location.
Virtualenvwrapper
Since virtualenv does not facilitate centralized management of virtual environments, it is recommended to use Virtualenvwrapper directly. Virtualenvwrapper provides a series of commands that make it easy to work with virtual environments. It puts all your virtual environments in one place.
Install Virtualenvwrapper (make sure virtualenv is installed)
Pip Install Virtualenvwrapper
Pip Install Virtualenvwrapper-win #Windows使用该命令
After the installation is complete, write the following in ~/.BASHRC
Export Workon_home=~/envs
source/usr/local/bin/virtualenvwrapper.sh
First line: Virtualenvwrapper store the Virtual environment directory
Second line: Virtrualenvwrapper will be installed in the Python bin directory, so the path is under the Python installation directory bin/virtualenvwrapper.sh
SOURCE ~/.BASHRC #读入配置文件, effective immediately
Virtualenvwrapper Basic Use
1. Create a virtual environment mkvirtualenv
Mkvirtualenv venv
This creates a new virtual environment named Venv under the directory specified by the Workon_home variable.
If you want to specify a python version, you can specify the Python interpreter through "--python"
Mkvirtualenv--python=/usr/local/python3.5.3/bin/python venv
2. Basic commands
View the current virtual environment directory
[Email protected] ~]# Workonpy2py3
Switch to a virtual environment
Exiting a virtual environment
Deleting a virtual environment
Rmvirtualenv venv
This article refers to the link: http://pythonguidecn.readthedocs.io/zh/latest/dev/virtualenvs.html
Python Virtual Environment--virtualenv