程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Sorting out the use of Python virtual environment

編輯:Python

python Use in Python A virtual environment .
Virtual environment is actually designed to meet the needs of using different versions on the same platform python、 library 、 Package . In professional terms, it is environmental isolation . In fact, it is understandable to follow the conventional thinking . On the same platform , Use the same library , There is bound to be conflict , In particular, configure environment variables , When all library packages are accessed in the same place . Because we all know , When referencing library packages , Basically, the path is specified , Then go to the specified path to find the file with the specified name . So putting it in the same folder will cause conflicts .
Environment isolation is actually changing the search path . In essence, it can be said that . let me put it another way , If you put your bag in another place , And when you use it , Directly specify the storage path of the version library package you want , And other configurations under this path ok, Then you can do it without the current virtual environment . But think about it , In fact, it is also a rough version of the virtual environment . Of course , You can also try to copy the master file to your project directory , Then the reference , It's OK, too .

Packages that use virtual environments are virtualenv. May I see if you have this bag in your library , If not, download .
pip3 install virtualenv
or
yum install python3-virtualenv

If the package is downloaded, you can proceed to the next step . Creating a virtual environment .
Find a place where you want to store items , then
virtualenv py_test
Wait for the program to run .
Activate the virtual environment :
source /py_test/bin/activate
Activating a virtual environment requires activate Of this file . So we need to use the path to specify him . If you are crazy at this time , I do not know! activate Is it a file or a command , please remember , It is a file generated after you just created the virtual environment . Then the next step is regular use python The operation of .
Exit the virtual environment if necessary
deactivate


The above description is linux Creation of virtual environment under the environment . Next is win Virtual environment creation under environment .
One 、 download virtualenv package
pip install virtualenv
Two 、 Creating a virtual environment
virtualenv py_test
3、 ... and 、 Activate the virtual environment
activate( Find it directly activate This file , Remember , There are no other suffixes , Then run it . It would be better to run in a black box , Otherwise, other situations cannot be controlled )
Four 、 Exit virtual environment
deactivate

Then there is another question to consider , We have created a virtual environment under the development environment for related development , After the development, we need to migrate , Can you take the environment directly ?
Looked at some information , It seems that you can't . Of course, it hasn't been verified yet . Now you can analyze how things have changed during the migration process ? Why is it not available after migration ?
If the platform is the same , Then we have reason to believe that it must be caused by the path change of some files , Otherwise it's just a file , As long as the environment is the same , After the migration , He should be able to run normally . And now you can't run , Then only from the environment 、 I started to check the path .
Use win Environment migration to linux The environment has been tested , No problem found , Very profitable operation . No report error . So the migration went well .

For virtual environments , We have experienced
The environment create ——》 Enter the environment ——》 Environment exit . Actually, this is just the use of virtual environment . We should also pay attention to , Management of virtual environment .
If there are multiple virtual environments , How to manage ?
At this time, it involves the current virtual environment instances ?
And how to see ?
It is not yet solved , Let's leave it to the future

About python A virtual environment , In fact, there are still some places that have not been handled completely . For example, the file hierarchy in the virtual environment , And the unique files and identifications in the virtual environment .

From the documents , Only

lib,scripts,.gitignore,pyvenv.cfg Just these points , It is estimated that cfg It can be changed . After a new environment cfg File changes can be perfectly adapted ? This needs to be verified .
 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved