[Learning experience] Python virtual environment construction under Linux

Problem description: This article solves how to implement multiple versions of Python and build a Python virtual environment in a Linux system (Ubuntu for example).

1. Implementing multiple versions of Python in Linux systems

1. Step one: Find the download link for the tar compressed package corresponding to the Python version on the official website.

 Find the Python version you want in historical versions

 Right click -> Copy link 2. Step 2: Use wget in the terminal to download the Python version you just selected to Ubuntu.

wget https://www.python.org/ftp/python/3.8.16/Python-3.8.16.tgz

 3. Step 3: Install dependency packages before officially installing Python.

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev

4. Step 4: Unzip the tar file and compile it

# 解压
tar -zxvf Python-3.8.16.tar
# 进入文件夹
cd Python-3.8.16

# 指定安装位置prefix=/usr/local/python-3.8
# 优化快速安装--enable-optimizations
./configure prefix=/usr/local/python-3.8 --enable-optimizations

# 编译&&安装
sudo make && make install

After compilation and installation, Python3.8 is installed under /usr/local

 There is a python3 executable file (blue) under /usr/local/python-3.8/bin

 5. Step 5: Configure environment variables (create soft links)

sudo ln -s /usr/local/python-3.8/bin/python3.8 /usr/bin/python3.8

sudo ln -s /usr/local/python-3.8/bin/pip3.8 /usr/bin/pip3.8

In this way, you can use it by typing python3.8 directly in the terminal!

6. Step 6: Test it

 

 

2. Python virtual environment construction under Linux

1. Step one: Install the virtual environment library virtualenv and the virtual environment management library virtualenvwrapper

A brief introduction to virtualenvwrapper:

virtualenvwrapper is a Python virtual environment management tool that makes it easier to manage virtual environments. Compared to using venv or virtualenv directly, virtualenvwrapper has the following advantages:

  • Unified management environment: Use virtualenvwrapper to neatly place all virtual environments in one place, such as ~/.virtualenvs. The virtual environments created by venv and virtualenv are scattered everywhere and are difficult to manage.
  • Simple commands: virtualenvwrapper provides simple commands to create, delete, and copy virtual environments, such as mkvirtualenv, rmvirtualenv, cpvirtualenv, etc., which are more convenient to use.
  • Automatically activated. Using virtualenvwrapper can automatically activate the corresponding virtual environment when entering a certain directory.
  • Tab completion. virtualenvwrapper provides a Tab completion function for virtual environment names. After entering part of the name and pressing Tab, the matching environment name can be displayed.
pip3.8 install virtualenv
pip3.8 install virtualenvwrapper

[Note] The virtualenvwrapper.sh file can only be found through pip installation.

2. Step 2: Create a virtual environment storage directory

# cd到家目录
cd ~
# 创建目录存放虚拟环境
mkdir .virtualenvs

3. Step 3: Modify the .bashrc file

A brief introduction to the .bashrc file:

The .bashrc file is the configuration file of the Bash shell, which is used to configure the Bash shell environment, Shell variables, functions and other settings. The .bashrc file is stored in the user's home directory and named .bashrc.

Find the file path of virtualenvwrapper.sh, and write the found file path in the .bashrc file.

which virtualenvwrapper.sh

cd ~
vi .bashrc

Add the following three lines at the bottom of the file

# 设置虚拟环境的安装位置
export WORKON_HOME=$HOME/.virtualenvs

# 在不指定虚拟环境编译器的情况下,设置默认使用的python3编译器
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

# 后面接的是virtualenvwrapper.sh文件路径
source /home/wangxin/.local/bin/virtualenvwrapper.sh

Update virtualenvwrapper.sh

source /home/wangxin/.local/bin/virtualenvwrapper.sh

update.bashrc

source ~/.bashrc

4. Step 4: Common virtual environment commands

# 查看当前存在的虚拟环境
workon

# 创建一个指定Python版本的虚拟环境
mkvirtualenv -p python3.8 env_name

# 删除一个虚拟环境
rmvirtualenv env_name

# 进入虚拟环境
workon env_name

# 退出虚拟环境
deactivate

Guess you like

Origin blog.csdn.net/qq_39780701/article/details/130978366
Recommended