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

Web Design Tutorial (3): Design Steps and Thinking

<br />Previous tutorial: Web Design Tutorial...

Two ways to start Linux boot service

Table of contents rc.local method chkconfig metho...

How to install jupyter in docker on centos and open ports

Table of contents Install jupyter Docker port map...

Troubleshooting and solutions for MySQL auto-increment ID oversize problem

introduction Xiao A was writing code, and DBA Xia...

A summary of the reasons why Mysql does not use date field index

Table of contents background explore Summarize ba...

Nginx uses reverse proxy to implement load balancing process analysis

Introduction Based on docker container and docker...

How to modify create-react-app's configuration without using eject

1. Why is eject not recommended? 1. What changes ...

Teach you how to build the vue3.0 project architecture step by step

Table of contents Preface: 1. Create a project wi...

A quick solution to the first login failure in mysql5.7.20

First, we will introduce how (1) MySQL 5.7 has a ...

A brief discussion on the efficiency of MySQL subquery union and in

Recent product testing found a problem that when ...

Detailed tutorial on setting password for MySQL free installation version

Method 1: Use the SET PASSWORD command MySQL -u r...

CSS multi-level menu implementation code

This is a pretty cool feature that makes web page...

A very detailed explanation of Linux C++ multi-thread synchronization

Table of contents 1. Mutex 1. Initialization of m...