Python Virtual Environment virtualenv installation and use details
- 2020-06-03 07:04:01
- OfStack
preface
Dynamic languages Ruby and Python have their own virtual environment. Virtual environment is an independent execution environment during program execution. Different virtual environments can be created in the same server for different systems to use. For example, projects can run in es5EN2-based environments and B can run in Python3-based environments. Python through virtualenv tools to manage virtual environments.
Install virtualenv
$ pip install virtualenv
Method of use
$ cd my_project_folder
$ virtualenv venv
After executing the command, it creates a folder in the current directory containing 1 Python execution files and copies of pip to install the other packges.
.
├ ─ ─ bin
│ ├ ─ ─ activate
│ ├ ─ ─ activate.csh
│ ├ ─ ─ activate.fish
│ ├ ─ ─ activate_this.py
│ ├ ─ ─ easy_install
│ ├ ─ ─ easy_install-3.5
│ ├ ─ ─ pip
│ ├ ─ ─ pip3
│ ├ ─ ─ pip3.5
│ ├ ─ ─ python -> python3
│ ├ ─ ─ python3
│ ├ ─ ─ python3.5 -> python3
│ └ ─ ─ wheel
├ ─ ─ include
│ └ ─ ─ python3.5m -> /Library/Frameworks/Python.framework/Versions/3.5/include/python3.5m
└ ─ ─ lib
└ ─ ─ python3.5
You can also select the specified Python interpreter when creating env, such as the virtual environment created based on Python3 below
$ virtualenv -p /usr/local/bin/python3 venv
By default, the virtual environment will depend on site packages in the system environment, which means that the installed third party package in the system will also be installed in the virtual environment. If you do not want to rely on these package, you can add the parameter -- ES35en-ES36en-ES37en to build the virtual environment
virtualenv --no-site-packages [ Virtual environment name ]
Activate the virtual environment
cd env
source ./bin/activate
After successful activation, the name of the virtual environment is displayed on the command line, similar to
((env)Your-Computer:your_project UserName$)
Exit the virtual environment
$ deactivate
If you want to delete the virtual environment, run it
rm -rf venv/
The command is enough.
Install Python packages in the virtual environment
Virtualenv comes with the pip installation tool, so the packages you need to install can run directly:
pip install [ The name of the suite ]
If the virtual environment is not started and the pip tool is installed in the system environment, packages will be installed in the system environment. To avoid this, add the following in the ~/.bashrc file:
export PIP_REQUIRE_VIRTUALENV=true
If you run pip without the virtual environment on, you will get an error:
Could not find an activated virtualenv (required).
Virtualenvwrapper
Virtaulenvwrapper is an extension of virtualenv for easier management of virtual environments. It can do:
1. Consolidate all virtual environments in one directory
2. Manage (add, delete, copy) the virtual environment
3. Switch virtual environments
4. ...
Install Virtualenvwrapper
You need virtualenv to be nearly installed before installing Virtualenvwrapper
$ cd my_project_folder
$ virtualenv venv
0
The default virtualenvwrapper is installed under /usr/local/bin. In fact, you need to run virtualenvwrapper. sh.
1. Create a directory to hold the virtual environment
$ cd my_project_folder
$ virtualenv venv
1
Edit ~/.zshrc or ~/.bashrc (depending on what type you are using)
export WORKON_HOME = $HOME/Envs source/usr/local/bin/virtualenvwrapper sh 3. Run:
$ cd my_project_folder
$ virtualenv venv
2
virtualenvwrapper is now available. Basic usage of virtualenvwrapper:
1. Make a list of virtual environments
workon or lsvirtualenv
New virtual environment
mkvirtualenv [Virtual environment Name]
2. Start/switch virtual environment
$ cd my_project_folder
$ virtualenv venv
4
3. Delete the virtual environment
$ cd my_project_folder
$ virtualenv venv
5
4. Leave the virtual environment and command like virutalenv1
$ cd my_project_folder
$ virtualenv venv
6
conclusion