When we use Python, we may use multiple Python environments. Some Python versions are different, and some Python third-party modules are different. Therefore, we need to manage Python’s virtual environment.
The advantage of using the Python virtual environment is that, on the one hand, it can provide complete environment support for the project to run, such as Python versions, third-party modules, etc., on the other hand, it can specify the required environment for the project to run, avoiding interference between different versions of Python and different third-party modules. Python virtual environment management can be implemented in many ways, such as Conda and Python’s own management tool virtualenv.
This article will show you how to use Conda to manage Python virtual environments.
Manage the Python virtual environment using Conda
First, make sure you have Anconda installed on your system. The command to check whether Anconda is installed in the system is conda -v, as follows: \
View the version of Anconda
The common Conda commands are as follows:
Conda env list # check which virtual environments are installed conda update condaCopy the code
See which virtual environments Conda has installed
The command to create a virtual environment using Conda is as follows:
conda create -n your_env_name python=X.X
Copy the code
Your_env_name is the name of the virtual environment. Python is followed by the specified Python version, which can be 2.7, 3.6, 3.7, etc.
Once you have created your own virtual environment, you can activate it by running the following command:
Linux: source activate your_env_name or conda activate your_env_name Windows: activate your_env_nameCopy the code
To install third-party modules in a virtual environment, run the following command:
conda install -n your_env_name [package]
Copy the code
To exit the virtual environment, run the following command:
Linux: source deactivate or conda deactivate Windows: deactivateCopy the code
demo
In this section, we will create two virtual environments, Py2-LMJ and PY3-LMJ, one Python2 and one Python3, and show how to use them.
We demonstrate the 1/2 result with the output string “Hello World!” in py2-LMJ and PY3-LMJ respectively. .
[vagrant@localhost ~]$ conda create -n py3-lmj python=3.6
[vagrant@localhost ~]$ conda create -n py2-lmj python=2.7
[vagrant@localhost ~]$ conda env list
# conda environments:
#
py2-lmj /home/vagrant/.conda/envs/py2-lmj
py3-lmj /home/vagrant/.conda/envs/py3-lmj
base * /usr/local/Anaconda3
rasa_env /usr/local/Anaconda3/envs/rasa_env
tfgpu /usr/local/Anaconda3/envs/tfgpu
[vagrant@localhost ~]$ conda activate py3-lmj
(py3-lmj) [vagrant@localhost ~]$ python
Python 3.610. |Anaconda, Inc.| (default, May 8 2020.02:54:21)
[GCC 7.3. 0] on linux
Type "help"."copyright"."credits" or "license" for more information.
>>> 1/2
0.5
>>> print("hello world!")
hello world!
>>> print "hello world!"
File "<stdin>", line 1
print "hello world!"
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("hello world!")?
>>> quit()
(py3-lmj) [vagrant@localhost ~]$ conda deactivate
[vagrant@localhost ~]$ conda activate py2-lmj
(py2-lmj) [vagrant@localhost ~]$ python
Python 2.718. |Anaconda, Inc.| (default, Apr 23 2020.22:42:48)
[GCC 7.3. 0] on linux2
Type "help"."copyright"."credits" or "license" for more information.
>>> 1/2
0
>>> print("hello world!")
hello world!
>>> print "hello world!"
hello world!
>>> quit()
(py2-lmj) [vagrant@localhost ~]$ conda deactivate
Copy the code
Read more
5 minutes to master using C++\ in Cython
5 minutes to master common configuration files in Python \
Learn the Hook function \ in Python in 5 minutes
Special recommendation \
\
Click below to read the article and join the community