Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python Virtual Environment under Linux system

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will give you a detailed explanation on how to use the Python virtual environment under the Linux system. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Why do you need a virtual environment

In fact, the fundamental problem to be solved in virtual environment is the confusion of all kinds of third-party packages.

If you receive three projects at the company,

One is using Django1.5, the old project.

One is to use Django1.8, the most recent project

One is using Django2.0+, 's new project.

You can only install one Django per computer, so what to do, you need a tool to separate the various versions.

This is just a situation, our big Python is most proud of a large number of third-party packages, when a project and another project of a large number of packages and the same package, it is over, no one can maintain this project except you.

Next, we will introduce the most commonly used and standardized third-party package.

Virtualenv

Add a package management tool

Virtualenvwrapper

With these two, the above problems are floating clouds.

Overview of Linux environment

This example is version 18.04 of Linux Ubuntu, as shown in the following figure.

The Python version comes with 3.6.9, as shown in the following figure.

The pip version has been updated to the latest, as shown in the following figure.

Pip update command:

Sudo Python3-m pip install-- upgrade pip

Installation configuration

1. Install virtualenv

Pip install virtualenv

two。 Install virtual environment management tools

Pip install virtualenvwrapper

Operation steps

1. Create a folder to store the virtual environment

Mkdir $HOME/.virtualenvs

Or

Mkdir .virtualenvs

In fact, under the current home directory, ~ means the current home directory, if it has been under ~, you don't need $HOME.

. The beginning is a hidden file, and a command is required

Ls-al

To see it.

two。 Execute a command

Which Python3

Look at the directory where Python is installed, as shown in the following figure.

3. Execute a command

Which virtualenvwrapper.sh

View the directory of virtualenvwrapper.

The red part is actually my home catalog, and it's OK to abbreviate it to ~ /.

4. Modify the. / bashrc file, using the command:

Vim .bashrc

You need to have a basic vim here. If you are not familiar with vim, take a look back.

Add content, as shown below.

Export WORKON_HOME=$HOME/.virtualenvs source ~ / .local/bin/virtualenvwrapper.sh # which virtualenvwrapper.sh command get VIRTUALENVWRAPPER_PYTHON=/usr/bin/Python3 # which Python3 command get

5. Refresh the configuration file

Source .bashrc

Common command

1. Create a new virtual environment

Mkvirtualenv spider

Create a new virtual environment of spider and enter the environment of spider.

(spider) indicates that you have entered the virtual environment of spider and are in use, as shown in the following figure.

two。 Exit the current virtual environment

Deactivate

3. View all virtual environments

Workon or lsvirtualenv

These two commands are generally acceptable.

Two have been created, flask and spider, as shown in the figure above.

4. Delete virtual environment

Rmvirtualenv flask

Flask virtual environment has been deleted

5. Enter a virtual environment

Tips

Even if you do not exit the current virtual environment, workon can switch directly to another virtual environment.

In Linux, commands in virtual environment can be completed automatically by tab key.

Command summary

This is the end of the article on "how to use the Python virtual environment under the Linux system". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report