Ubuntu20.04 Virtual Environment Tutorial for Installing Python3

  • 2021-08-12 04:13:06
  • OfStack

The following are done on my virtual machine

1. Install pip3

sudo apt install python3-pip

2. Install the virtual environment


sudo apt install virtualenv
sudo apt install virtualenvwrapper

3. Modify the configuration file to set the environment variables

cd ~
vim .bashrc

Add the following two lines after the. bashrc file


export WORKON_HOME=$HOME/.virtualenvs 
source /usr/share/virtualenvwrapper/virtualenvwrapper.sh 

Among them, the sentence "usr/share/virtualenvwrapper/virtualenvwrapper. sh" is different from other Ubuntu versions. Other bosses are "source/usr/local/bin/virtualenvwrapper. sh" on Ubuntu18.04, for example. The specific file location can be found and filled in by yourself
Tip: If you can't find virtualenvwrapper. sh, you can use the following command to find the path where the file is located, and replace the found path.


sudo find / -name virtualenvwrapper.sh

4. Enable profiles


source .bashrc

At this point, the virtual environment can be created normally.

PS: Let's take a look at Python3 creating a virtual environment

Purpose

The virtual environment is used to isolate Python libraries between different projects

Create a virtual environment

Python3 has built-in venv module. First, create a project directory. After entering the directory, execute

python3 -m venv venv

Activate the virtual environment

Before starting work, activate the corresponding virtual environment:

. venv/bin/activate

Under Windows:

venv\Scripts\activate

When activated, your terminal prompt will display the name of the virtual environment.

Install the appropriate modules, such as:

pip install Flask

Summarize


Related articles: