ubuntu: ubuntu22.04 creates python virtual environment

1. Premise:

A python interpreter has been installed and added to the system environment

You can see: ubuntu system: install python on ubuntu22.04, and set system environment variables_Donglin Muzhi's Blog-CSDN Blog

 2. Steps for creating a virtual environment in ubuntu

2.1. Installation package

Prepare to create a package for the virtual environment and a folder for storing the virtual environment

#1. Download virtualenv depends on
 sudo apt install virtualenv
 
#2. Need pip to install
 pip install virtualenv
 pip install virtualenvwrapper
 
#3. Create a .virtualenv folder in /home/the current user’s home directory
 sudo mkdir $HOME/.virtualenvs
 
 #This folder Is the virtual environment created with save
 

2.2. Modify the configuration file

#1. Check the directory where virtualenvwrapper.sh is located
sudo find / -name virtualenvwrapper.sh

        # Get: /home/liuhaizhang/.local/bin/virtualenvwrapper.sh

#2. Modify the configuration file
sudo vi ~/.bashrc

#Add the following two sentences
export WORKON_HOME=$HOME/.virtualenvs #Set the directory where the created virtual environment is saved
source ~/.local/bin/virtualenvwrapper.sh #Set the execution of virtual commands

2.3. Create a virtual environment

1. Create a virtual environment for python2
        #[Use python2 in the system environment to create]
mkvirtualenv virtual environment name

2. Create a virtual environment for python3
        #[Use python3 in the system environment to create]
mkvirtualenv -p python3 virtual environment name

3. Use the specified python interpreter to create a virtual environment
        #Assume that you have installed other versions of the interpreter at /opt/python37/bin/python3.7
mkvirtualenv -p /opt/python37/bin/python3.7 virtual environment name

2.4. Common commands in virtual environment

1. View the virtual environment
workon of the system and press Enter

2. Enter the virtual environment
workon virtual environment name

3. Launch the virtual environment
deactivate

4. Delete the virtual environment
rmvitualenv

Guess you like

Origin blog.csdn.net/weixin_46371752/article/details/130415226