SoFunction
Updated on 2025-03-03

Installing python virtual environment under Linux environment and precautions

Create python virtual environment virtualenv, virtualenvwrapper

1. Why do you need to build a virtual environment?

Since the project runs when two projects on the machine depend on different versions of the same package, the project will fail, and the virtual environment can be installed at this time.

2. What is a virtual environment

The virtual environment is a copy version of the python environment
A network connection is required when creating a virtual environment

3. Install python, pip, virtualenv

ubuntu:
sudo apt-get install python3    # Install python3sudo apt-get install python-pip    # Install pipsudo pip install virtualenv     #Installing a virtual environmentsudo pip install virtualenvwrapper   #Installing the virtual environment extension packagecentos:
sudo yum -y install python36 # Install python3sudo yum -y install python36-pip # Install pipyum install python-virtualenv #Installing a virtual environmentyum install python-virtualenvwrapper #Install the virtual environment extension package

Check the installation status:

python3.6 -V
pip3.6 -V

Add soft link:

# Use python3 to use Python3.6:ln -s /usr/bin/python3.6 /usr/bin/python3
# Copy code pip3.6 the same thing:ln -s /usr/bin/pip3.6 /usr/bin/pip3

4. Edit the virtual file and run it

# Edit files in the home directory (home directory)vim .bashrc 
# Add virtual environment address at the end (the virtual files are stored in .virtualenvs)export WORKON_HOME=$HOME/.virtualenvs 
# Add commands that can be used directly in the virtual environmentsource /usr/bin/  
# Run the file and take effectsource .bashrc 

5. Create a virtual environment

# Create a python3 virtual environment and name itmkvirtualenv -p python3 milepost
 # Exit the virtual environmentdeactivate 
 # Enter the virtual environment to workworkon web_working 
# List all virtual environmentslsvirtualenv -b
# cpvirtualenv replication environmentcpvirtualenv env1 env3 (copyenv1arriveenv3)
# List the site-packages content in the current environment (execute in the environment)lssitepackages 
# Clear all third-party packages in the environmentcdsitepackages 
 # Delete virtual environment project rmvirtualenv + Virtual environment name 

Notice:

1. The python used in the virtual environment is copied python, and the installation of the python package is also installed in the copied python, and there is no difference in other ways. Any operations such as creating files in a virtual environment exist in the real environment, but copying python and installing python packages are different from the real environment.

2. Install packages in a virtual environment. You cannot use sudo pip install. This command will install the packages into the real environment. Sudo should be removed.

Summarize

The above is the editor’s introduction to installing python virtual environment and precautions in the Linux environment. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!