惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Azure Blog
Microsoft Azure Blog
Cloudbric
Cloudbric
I
InfoQ
V
V2EX
博客园_首页
The Register - Security
The Register - Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
S
Secure Thoughts
Vercel News
Vercel News
Forbes - Security
Forbes - Security
云风的 BLOG
云风的 BLOG
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
D
DataBreaches.Net
H
Hacker News: Front Page
Application and Cybersecurity Blog
Application and Cybersecurity Blog
B
Blog RSS Feed
A
About on SuperTechFans
N
News and Events Feed by Topic
Apple Machine Learning Research
Apple Machine Learning Research
Help Net Security
Help Net Security
Attack and Defense Labs
Attack and Defense Labs
N
Netflix TechBlog - Medium
Spread Privacy
Spread Privacy
F
Full Disclosure
Recorded Future
Recorded Future
AWS News Blog
AWS News Blog
博客园 - 【当耐特】
The Cloudflare Blog
T
Threatpost
T
Tor Project blog
Google DeepMind News
Google DeepMind News
C
CXSECURITY Database RSS Feed - CXSecurity.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
M
MIT News - Artificial intelligence
A
Arctic Wolf
C
Check Point Blog
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News - Newest:
Hacker News - Newest: "LLM"
WordPress大学
WordPress大学
Cyberwarzone
Cyberwarzone
小众软件
小众软件
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
Security Latest
Security Latest
The Last Watchdog
The Last Watchdog

Xiaobin's Notes

Mac本地快速部署DeepSeek Electron的原理 如何让大语言模型输出JSON格式 webstorm 的 cpu 占用高 修改Joplin主题样式 ECMAScript 历代版本 新一代包管理器 PNPM React useEffect() Hook Vue 2.x 使用高德地图JS API 2.0加载起点终点路径轨迹 家庭用电插座 MacOS 14.4 引发Java 应用崩溃 重定向广告 ElasticSearch集群原理 ElasticSearch集群节点 Elasticsearch Mapping 参数 Elasticsearch元数据 Elasticsearch的数据类型 Go语言的向后兼容和toolchain规则 Go 1.21 新增特性
Pyenv工具
xbl · 2024-01-08 · via Xiaobin's Notes

Python 版本管理工具的主要作用是帮助开发者在同一台机器上管理多个 Python 版本和环境。这对于开发和部署不同项目非常有用,因为不同项目可能依赖不同的 Python 版本或者不同的包版本。具体来说,Python 版本管理工具应有以下功能:

(1)避免依赖冲突,不同的项目可能依赖不同版本的库,使用版本管理工具可以创建独立的虚拟环境,避免依赖冲突。

(2)简化开发流程,开发者可以轻松地在不同的 Python 版本之间切换,而不需要重新安装或配置 Python。

(3)便于部署,减少冲突。在开发环境中使用与生产环境相同的 Python 版本和依赖,可以减少部署时出现的问题。

(4)共享环境配置,提高开发环境一致性。可以将环境配置文件(如 requirements.txtpyproject.toml)共享给团队成员,确保大家使用相同的开发环境。

一、工具选择

常见的管理工具有 Pyenv 和 Conda。Pyenv 是当前最流行的 Python 版本管理工具,支持多种 Python 版本,如 CPython、Anaconda、PyPy 等,功能全面且简单易用。Conda 最初由 Anaconda, Inc. 开发,主要用于 Python 和 R 编程语言的软件包(含 Python)及环境管理,特别适合跨平台、多语言项目,Python 版本管理只是其一小部分功能,若仅用于管理 Python 版本,Conda 有些大材小用,且系统较复杂、学习成本略高。相比之下,Pyenv 是常规项目 Python 版本管理的最优选择。

以下详细介绍 Pyenv 的使用方法。

二、Pyenv 安装

建议: 先卸载系统内置的 Python,否则可能导致 pyenv 设置不生效。

1. Windows

pyenv 本身是为 Unix 系统设计的。你可以使用 pyenv-win 这个项目,它是 pyenv 的 Windows 版本。

你需要在 PowerShell 中执行以下命令安装 pyenv-win:

1
Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"

重新打开 PowerShell,运行 pyenv –version 检查安装是否成功。

2. Linux

你可以使用以下命令来安装 pyenv

1
curl https://pyenv.run | bash

