Pyenv可托管多个不同的Python版本。
Install pyenv: git clone https://github.com/pyenv/pyenv.git ~/.pyenv
Add ~/.pyenv/bin to PATH:
if [[ $(echo $PATH | grep 'pyenv' | wc -l) -eq 0 ]]; then PATH=$PATH:~/.pyenv/bin fi
查看当前系统上已经安装和正在使用的 python 版本:
zzh@ZZHPC:~$ pyenv versions * system (set by /home/zzh/.pyenv/version)
其中 system 表明是系统安装的包。* 表示当前正在使用的Python环境。
查看当前可以被安装的Python版本:
zzh@ZZHPC:~$ pyenv install -l | grep "^ 3.1[0-9]" | sort -r | head -10 3.14-dev 3.13-dev 3.13.0b1 3.12-dev 3.12.3 3.12.2 3.12.1 3.12.0 3.11-dev 3.11.9
安装指定版本的Python:
zzh@ZZHPC:~$ pyenv install 3.12.3 Downloading Python-3.12.3.tar.xz... -> https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tar.xz Installing Python-3.12.3... Traceback (most recent call last): File "<string>", line 1, in <module> File "/home/zzh/.pyenv/versions/3.12.3/lib/python3.12/bz2.py", line 17, in <module> from _bz2 import BZ2Compressor, BZ2Decompressor ModuleNotFoundError: No module named '_bz2' WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib? Traceback (most recent call last): File "<string>", line 1, in <module> File "/home/zzh/.pyenv/versions/3.12.3/lib/python3.12/curses/__init__.py", line 13, in <module> from _curses import * ModuleNotFoundError: No module named '_curses' WARNING: The Python curses extension was not compiled. Missing the ncurses lib? Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'readline' WARNING: The Python readline extension was not compiled. Missing the GNU readline lib? Traceback (most recent call last): File "<string>", line 1, in <module> File "/home/zzh/.pyenv/versions/3.12.3/lib/python3.12/ssl.py", line 100, in <module> import _ssl # if we can't import it, let the error propagate ^^^^^^^^^^^ ModuleNotFoundError: No module named '_ssl' ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib? Please consult to the Wiki page to fix the problem. https://github.com/pyenv/pyenv/wiki/Common-build-problems BUILD FAILED (Ubuntu 22.04 using python-build 2.4.1) Inspect or clean up the working tree at /tmp/python-build.20240515222234.6177 Results logged to /tmp/python-build.20240515222234.6177.log Last 10 log lines: esac; \ LD_LIBRARY_PATH=/tmp/python-build.20240515222234.6177/Python-3.12.3 ./python -E -m ensurepip \ $ensurepip --root=/ ; \ fi Looking in links: /tmp/tmpva3h6yei Processing /tmp/tmpva3h6yei/pip-24.0-py3-none-any.whl Installing collected packages: pip WARNING: The scripts pip3 and pip3.12 are installed in '/home/zzh/.pyenv/versions/3.12.3/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. Successfully installed pip-24.0
Recipe to fix:
sudo apt-get install build-essential zlib1g-dev libffi-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev liblzma-dev
缷载指定版本的Python:
zzh@ZZHPC:~$ pyenv uninstall 3.12.3 pyenv: version `3.12.3' not installed
zzh@ZZHPC:~$ pyenv install 3.12.3
Downloading Python-3.12.3.tar.xz...
-> https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tar.xz
Installing Python-3.12.3...
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/zzh/.pyenv/versions/3.12.3/lib/python3.12/tkinter/__init__.py", line 38, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named '_tkinter'
WARNING: The Python tkinter extension was not compiled and GUI subsystem has been detected. Missing the Tk toolkit?
Installed Python-3.12.3 to /home/zzh/.pyenv/versions/3.12.3
zzh@ZZHPC:~$ pyenv uninstall 3.12.3 pyenv: remove /home/zzh/.pyenv/versions/3.12.3? [y|N] y pyenv: 3.12.3 uninstalled zzh@ZZHPC:~$ sudo apt-get install tk-dev
zzh@ZZHPC:~$ pyenv install 3.12.3 Downloading Python-3.12.3.tar.xz... -> https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tar.xz Installing Python-3.12.3... Installed Python-3.12.3 to /home/zzh/.pyenv/versions/3.12.3
使用指定版本的Python:
zzh@ZZHPC:~$ pyenv global 3.12.3 zzh@ZZHPC:~$ pyenv versions system * 3.12.3 (set by /home/zzh/.pyenv/version)
Add ~/.pyenv/versions/3.12.3/bin to PATH:
if [[ $(echo $PATH | grep 'pyenv' | wc -l) -eq 0 ]]; then PATH=$PATH:~/.pyenv/bin:~/.pyenv/versions/3.12.3/bin fi
查看当前使用的Python版本:
zzh@ZZHPC:~$ python --version Python 3.12.3
使用Python自带的venv创建虚拟环境:
zzh@ZZHPC:/zdata/Github$ python -m venv zpython3.12_project1 zzh@ZZHPC:/zdata/Github$ cd zpython3.12_project1/ zzh@ZZHPC:/zdata/Github/zpython3.12_project1$ ll drwxrwxr-x 2 zzh zzh 4096 May 20 18:58 bin drwxrwxr-x 3 zzh zzh 4096 May 20 18:58 include drwxrwxr-x 3 zzh zzh 4096 May 20 18:58 lib lrwxrwxrwx 1 zzh zzh 3 May 20 18:58 lib64 -> lib -rw-rw-r-- 1 zzh zzh 256 May 20 18:58 pyvenv.cfg zzh@ZZHPC:/zdata/Github/zpython3.12_project1$ source bin/activate (zpython3.12_project1) zzh@ZZHPC:/zdata/Github/zpython3.12_project1$ pip install flask Collecting flask Downloading flask-3.0.3-py3-none-any.whl.metadata (3.2 kB) Collecting Werkzeug>=3.0.0 (from flask) Downloading werkzeug-3.0.3-py3-none-any.whl.metadata (3.7 kB) Collecting Jinja2>=3.1.2 (from flask) Downloading jinja2-3.1.4-py3-none-any.whl.metadata (2.6 kB) Collecting itsdangerous>=2.1.2 (from flask) Downloading itsdangerous-2.2.0-py3-none-any.whl.metadata (1.9 kB) Collecting click>=8.1.3 (from flask) Downloading click-8.1.7-py3-none-any.whl.metadata (3.0 kB) Collecting blinker>=1.6.2 (from flask) Downloading blinker-1.8.2-py3-none-any.whl.metadata (1.6 kB) Collecting MarkupSafe>=2.0 (from Jinja2>=3.1.2->flask) Downloading MarkupSafe-2.1.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (3.0 kB) Downloading flask-3.0.3-py3-none-any.whl (101 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 101.7/101.7 kB 7.0 kB/s eta 0:00:00 Downloading blinker-1.8.2-py3-none-any.whl (9.5 kB) Downloading click-8.1.7-py3-none-any.whl (97 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.9/97.9 kB 10.4 kB/s eta 0:00:00 Downloading itsdangerous-2.2.0-py3-none-any.whl (16 kB) Downloading jinja2-3.1.4-py3-none-any.whl (133 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.3/133.3 kB 8.7 kB/s eta 0:00:00 Downloading werkzeug-3.0.3-py3-none-any.whl (227 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 227.3/227.3 kB 6.8 kB/s eta 0:00:00 Downloading MarkupSafe-2.1.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (28 kB) Installing collected packages: MarkupSafe, itsdangerous, click, blinker, Werkzeug, Jinja2, flask Successfully installed Jinja2-3.1.4 MarkupSafe-2.1.5 Werkzeug-3.0.3 blinker-1.8.2 click-8.1.7 flask-3.0.3 itsdangerous-2.2.0 (zpython3.12_project1) zzh@ZZHPC:/zdata/Github/zpython3.12_project1$ python Python 3.12.3 (main, May 16 2024, 09:18:37) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import flask >>>
安装的包在如下位置:
zzh@ZZHPC:/zdata/Github/zpython3.12_project1/lib/python3.12/site-packages$ lh drwxrwxr-x 5 zzh zzh 4.0K May 20 18:58 pip drwxrwxr-x 2 zzh zzh 4.0K May 20 18:58 pip-24.0.dist-info drwxrwxr-x 3 zzh zzh 4.0K May 20 18:59 markupsafe drwxrwxr-x 2 zzh zzh 4.0K May 20 18:59 MarkupSafe-2.1.5.dist-info drwxrwxr-x 3 zzh zzh 4.0K May 20 18:59 itsdangerous drwxrwxr-x 2 zzh zzh 4.0K May 20 18:59 itsdangerous-2.2.0.dist-info drwxrwxr-x 3 zzh zzh 4.0K May 20 18:59 click drwxrwxr-x 2 zzh zzh 4.0K May 20 18:59 click-8.1.7.dist-info drwxrwxr-x 3 zzh zzh 4.0K May 20 18:59 blinker drwxrwxr-x 2 zzh zzh 4.0K May 20 18:59 blinker-1.8.2.dist-info drwxrwxr-x 9 zzh zzh 4.0K May 20 18:59 werkzeug drwxrwxr-x 2 zzh zzh 4.0K May 20 18:59 werkzeug-3.0.3.dist-info drwxrwxr-x 3 zzh zzh 4.0K May 20 18:59 jinja2 drwxrwxr-x 2 zzh zzh 4.0K May 20 18:59 jinja2-3.1.4.dist-info drwxrwxr-x 5 zzh zzh 4.0K May 20 18:59 flask drwxrwxr-x 2 zzh zzh 4.0K May 20 18:59 flask-3.0.3.dist-info
新建一个虚拟环境不安装flask:
zzh@ZZHPC:/zdata/Github$ python -m venv zpython3.12_project2 zzh@ZZHPC:/zdata/Github$ cd zpython3.12_project2 zzh@ZZHPC:/zdata/Github/zpython3.12_project2$ source bin/activate (zpython3.12_project2) zzh@ZZHPC:/zdata/Github/zpython3.12_project2$ python Python 3.12.3 (main, May 16 2024, 09:18:37) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import flask Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'flask' >>>
导入flask失败。可见安装的包是基于每个虚拟环境的,不会共用。
直接输入deactivate可退出当前虚拟环境:
(zpython3.12_project2) zzh@ZZHPC:/zdata/Github/zpython3.12_project2$ deactivate zzh@ZZHPC:/zdata/Github/zpython3.12_project2$
使用pyenv-virtualenv创建虚拟环境:
Install pyenv-virtualenv: git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
创建一个virtualenv环境:
zzh@ZZHPC:~$ pyenv virtualenv 3.12.3 zpython3.12
激活当前需要使用的virtualenv:
zzh@ZZHPC:~$ pyenv activate zpython3.12
Failed to activate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
Add init actions to .bashrc:
if command -v pyenv 1>/dev/null 2>&1; then eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)" fi
zzh@ZZHPC:~$ pyenv activate zpython3.12 (zpython3.12) zzh@ZZHPC:~$
去除当前需要使用的virtualenv:
(zpython3.12) zzh@ZZHPC:~$ pyenv deactivate zzh@ZZHPC:~$
删除当前需要使用的virtualenv:
zzh@ZZHPC:~$ pyenv virtualenv-delete zpython3.12 pyenv-virtualenv: remove /home/zzh/.pyenv/versions/3.12.3/envs/zpython3.12? (y/N) y
Pipenv包装了virtualenv,使用起来更加方便:
zzh@ZZHPC:~$ command -v pip /home/zzh/.pyenv/shims/pip zzh@ZZHPC:~$ which pip /home/zzh/.pyenv/shims/pip zzh@ZZHPC:~$ pip install pipenv Collecting pipenv Downloading pipenv-2023.12.1-py3-none-any.whl.metadata (19 kB) Collecting certifi (from pipenv) Downloading certifi-2024.2.2-py3-none-any.whl.metadata (2.2 kB) Collecting setuptools>=67 (from pipenv) Downloading setuptools-69.5.1-py3-none-any.whl.metadata (6.2 kB) Collecting virtualenv>=20.24.2 (from pipenv) Downloading virtualenv-20.26.2-py3-none-any.whl.metadata (4.4 kB) Collecting distlib<1,>=0.3.7 (from virtualenv>=20.24.2->pipenv) Downloading distlib-0.3.8-py2.py3-none-any.whl.metadata (5.1 kB) Collecting filelock<4,>=3.12.2 (from virtualenv>=20.24.2->pipenv) Downloading filelock-3.14.0-py3-none-any.whl.metadata (2.8 kB) Collecting platformdirs<5,>=3.9.1 (from virtualenv>=20.24.2->pipenv) Downloading platformdirs-4.2.2-py3-none-any.whl.metadata (11 kB) Downloading pipenv-2023.12.1-py3-none-any.whl (3.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.1/3.1 MB 101.6 kB/s eta 0:00:00 Downloading setuptools-69.5.1-py3-none-any.whl (894 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 894.6/894.6 kB 296.1 kB/s eta 0:00:00 Downloading virtualenv-20.26.2-py3-none-any.whl (3.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.9/3.9 MB 450.1 kB/s eta 0:00:00 Downloading certifi-2024.2.2-py3-none-any.whl (163 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 163.8/163.8 kB 747.2 kB/s eta 0:00:00 Downloading distlib-0.3.8-py2.py3-none-any.whl (468 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 468.9/468.9 kB 856.0 kB/s eta 0:00:00 Downloading filelock-3.14.0-py3-none-any.whl (12 kB) Downloading platformdirs-4.2.2-py3-none-any.whl (18 kB) Installing collected packages: distlib, setuptools, platformdirs, filelock, certifi, virtualenv, pipenv Successfully installed certifi-2024.2.2 distlib-0.3.8 filelock-3.14.0 pipenv-2023.12.1 platformdirs-4.2.2 setuptools-69.5.1 virtualenv-20.26.2 zzh@ZZHPC:~$
zzh@ZZHPC:~$ rm -rf ~/.pyenv/plugins/pyenv-virtualenv zzh@ZZHPC:~$ exit
zzh@ZZHPC:~$ command -v pipenv /home/zzh/.pyenv/shims/pipenv zzh@ZZHPC:~$ which pipenv /home/zzh/.pyenv/shims/pipenv
Installing packages for your project
Pipenv manages dependencies on a per-project basis. To install a package, change into your project’s directory (or just an empty directory for this tutorial) and run
$ cd myproject
$ pipenv install <package>
Note: Pipenv is designed to be used by non-privileged OS users. It is not meant to install or handle packages for the whole OS. Running Pipenv as root
or with sudo
(or Admin
on Windows) is highly discouraged and might lead to unintend breakage of your OS.
zzh@ZZHPC:/zdata/Github$ mkdir zpython zzh@ZZHPC:/zdata/Github$ cd zpython zzh@ZZHPC:/zdata/Github/zpython$ pipenv install numpy Creating a virtualenv for this project... Pipfile: /zdata/Github/zpython/Pipfile Using default python from /home/zzh/.pyenv/versions/3.12.3/bin/python3.12 (3.12.3) to create virtualenv... ⠸ Creating virtual environment...created virtual environment CPython3.12.3.final.0-64 in 189ms creator CPython3Posix(dest=/home/zzh/.local/share/virtualenvs/zpython-4_fgvs60, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, via=copy, app_data_dir=/home/zzh/.local/share/virtualenv) added seed packages: pip==24.0 activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator ✔ Successfully created virtual environment! Virtualenv location: /home/zzh/.local/share/virtualenvs/zpython-4_fgvs60 Creating a Pipfile for this project... Installing numpy... Resolving numpy... Added numpy to Pipfile's [packages] ... ✔ Installation Succeeded Pipfile.lock not found, creating... Locking [packages] dependencies... Building requirements... Resolving dependencies... ✔ Success! Locking [dev-packages] dependencies... Updated Pipfile.lock (726a898126c8b3dba9a236f339f7fc035602dd63fcf370ab486903db57c79fd0)! Installing dependencies from Pipfile.lock (c79fd0)... To activate this project's virtualenv, run pipenv shell. Alternatively, run a command inside the virtualenv with pipenv run. zzh@ZZHPC:/zdata/Github/zpython$ ll -rw-rw-r-- 1 zzh zzh 151 May 20 17:29 Pipfile -rw-r--r-- 1 zzh zzh 3915 May 20 17:30 Pipfile.lock zzh@ZZHPC:~/.local/share/virtualenvs/zpython-4_fgvs60$ lh drwxrwxr-x 3 zzh zzh 4.0K May 20 17:29 lib -rw-rw-r-- 1 zzh zzh 40 May 20 17:29 .gitignore -rw-rw-r-- 1 zzh zzh 339 May 20 17:29 pyvenv.cfg -rw-rw-r-- 1 zzh zzh 21 May 20 17:29 .project drwxrwxr-x 2 zzh zzh 4.0K May 20 17:29 src drwxrwxr-x 2 zzh zzh 4.0K May 20 17:30 bin
经过实验我认为pyenv+venv是最佳选择。