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

What is the correct use of Python virtual environment in actual development?

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

The content of this article mainly focuses on how to correctly use the Python virtual environment in the actual development. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!

What is a virtual environment

To put it simply, a virtual environment is equivalent to a separate container. In this container, we can only install the dependent packages we need, and the containers are isolated from each other and do not affect each other. We all know that python has many wenb frameworks, Django,Flask, etc. In the actual development process, maybe a project we use Django framework, we can create a virtual environment with Django installed. Another project uses Flask to create a virtual environment with Flask installed.

Benefits of using a virtual environment

When actually developing or deploying a development environment, sometimes there are multiple projects on a single server. A dependency package may be used in each project, for example, two services are deployed in a system at the same time, one is Django 1.4 and the other is Django 1.7. At this point, the benefits of the virtual environment are realized, and it is easy to manage, such as activating the virtual environment where the project is located when updating a project, and installing dependency packages without affecting other projects that are already running. This is very important in the actual development process, and it is strongly recommended that you learn to use a virtual environment while learning python development, rather than wait until you deploy the project at work. At that time, there will be problems in a hurry, affecting the launch of the project, the problem can be big!

How to create a virtual environment

It is recommended to use virtualenvwrapper for centralized management of virtual environment.

Virtualenvwrapper provides a series of commands to make it easy to work with virtual environments. It puts all your virtual environments in one place.

1. Install virtualenvwrapper (make sure virtualenv is installed)

Virtualenvwrapper manages all virtual environments under the ~ / .virtualenvs directory by default. You can modify the environment variable WORKON_HOME to specify the storage directory of the virtual environment.

For example, the virtual environment on my computer is all stored on the F disk.

The installation commands are as follows:

Enter DOS command line mode and enter the following command

1 pip install virtualenvwrapper-win

two。 Create a virtual environment 1 mkvirtualenv test

This creates a new virtual environment named test in the directory specified by the WORKON_HOME variable

3. View the current virtual environment directory

Workon

Use this command to view the current virtual environment and its name

4. Activate the virtual environment

Workon test

After activation, the command line will be preceded by a virtual environment name (test)

At the same time, we can use pip list to view the dependent packages that have been installed in the current virtual environment

You can see the newly built virtual environment, with only the above three dependent packages

5. Exit the virtual environment

Deactive

1 (test) C:\ Users\ python > deactivate

2 C:\ Users\ python >

6. Delete virtual environment

Rmvirtualenv test

The use of Virtual Environment in Pycharm

After creating a new project in pycharm, import the virtual environment we just created

Click the gear symbol on the right in Settings-- > Project-- > Project Interpreter, select Add, select Existing environment, then go to the directory saved by the virtual environment, and select the test virtual environment we just created.

Then click ok apply along the way and wait for confirmation.

After importing the environment, our next step is to install the dependency package. We recommend that you use the following methods, which are very practical in project development.

Create a new requirements.txt file in the home directory of the project, then write the dependent package and its version number used in our project to the file, and Pycharm will automatically prompt you to install the required version of the dependent package.

Or click Terminal to install in command line mode

Pip install-r requirements.txt

Thank you for your reading. I believe you have a certain understanding of "what is the correct use of Python virtual environment in actual development". Go ahead and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!

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

Internet Technology

Wechat

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

12
Report