MENU

Linux源码安装Python及Python虚拟环境

July 10, 2023 • Read: 1054 • 学习·笔记

背景

Linux 本身自带 Python2 和 Python3,不希望重新 apt/yum 更新Python版本污染主机环境,故额外安装版本比较好

root@VM-12-6-ubuntu:~# whereis python
python: /usr/bin/python2.7 /usr/bin/python3.6m /usr/bin/python /usr/bin/python3.6 /usr/bin/python3.6-config /usr/bin/python3.6m-config /usr/lib/python2.7 /usr/lib/python3.6 /usr/lib/python3.7 /usr/lib/python3.8 /etc/python2.7 /etc/python /etc/python3.6 /usr/local/lib/python2.7 /usr/local/lib/python3.6 /usr/include/python3.6m /usr/include/python3.6 /usr/share/python /usr/share/man/man1/python.1.gz
root@VM-12-6-ubuntu:~#
root@VM-12-6-ubuntu:~# python -V
Python 2.7.17
root@VM-12-6-ubuntu:~#
root@VM-12-6-ubuntu:~# python3 -V
Python 3.6.9
root@VM-12-6-ubuntu:~#

但是 https://www.python.org/downloads/ 下载页面中并没有形如 Java 的 Linux 解压版本可用,需要下载源码包编译来安装。各个版本的下载位置为:https://www.python.org/ftp/python/ ;以 3.10.11 为例子,下载页面为 https://www.python.org/ftp/python/3.10.11/ 源码连接为:https://www.python.org/ftp/python/3.10.11/Python-3.10.11.tgz

安装依赖(可选,已存在则跳过)

apt install -y make zlib1g-dev libffi-dev libssl-dev libbz2-dev libncursesw5-dev libgdbm-dev liblzma-dev libsqlite3-dev tk-dev uuid-dev libreadline-dev

apt install -y build-essential gdb lcov pkg-config \
      libbz2-dev libffi-dev libgdbm-dev libgdbm-compat-dev liblzma-dev \
      libncurses5-dev libreadline6-dev libsqlite3-dev libssl-dev \
      lzma lzma-dev tk-dev uuid-dev zlib1g-dev

apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl

apt install bzip2 libbz2-dev sqlite3 libsqlite3-dev libreadline6 libreadline6-dev libgdbm-dev uuid-dev tk-dev

上述依赖从别的博文找来的,存在重复的依赖

源码下载及解压

下载到 /opt/tmp 并解压,进入编译目录

root@VM-12-6-ubuntu:~# mkdir -p /opt/tmp && cd /opt/tmp
root@VM-12-6-ubuntu:/opt/tmp# wget https://www.python.org/ftp/python/3.10.11/Python-3.10.11.tgz
root@VM-12-6-ubuntu:/opt/tmp# tar xzf Python-3.10.11.tgz
root@VM-12-6-ubuntu:/opt/tmp# cd Python-3.10.11
root@VM-12-6-ubuntu:/opt/tmp/Python-3.10.11#

下载比较慢,网络不好的需要使用代理,建议使用 cloudflare 搭建免费的形如 https://ghproxy.com 的代理。

编译及安装到 /opt/software/python310

root@VM-12-6-ubuntu:/opt/tmp/Python-3.10.11# mkdir -p /opt/software/
root@VM-12-6-ubuntu:/opt/tmp/Python-3.10.11# ./configure --prefix=/opt/software/python310 --enable-loadable-sqlite-extensions --enable-optimizations
root@VM-12-6-ubuntu:/opt/tmp/Python-3.10.11# make
root@VM-12-6-ubuntu:/opt/tmp/Python-3.10.11# make install

安装及检查虚拟环境版本

此处使用主机原本的python3版本来安装虚拟环境

root@VM-12-6-ubuntu:/opt/tmp/Python-3.10.11# pip3 install virtualenv
root@VM-12-6-ubuntu:/opt/tmp/Python-3.10.11# virtualenv --version
virtualenv 20.17.1 from /usr/local/lib/python3.6/dist-packages/virtualenv/__init__.py

指定python版本创建虚拟环境

root@VM-12-6-ubuntu:/opt/tmp/Python-3.10.11# mkdir -p /opt/tmp/virtest && cd /opt/tmp/virtest
root@VM-12-6-ubuntu:/opt/tmp/virtest# python -V
Python 2.7.17
root@VM-12-6-ubuntu:/opt/tmp/virtest# virtualenv venv -p "/opt/software/python310/bin/python3"
created virtual environment CPython3.10.11.final.0-64 in 830ms
  creator CPython3Posix(dest=/opt/tmp/virtest/venv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==22.3.1, setuptools==65.6.3, wheel==0.38.4
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
root@VM-12-6-ubuntu:/opt/tmp/virtest# source ./venv/bin/activate
(venv) root@VM-12-6-ubuntu:/opt/tmp/virtest#
(venv) root@VM-12-6-ubuntu:/opt/tmp/virtest#
(venv) root@VM-12-6-ubuntu:/opt/tmp/virtest# python -V
Python 3.10.11
(venv) root@VM-12-6-ubuntu:/opt/tmp/virtest#

退出虚拟环境

(venv) root@VM-12-6-ubuntu:/opt/tmp/virtest# deactivate
root@VM-12-6-ubuntu:/opt/tmp/virtest#

本文由 ONE 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
如有版权疑问交流,请给我留言:oneisall8955@gmail.com
本文永久链接:http://liuzhicong.cn/index.php/study/linux-python-virtualenv.html

Archives QR Code Tip
QR Code for this page
Tipping QR Code