Use Python virtual environment to create an independent Python running environment

What is a Python virtual environment?

Python virtual environment is a tool in Python for managing dependencies and project environments. Virtual environments allow you to isolate dependencies and environments between different projects on the same computer. In Python applications, we usually use a lot of third-party libraries and dependencies, and the libraries used in different projects may be different and the versions may be different. Using a virtual environment can avoid dependency problems between different projects and make the isolation between projects more complete.

A virtual environment is a directory that contains the Python interpreter and libraries to be used. In order to work in a virtual environment, the virtual environment must be activated. After activation, all required libraries and programs run within a namespace protected by the virtual environment, thus avoiding version confusion issues.

How to create a Python virtual environment?

1. First create a working directory:

mkdir transformers-course
cd transformers-course

2. Via Python venv命令来创建虚拟环境:

python -m venv .env

3. Check whether the creation is successful:

ls -a

Displayed as follows:

(.env) (base) [ipa@comm-agi]$ ls -a
.  ..    .env

4. activate 命令Enter the virtual environment using:

# Activate the virtual environment
source .env/bin/activate

(base) [ipa@comm-agi]$ source .env/bin/activate
(.env) (base) [ipa@comm-agi]$

(Please note that after executing the activate command, there is an extra (.env) in front of the command line prompt)

5. Use which python to verify whether the virtual environment is enabled:

(.env) (base) [ipa@comm-agi]$ which python

/data2/transformers-course/.env/bin/python

6. deactivate Exit the virtual environment using:

(.env) (base) [ipa@comm-agi]$ deactivate
(base) [ipa_sudo@comm-agi]$

(Please note that after executing the deactivate command, the (.env) in front of the command line prompt is gone)

Guess you like

Origin blog.csdn.net/duzm200542901104/article/details/132708237