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

Virtual environment using Python virtualenv

編輯:Python

If one python Projects need to rely on numpy==1.20.1 Version of , the other one python The project must depend on numpy==1.20.2 Version of . Although we can also use it directly docker Or other container solutions to isolate the programming environment , But it costs a lot of resources , Because we don't need to reconstruct the whole system . therefore python It also provides a more elegant solution : Use virtualenv To construct a virtual python Library environment , We can customize what we need python Dependent version . More detailed virtualenv Use method can refer to Official documents , Here we just do some simple introduction and demonstration of using method .

install virtualenv

virtualenv You can go directly through pip To install and manage , It also greatly simplifies our operation :

1
2
3
4
5
6
7
8
9
10
11
12
[[email protected] virtualenv]$ python3 -m pip install virtualenv
Collecting virtualenv
Downloading virtualenv-20.4.3-py2.py3-none-any.whl (7.2 MB)
|████████████████████████████████| 7.2 MB 7.5 MB/s
Requirement already satisfied: appdirs<2,>=1.4.3 in /home/dechin/anaconda3/lib/python3.8/site-packages (from virtualenv) (1.4.4)
Collecting distlib<1,>=0.3.1
Downloading distlib-0.3.1-py2.py3-none-any.whl (335 kB)
|████████████████████████████████| 335 kB 8.5 MB/s
Requirement already satisfied: filelock<4,>=3.0.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from virtualenv) (3.0.12)
Requirement already satisfied: six<2,>=1.9.0 in /home/dechin/anaconda3/lib/python3.8/site-packages (from virtualenv) (1.15.0)
Installing collected packages: distlib, virtualenv
Successfully installed distlib-0.3.1 virtualenv-20.4.3

It should be noted that , Here, though, we can go through virtualenv To construct a pure python Programming environment , however python The version of is directly dependent on what is contained in the system python Version of , We can't go through virtualenv To construct a different python edition .

virtualenv Use

virtualenv Basically, the steps of using can be simply divided into : Create an environment - Activate the environment - Configure and use the environment - Shut down the environment , The following is a demonstration .

Create a virtual environment

First we go to an empty directory :

1
2
[[email protected] virtualenv]$ ll
Total usage 0

And then directly execute virtualenv envname To build a virtual environment , Here, because there's only one in our system python edition , Multiple python Version of the environment needs to use -p Option to configure .

1
2
3
4
5
6
[[email protected] virtualenv]$ virtualenv test_env
created virtual environment CPython3.8.5.final.0-64 in 295ms
creator CPython3Posix(dest=/home/dechin/projects/2021-python/virtualenv/test_env, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/dechin/.local/share/virtualenv)
added seed packages: pip==21.0.1, setuptools==54.1.2, wheel==0.36.2
activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

After executing the above instructions , We found that a directory name consistent with the virtual environment name was generated under the directory just now :

1
2
3
[[email protected] virtualenv]$ ll
Total usage 4
drwxr-xr-x 4 dechin dechin 4096 4 month 1 21:06 test_env

This means that the virtual environment has been created successfully .

Activate the virtual environment

When using a specified virtual environment , We need to activate the virtual environment first , In the virtual environment Directory bin Directory , There is one named activate The executable of , It's used to activate the virtual environment :

1
2
3
4
5
6
7
[[email protected] virtualenv]$ source test_env/bin/activate
(test_env)[[email protected] virtualenv]$ python3 -m pip list
Package Version
---------- -------
pip 21.0.1
setuptools 54.1.2
wheel 0.36.2

We can find a feature , After activating the virtual environment , stay Linux The command line of is preceded by the name of the virtual environment , It is used to distinguish the current virtual environment . And here in the virtual environment python There are few third-party libraries , It's a very pure environment ,321 No damage to music We need to install and configure the environment manually .

Installation and configuration python library

The operation in the virtual environment is consistent with the installation operation in the actual environment , We can also use pip To manage the installation package , Only at this time, the changes executed by the installation will only be saved to the current virtual environment , Does not affect the actual environment and other virtual environment .

1
2
3
4
5
6
7
8
9
10
11
12
13
(test_env)[[email protected] virtualenv]$ python3 -m pip install numpy
Collecting numpy
Downloading numpy-1.20.2-cp38-cp38-manylinux2010_x86_64.whl (15.4 MB)
|████████████████████████████████| 15.4 MB 295 kB/s
Installing collected packages: numpy
Successfully installed numpy-1.20.2
(test_env)[[email protected] virtualenv]$ python3 -m pip list
Package Version
---------- -------
numpy 1.20.2
pip 21.0.1
setuptools 54.1.2
wheel 0.36.2

After installation , We see the current installation of numpy The version number is 1.20.2. Let's open a new terminal window and take a look at numpy Version number of :

1
2
3
4
5
6
7
8
9
10
11
[[email protected] virtualenv]$ python3 -m pip show numpy
Name: numpy
Version: 1.20.1
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email: None
License: BSD
Location: /home/dechin/anaconda3/lib/python3.8/site-packages
Requires:
Required-by: xarray, vaex-core, tifffile, tables, statsmodels, seaborn, scipy, scikit-learn, scikit-image, PyWavelets, pytools, pythreejs, pyscf, pyarrow, projectq, plotdigitizer, patsy, pandas, opencv-python, numexpr, numba, mkl-random, mkl-fft, matplotlib, ipyvolume, ipydatawidgets, imageio, hiq-projectq, h5py, cupy, bqplot, Bottleneck, bokeh, bkcharts, astropy, ts, hiqfermion

What can be distinguished here is , In a real environment, the command line is not preceded by the name of the virtual environment . We can see in the actual environment numpy The version is 1.20.1, In this way, we use such a way that the cost is not very high , Two different numpy The coexistence of versions .

Exit virtual environment

In the current virtual environment , Can be executed directly deactivate sign out . We can test such a scenario : Exit the virtual environment first , Then go back to , To confirm that the previous operation has been automatically saved to the virtual environment :

1
2
3
4
5
6
7
8
9
10
(test_env)[[email protected] virtualenv]$ deactivate
[[email protected] virtualenv]$ source test_env/bin/activate
(test_env)[[email protected] virtualenv]$ python3 -m pip list
Package Version
---------- -------
numpy 1.20.2
pip 21.0.1
setuptools 54.1.2
wheel 0.36.2
(test_env)[[email protected] virtualenv]$ deactivate

As can be seen from the above execution results , Yes python Changes in the environment are permanent . At this point, operation is better than docker Containers are easier , stay docker If you need to persist to save an operation , After mirroring the container, you need to , Perform extra commit Instructions can be saved .mp3 Songs are free to download So we think virtualenv It's a more elegant 、 More lightweight ,python Solutions to environmental differentiation Management .

Summary

In previous blogs, we have introduced docker The solution of programming environment based on container , But the container as a system level isolation scheme , In fact, more emphasis is placed on the isolation between users , This also benefits from NameSpace The promotion and use of Technology . But in part of the scene , For example, quickly build a pure python Environmental Science 、 Lightweight operations are implemented differently python The coexistence of packages , Although container technology can also be done , But the function is too redundant , This requires the use of the virtualenv Such a one python Rely on management solutions . In this paper we introduce virtualenv Installation and basic use of , Did a more complete demonstration .


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