Python uses virtual environments



 

Imagine the requirements here. Write a series of 1.0 version plugins used by a project. Now you want to write a new project and need to use the 2.0 version of these plugins. What should I do? Are they updated to version 2.0? In this way, the previous projects cannot be maintained.

At this time , we need a virtual environment. Python supports such a plug-in. Virtualenv

is installed below:

1. pip3 install virtualenv

2. cd into a directory like a virtual environment to create a virtual environment    



       virtualenv --no-site-packages venv

 



          This venv is the name, you can name it yourself

 



   

 



  3. Activate the virtual environment   

 

 

 

       source venv/bin/activate #source is the environment under linux under

  

 

                         windows, directly cd into the scripts directory, and execute activate

  

 

            here The venv is the name above.

  

 

    

  

 

       When this format appears, it means that it is now in the virtual environment

  

  

   

    F:\codebase\virtual environment virtualenv\ven_test\Scripts>activate


(ven_test) F:\codebase\virtual environment virtualenv\ven_test\Scripts> ;

   

    

     There are no plugins in the environment at this time, because we chose no-site-packages when we created this virtual environment

     Therefore, any plugins need to be reinstalled

   4. Exit the virtual environment

     deactivate 

  

  programming development ode">

   (ven_test) F:\code base\virtual environment virtualenv\ven_test\Scripts>deactivate

F:\code base\virtual environment virtualenv\ven_test \Scripts>

  

   

   

   

  In fact, in pycharm, this function is also encapsulated for us.

  Let's demonstrate how to use a virtual environment in pycharm.

  1. Create a virtual environment

  

   

  

  . Here you can choose whether to inherit the plug-in of the previous version

  . Then apply it to our project.

  

   

  In the project configuration, change the interpreter to the virtual environment we created.

  

   

   

   

  This is a new virtual environment. If you add an existing environment to pycharm, you need to select this

  

  and find the Python.exe in the scripts directory of the added environment, just add it

   



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326373667&siteId=291194637