Tutorial on installing and using virtualenv in Deepin

Tutorial on installing and using virtualenv in Deepin

virtualenv is a tool for creating isolated Python virtual environments. It can create its own Python environment in an independent directory. Programs run using virtualenv will not access the global Python environment, nor will they access Python environments that do not belong to their own directories, thus isolating the Python environment.

Install virtualenv

When developing Python applications, all third-party packages will be installed by pip into Python's site-packages directory. And only one version can be installed. Therefore, if we want to develop multiple applications at the same time, these applications all share the same Python, but different applications rely on different versions of third-party packages, it will be more difficult to handle.

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

First, we install virtualenv using pip:

sudo pip3 install virtualenv

Note: Be sure to install with administrator privileges, otherwise it will prompt that virtualenv cannot be found.

Creating a Virtual Environment

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

virtualenv --no-site-packages .venv

This command can create a new directory named .venv in the current directory, which contains the newly created virtual Python runtime 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 Virtual Environments

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 (.venv) prefix, indicating that the Python virtual environment named .venv is currently being used.

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

Exit the virtual environment

You can exit the currently used virtual environment by deactivate.

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

Install virtualenvwrapper

To use virtualenv, you need to enter the corresponding path, and there are some differences in usage under Linux and Windows. You can use virtualenvwrapper to simplify the operation of the virtual environment.

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 and exit, execute source ~/.bashrc

Using virtualenvwrapper

You can create a virtual environment using the mkvirtualenv command:

mkvirtualenv .venv

After execution, a virtual environment named .venv will be created in the directory set by WORKON_HOME just now and automatically entered.

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

After exiting, you don’t need to search for the path like virtualenv to enter the virtual environment again. You can directly use the workon command to enter the virtual environment:

workon .venv

Other commands of virtualenvwrapper

  • mvirtualenv ENV deletes the running environment ENV
  • mkproject hello creates the hello project and operating environment hello
  • mktmpenv creates a temporary operating environment
  • lsvirtualenv lists available runtime environments
  • lssitepackages lists the packages installed in the current environment

Summarize

The above is the tutorial on how to install and use virtualenv in Deepin introduced by the editor. I hope it will be helpful to everyone!

You may also be interested in:
  • How to create a quick launch icon for Ubuntu/Deepin
  • Deepin Windows XP Lite V5.8 perfect streamlined official version download address
  • deepin xp simplified version (220M) Download
  • Simple usage of Python's virtualenv (must read)

<<:  Detailed tutorial on downloading mysql on Windows 10

>>:  Vue project packaging and optimization implementation steps

Recommend

Pure CSS to adjust Div height according to adaptive width (percentage)

Under the requirements of today's responsive ...

HTML6 implements folding menu and accordion menu example code

The main part of the page: <body> <ul id...

Create a movable stack widget function using flutter

This post focuses on a super secret Flutter proje...

Server stress testing concepts and methods (TPS/concurrency)

Table of contents 1 Indicators in stress testing ...

js to achieve the effect of dragging the slider

This article shares the specific code of how to d...

MySQL uses frm files and ibd files to restore table data

Table of contents Introduction to frm files and i...

Detailed process of upgrading gcc (version 10.2.0) under CentOS7 environment

Table of contents Short Introduction 1. Check the...

How to uninstall MySQL cleanly (tested and effective)

How to uninstall Mysql perfectly? Follow the step...

HTML tbody usage

Structured Table (IExplore Only) 1) Group by rows ...

Table of CSS Bugs Caused by hasLayout

IE has had problems for a long time. When everyone...

Solution to the problem of English letters not wrapping in Firefox

The layout of text has some formatting requiremen...

MySQL database connection exception summary (worth collecting)

I found a strange problem when deploying the proj...

jQuery plugin to implement minesweeper game (2)

This article shares the second article of using j...

Detailed explanation of mysql execution plan id is empty (UNION keyword)

Introduction During the work process, slow querie...