Python 项目管理工具 Rye 的工作原理

在开发Python项目时,有时会在一台电脑上,同时存在多个python项目,而且每个项目的python版本和依赖可能都不一样。此时需要使用python项目管理工具来进行管理。

rye是一个python项目管理工具。它的优点是安装和使用都比较简单,而且使用pyproject.toml来管理python项目。

本文简单介绍rye的工作原理。

pyproject.toml

pyproject.toml是PEP 518 提出的一种文件格式。
其中包含python项目构建系统的要求,和项目的一些配置。
这有一篇文档,展示了此文件的结构。packaging.python.org/en/latest/s…
此文件可以由多个段落组成。其中project段中的dependencies表示项目的依赖。

rye philosophy

推荐看看这篇文章rye-up.com/philosophy/ 这篇文章展示了rye的设计哲学。其中就包含了rye的一些优点。例如

  1. 不用给虚拟环境安装pip,就能管理依赖。
  2. 不使用系统的python。

Rye 的安装和配置shell和基本使用方式

安装和配置shell的方式

rye-up.com/guide/insta…
核心就是以下两行。

curl -sSf https://rye-up.com/get | bash
echo 'source "$HOME/.rye/env"' >> ~/.bashrc
curl -sSf https://rye-up.com/get | bash
echo 'source "$HOME/.rye/env"' >> ~/.bashrc
curl -sSf https://rye-up.com/get | bash echo 'source "$HOME/.rye/env"' >> ~/.bashrc

第一行,是安装。

  1. https://rye-up.com/get下载脚本,运行。
  2. 脚本其中核心步骤是下载一个二进制文件,运行这个二进制文件,来安装。至于这个二进制文件是如何把rye安装到系统中的,文章后面再详细阐述。

第二行,是配置shell(内容是配置环境变量)。核心内容是把$HOME/.rye/shims添加到$PATH的最前面。

➜ ~ cat "$HOME/.rye/env"
# rye shell setup
case ":${PATH}:" in
*:"$HOME/.rye/shims":*)
;;
*)
export PATH="$HOME/.rye/shims:$PATH"
;;
esac
➜  ~ cat "$HOME/.rye/env"


# rye shell setup
case ":${PATH}:" in
  *:"$HOME/.rye/shims":*)
    ;;
  *)
    export PATH="$HOME/.rye/shims:$PATH"
    ;;
esac
➜ ~ cat "$HOME/.rye/env" # rye shell setup case ":${PATH}:" in *:"$HOME/.rye/shims":*) ;; *) export PATH="$HOME/.rye/shims:$PATH" ;; esac

Rye实际上安装了哪些东西

rye的安装默认路径是~/.rye。rye的大致目录架构如下(以下是我的电脑上安装后用过几次的。刚安装时可能会少一些东西。):

➜ ~ tree -L 2 ~/.rye
/home/xyc/.rye
├── env
├── pip-tools
│ ├── cpython@3.11
│ └── cpython@3.9
├── py
│ ├── cpython@3.10.11
│ ├── cpython@3.11.3
│ └── cpython@3.9.16
├── self
│ ├── bin
│ ├── include
│ ├── lib
│ ├── lib64 -> lib
│ ├── pyvenv.cfg
│ └── tool-version.txt
└── shims
├── python
├── python3
└── rye
13 directories, 6 files
➜  ~ tree -L 2 ~/.rye
/home/xyc/.rye
├── env
├── pip-tools
│   ├── cpython@3.11
│   └── cpython@3.9
├── py
│   ├── cpython@3.10.11
│   ├── cpython@3.11.3
│   └── cpython@3.9.16
├── self
│   ├── bin
│   ├── include
│   ├── lib
│   ├── lib64 -> lib
│   ├── pyvenv.cfg
│   └── tool-version.txt
└── shims
    ├── python
    ├── python3
    └── rye

13 directories, 6 files
➜ ~ tree -L 2 ~/.rye /home/xyc/.rye ├── env ├── pip-tools │   ├── cpython@3.11 │   └── cpython@3.9 ├── py │   ├── cpython@3.10.11 │   ├── cpython@3.11.3 │   └── cpython@3.9.16 ├── self │   ├── bin │   ├── include │   ├── lib │   ├── lib64 -> lib │   ├── pyvenv.cfg │   └── tool-version.txt └── shims ├── python ├── python3 └── rye 13 directories, 6 files

