Python builds virtual environment

Time:2021-7-29

Statement: the following contents are my personal understanding. If you find any errors or questions, you can contact me for discussion

Why build a virtual environment.

In the process of normal development, different projects will encounter different third-party packages. If we are all in the python environment of the system, we will encounter many problems. Building an independent Python running environment through the virtual environment can make the projects not interfere with each other.

How to build

Generally, we use virtualenv to build a virtual environment. It is a third-party package that can create and manage Python virtual environment.

install

  • Windows, Linux or Mac can be installed directly through pip. It should be noted that when there are two pythons in the system, pay attention to the version of PIP and select the corresponding version for installation

    pip install virtualenv

Create virtual environment

We usually create a folder called env (of course, you can go to any name you like) to store different virtual environments

Enter the env folder and create a virtual environment named first through the command_ env

virtualenv first_env

Description of relevant parameters

  • -p: Select the Python interpreter you want to use. The default is the interpreter with virtualenv installed
  • – prompt = XX: set XX as the prompt prefix of the virtual environment

Using virtual environments

  • Windows

    • Enter the scripts directory of the virtual environment
    • Open the command line tool here
    • Enter activate. When the command prompt is prefixed with a virtual environment name, the activation is successful
  • Linux or Mac

    • Using source activation, when the command prompt is prefixed with a virtual environment name, the activation is successful

      source first_env/bin/activate

Exit virtual environment

Enter deactivate in the current virtual environment to exit when the prefix of the virtual environment name disappears at the command prompt

Delete virtual environment

When we do not need this virtual environment, we only need to delete the entire package of the virtual environment

Pipenv

brief introduction

Pipenv is a python dependency management tool released by Kenneth Reitz in January 2017 and is now maintained by pypa. Think of it as a combination of PIP and virtualenv.

Why use pipenv

Pipenv will automatically help you manage your virtual environment and dependent files, and provides a series of commands and options to help you implement various dependent and environment management related operations. In short, it is more convenient, perfect and safe.

How to use pipenv

install

pip install pipenv

establish

Execute under the project root directorypipenv install, pipenv will create a virtual environment according to the name of the project folder, and will generate pipfile and pipfile.lock in the project root directory to manage project dependencies (the dependencies installed by pipenv in the future will be automatically written to the pipfile file, and there is no need to maintain the requirements.txt file manually)

{{< admonition tip “prompt” true >}}

By default, pipenv will create the virtual environment in the ~ /. Virtualenvs directory and execute it in the project root directorypipenv --venvYou can view the specific location of the virtual environment corresponding to the project

{{< /admonition >}}

Activate use

Two ways

  • Execute under the project root directorypipenv shell
  • Execute under the project root directoryPipenv run + command

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Recommended Today

VBS obtains the operating system and its version number

VBS obtains the operating system and its version number ? 1 2 3 4 5 6 7 8 9 10 11 12 ‘************************************** ‘*by r05e ‘* operating system and its version number ‘************************************** strComputer = “.” Set objWMIService = GetObject(“winmgmts:” _  & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”) Set colOperatingSystems = objWMIService.ExecQuery _  (“Select * from […]