A tutorial for installing and using virtualenv in Deepin

  • 2021-07-22 12:21:10
  • OfStack

virtualenv is a tool for creating an isolated python virtual environment. It can create its own python environment in a separate directory, and programs running with virtualenv will not access the global python environment or the python environment that does not belong to its own directory, so it can play the role of isolating python environment.

Installing virtualenv

When developing an Python application, all third-party packages are installed by pip into the site-packages directory of Python. And only one version can be installed, so if we want to develop multiple applications at the same time, these applications all share one Python, but different applications depend on different versions of the third-party package, it will be difficult to deal with.

In this case, virtualenv can be used to create a set of "isolated" Python runtime environment for each application. In this way, the third-party packages that each application depends on can not affect each other.

First, we install virtualenv with pip:

sudo pip3 install virtualenv

Note: 1 Be sure to install with administrator privileges, otherwise you will be prompted that virtualenv cannot be found.

Create a virtual environment

After installing virtualenv, you can create a virtual environment from the command line. For example:

virtualenv --no-site-packages .venv

With this command, you can create a new directory named. venv under the current directory, which is the newly created virtual Python running environment. Adding the parameter--no-site-packages--indicates that there is no need to copy all third-party packages that have been installed in the system Python environment.

Using a virtual environment

The virtual environment needs to be entered through the source command.

source .venv/bin/activate

After executing the command, you can see that the command prompt has a prefix (. venv) indicating that an Python virtual environment named. venv is currently in use.

indoors31@indoors31-PC:~/Documents/Workspace/Hello$ source .venv/bin/activate
(.venv) indoors31@indoors31-PC:~/Documents/Workspace/Hello$

Exit the virtual environment

deactivate allows you to exit the currently used virtual environment.

(.venv) indoors31@indoors31-PC:~/Documents/Workspace/Hello$ deactivate
indoors31@indoors31-PC:~/Documents/Workspace/Hello$

Installing virtualenvwrapper

Using virtualenv needs to enter the corresponding path, and there are some differences between linux and windows, so we can simplify the operation of virtual environment by using virtualenvwrapper.

Installation steps:


sudo pip3 install virtualenvwrapper
mkdir $HOME/.virtualenvs  Save the directory of the virtual environment 
vim ~/.bashrc

Add the following command:


export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/share/virtualenvwrapper/virtualenvwrapper.sh

Save exit and execute source ~/. bashrc

Using virtualenvwrapper

You can create a virtual environment with the mkvirtualenv command:

mkvirtualenv .venv

After execution, a virtual environment named. venv is created in the directory set by WORKON_HOME and automatically entered.

As with virtualenv1, use the deactivate command to exit the virtual environment.

After exiting, entering the virtual environment again does not need to find the path like virtualenv, but can directly enter the virtual environment by using workon command:

workon .venv

Other commands for virtualenvwrapper r

mvirtualenv ENV Delete Runtime Environment ENV mkproject hello Create hello Project and Runtime Environment hello mktmpenv Creating a Temporary Runtime Environment lsvirtualenv lists available runtime environments lssitepackages lists the packages installed in the current environment

Summarize

Above is this site to introduce the installation of Deepin and use of virtualenv tutorial, I hope to help you!


Related articles: