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

How to install docker and portainer in kali

With the emergence of docker, many services have ...

Steps for IDEA to integrate Docker to achieve remote deployment

1. Enable remote access to the docker server Log ...

A simple example of creating a thin line table in html

Regarding how to create this thin-line table, a s...

How to avoid duplication of data when inserting in MySql batch

Table of contents Preface 1. insert ignore into 2...

Summary of js execution context and scope

Table of contents Preface text 1. Concepts relate...

How to introduce img images into Vue pages

When we learn HTML, the image tag <img> int...

IDEA graphic tutorial on configuring Tomcat server and publishing web projects

1. After creating the web project, you now need t...

MySQL login and exit command format

The command format for mysql login is: mysql -h [...

Understanding and example code of Vue default slot

Table of contents What is a slot Understanding of...

MySQL/MariaDB Root Password Reset Tutorial

Preface Forgotten passwords are a problem we ofte...

Java example code to generate random characters

Sample code: import java.util.Random; import java...

How to monitor array changes in Vue

Table of contents Preface Source code Where do I ...

How to set the text in the select drop-down menu to scroll left and right

I want to use the marquee tag to set the font scro...

Vue sample code for easily implementing virtual scrolling

Table of contents Preface Rolling principle accom...