之后再将 pyenv 配置到环境变量中并使之生效,执行如下命令:

1
2
3
4
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc 
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
source ~/.bashrc

上述配置仅能使 pyenv 在 bash 环境生效,更多 shell 环境配置请参考:Set up your shell environment for Pyenv。配置的本质在于将$PYENV_ROOT 下的 shims 和 bin 目录配置到 PATH 变量中,且 shims 需配置在前。配置后的 PATH 如下:

1

三、Pyenv 基本用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

pyenv

pyenv install --help

pyenv version

python -c "import sys; print(sys.executable)"

pyenv versions

pyenv version

pyenv install --list

pyenv install 3.9.1

python --version

pyenv uninstall 3.9.1

pyenv global 3.9.1

pyenv local 3.9.1

pyenv shell 3.9.1

pyenv which python

pyenv rehash

Python 安装常见问题,可参考:Python common build problems

四、Pyenv 核心原理 -Shims

pyenv 通过 Shims 实现了对不同 Python 版本的透明管理和切换。

1. 工作原理

上述环境配置中,在 PATH 环境变量最前面插入一个 shims 目录,$(pyenv root)/shims:$(pyenv root)/bin:/usr/local/bin:/usr/bin:/bin。通过一个称为 rehashing 的过程,pyenv 在该目录中维护垫片,以匹配每个已安装的 Python 版本中的每个 Python 命令,如: python、pip 等。

Shims 是轻量级可执行文件,它只是将你的命令传递给 pyenv。因此,在安装了 pyenv 的情况下,当你运行 pip 时,你的操作系统将执行以下操作:

(1)搜索 PATH 环境变量,寻找 pip 可执行文件

(2)在 $(pyenv root)/shims 中找到 pip

(3)运行名为 pip 的 shim,它将命令传递给 pyenv

2. 作用

(1)通过使用 Shims,pyenv 可以实现对不同项目使用不同 Python 版本的灵活管理,而不需要手动修改环境变量或路径。

(2)你可以方便地在全局、目录级别甚至是 shell 会话级别设置或切换 Python 版本,极大地方便了开发和测试工作。

3. 示例

(1)假设你在项目 A 中使用 Python 3.8,而在项目 B 中使用 Python 3.9。通过 pyenv 和 Shims,你可以在项目目录中分别设置 Python 版本:

1
2
3
4

pyenv local 3.8.10

pyenv local 3.9.5

(2)当你在项目 A 目录中运行 python 命令时,Shims 会确保调用的是 Python 3.8.10,而在项目 B 目录中则会调用 Python 3.9.5。

通过这种方式,Shims 实现了对不同 Python 版本的透明管理和切换。

五、Pyenv 初始化操作源码解读

1. pyenv init -

用于初始化 pyenv,使其在当前 shell 会话中工作。运行后,执行如下命令(相关说明附在注释中):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39



PATH="$(bash --norc -ec 'IFS=:; paths=($PATH);
for i in ${!paths[@]}; do
if [[ ${paths[i]} == "''/root/.pyenv/shims''" ]]; then unset '\''paths[i]'\'';
fi; done;
echo "${paths[*]}"')"



export PATH="/root/.pyenv/shims:${PATH}"

export PYENV_SHELL=bash

source '/root/.pyenv/libexec/../completions/pyenv.bash'

command pyenv rehash 2>/dev/null




pyenv() {
local command
command="${1:-}"
if [ "$#" -gt 0 ]; then
shift
fi

case "$command" in
activate|deactivate|rehash|shell)
eval "$(pyenv "sh-$command" "$@")"
;;
*)
command pyenv "$command" "$@"
;;
esac
}

2. pyenv init –path

用于设置 PYENV_ROOT 环境变量,使得 pyenv 可以找到安装的 Python 版本。pyenv init - 包含 pyenv init --path 操作。

sh 或 bash 环境运行后,执行如下命令(相关说明附在注释中):

1
2
3
4
5
6
7
8
9
10
11
12


PATH="$(bash --norc -ec 'IFS=:; paths=($PATH);
for i in ${!paths[@]}; do
if [[ ${paths[i]} == "''/root/.pyenv/shims''" ]]; then unset '\''paths[i]'\'';
fi; done;
echo "${paths[*]}"')"

export PATH="/root/.pyenv/shims:${PATH}"

command pyenv rehash 2>/dev/null