rye是rust写的,但是它利用了python能力。在这篇文章rye-up.com/philosophy/ 中,作者表示rye不使用系统安装的python。这里的self文件,就是rye直接从github.com/indygreg/py… 下载的已构建的python。

基本使用方式

rye-up.com/guide/basic…
一般只要学会

  1. rye init <PROJECT_NAME>
  2. rye pin <PYTHON_VERSION>
  3. rye add <PACKAGE_NAME>
  4. rye sync
    即可

Rye 的基本使用方式的工作原理

rye init

这个没什么好阐述的。rye源码里有模板。rye获取一些信息,然后填到模板里,生成相关文件即可。

rye pin

把版本号写在.python-servion文件里。至于.python-version有什么用,文章后面后讲。

rye add

rye源代码里有个脚本模板

const PACKAGE_FINDER_SCRIPT: &str = r#"
...省略一些代码
finder = PackageFinder(
index_urls=sources["index_urls"],
find_links=sources["find_links"],
trusted_hosts=sources["trusted_hosts"],
)
...省略一些代码
print(json.dumps([x.as_json() for x in choices]))
"#;
const PACKAGE_FINDER_SCRIPT: &str = r#"


...省略一些代码

finder = PackageFinder(
    index_urls=sources["index_urls"],
    find_links=sources["find_links"],
    trusted_hosts=sources["trusted_hosts"],
)

...省略一些代码

print(json.dumps([x.as_json() for x in choices]))
"#;
const PACKAGE_FINDER_SCRIPT: &str = r#" ...省略一些代码 finder = PackageFinder( index_urls=sources["index_urls"], find_links=sources["find_links"], trusted_hosts=sources["trusted_hosts"], ) ...省略一些代码 print(json.dumps([x.as_json() for x in choices])) "#;

运行rye add时,rye把相应的参数填到此代码模板里,然后启动一个python解释器,运行py代码,如果用户提供的依赖名是可以正确的,就把依赖填到pyproject.toml里。

➜ learn_rye git:(master) ✗ rye add typer
Added typer>=0.9.0 as regular dependency
➜  learn_rye git:(master) ✗ rye add typer
Added typer>=0.9.0 as regular dependency
➜ learn_rye git:(master) ✗ rye add typer Added typer>=0.9.0 as regular dependency

rye sync

这个命令很关键。因为,rye add只是把依赖填到pyproject.toml里,不会真的去安装依赖。
rye sync会根据pyproject.toml的内容,更新虚拟环境。如果还没有虚拟环境,会创建一个。
rye sync的关键步骤如下

  1. 更新lock文件(这里不详细阐述其中原理了)
  2. 如果rye自己使用的python,还没有安装pip-tools,安装一下。
  3. 给pip-sync命令传入相应的参数,运行

以上就是rye的核心工作原理了。

rye的其它使用方式的工作原理。

rye-up.com/guide/shims…
rye支持python +3.8 my-script.py这种方式来运行任意版本的python解释器。

➜ ~ python +3.10 --version
Python 3.10.11
➜ ~ python +3.11 --version
Python 3.11.3
➜  ~ python +3.10 --version
Python 3.10.11
➜  ~ python +3.11 --version
Python 3.11.3
➜ ~ python +3.10 --version Python 3.10.11 ➜ ~ python +3.11 --version Python 3.11.3

原理是什么?

之前提到,安装rye后要配置一下shell,使得在shell中,$HOME/.rye/shimsPATH的最前面。这样,python指向的就不是实际的python解释器了,而是PATH的最前面。这样,`python`指向的就不是实际的python解释器了,而是`HOME/.rye/shims/python`。

➜ ~ which python
/home/xyc/.rye/shims/python
➜  ~ which python
/home/xyc/.rye/shims/python
➜ ~ which python /home/xyc/.rye/shims/python

它会把命令转发到指定版本的python解释器中。

总结

本文简单介绍了rye的工作原理。在研究它的工作原理的过程中,看了一下rye的源代码。看起来项目不大,值得学习一下。

如果你有兴趣,还可以深入地研究一下。我暂时就研究到这里了,以后也许发现了更多原理,再回来补充。

© 版权声明
THE END
喜欢就支持一下吧
点赞0

Warning: mysqli_query(): (HY000/3): Error writing file '/tmp/MY4TKEkn' (Errcode: 28 - No space left on device) in /www/wwwroot/583.cn/wp-includes/class-wpdb.php on line 2345
admin的头像-五八三
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

图形验证码
取消
昵称代码图片