As far as I can tell all this did was write 3.11.1 to a ~/.python-version file. I’m not sure if there are any tools other than Rye which pay attention to this file. (UPDATE: pyenv uses this file too)
Actually fetching versions of Python can be done using rye toolchain fetch VERSION, for example:
1
% rye toolchain fetch 3.9
2
Downloading cpython@3.9.16
3
success: Downloaded cpython@3.9.16
This placed a whole bunch of files in ~/.rye/py/cpython@3.9.16/ - find . | wc -l reported 4,085 in that directory.
The one that matters most is:
1
~/.rye/py/cpython@3.9.16/install/bin/python3
Which gives me a 3.9.16 Python interpreter.
This is the feature of Rye I’m most excited about: the Python bundles it installs come from Gregory Szorc’s indygreg/python-build-standalone project.
This should mean that they completely ignore the many other weird ways that Python can end up installed on a system. Admittedly, this is a new weird way to install Python - but at least it shouldn’t clash with anything else.
Normally though you wouldn’t use rye toolchain fetch at all. The Rye suggested workflow is to run rye init in a new directory to create a pyproject.toml file:
1
% cd /tmp
2
/tmp % mkdir my-project
3
/tmp % cd my-project
4
my-project % rye init
5
success: Initialized project in /private/tmp/my-project/.
6
my-project % ls
7
README.md pyproject.toml
8
my-project % cat pyproject.toml
9
[project]
10
name = "my-project"
11
version = "0.1.0"
12
description = "Add a short description here"
13
authors = [
14
{ name = "Simon Willison", email = "...@gmail.com" }
15
]
16
dependencies = []
17
readme = "README.md"
18
requires-python = ">= 3.8"
19
license = { text = "MIT" }
20
21
[build-system]
22
requires = ["hatchling"]
23
build-backend = "hatchling.build"
24
25
[tool.rye]
26
managed = true
27
my-project % cat README.md
28
# my-project
29
30
Describe your project here.
31
32
* License: MIT
To add dependencies to that project, use rye add:
1
rye add httpx
This added the following line to pyproject.toml:
1
dependencies = ["httpx~=0.24.0"]
Then to actually install the dependencies, run rye sync:
1
my-project % rye sync
2
Initializing new virtualenv in /private/tmp/my-project/.venv
3
Python version: cpython@3.11.1
4
Generating production lockfile: /private/tmp/my-project/requirements.lock
5
Generating dev lockfile: /private/tmp/my-project/requirements-dev.lock
This adds a requirements.lock file that looks like this:
1
# generated by rye
2
# use `rye lock` or `rye sync` to update this lockfile
3
-e file:.
4
anyio==3.6.2
5
certifi==2022.12.7
6
h11==0.14.0
7
httpcore==0.17.0
8
httpx==0.24.0
9
idna==3.4
10
sniffio==1.3.0
And a requirements-dev.lock file with identical contents. Presumably this starts to differ as you install dev requirements (another Rye feature).
Your folder will now have a .venv hidden folder in it. Inside that is a Python virtual environment containing your installed dependencies and a file called rye-venv.json which just contains:
The rye add X command adds a dependency to your current virtual environment / Rye project.
rye install does something completely different: it installs new global packages, in a similar fashion to pipx in that they get their own isolated environments so their dependencies don’t clash with other installed applications.
1
% rye install cowsay
2
Collecting cowsay
3
Downloading cowsay-5.0.tar.gz (25 kB)
4
Installing build dependencies ... done
5
Getting requirements to build wheel ... done
6
Preparing metadata (pyproject.toml) ... done
7
Building wheels for collected packages: cowsay
8
Building wheel for cowsay (pyproject.toml) ... done
9
Created wheel for cowsay: filename=cowsay-5.0-py2.py3-none-any.whl size=25707 sha256=de872e9ef328d25cd9ac58df693c7bfd913033f696282c60562854d0db38737e
10
Stored in directory: /Users/simon/Library/Caches/pip/wheels/d4/2d/c7/c018bd8e6f825d6b47ae38f28baabd4588b3857e0e7dbc8cd3
11
Successfully built cowsay
12
Installing collected packages: cowsay
13
Successfully installed cowsay-5.0
14
installed script cowsay
You can now run cowsay with ~/.rye/shims/cowsay:
1
% ~/.rye/shims/cowsay hello
2
_____
3
| hello |
4
=====
5
\
6
\
7
^__^
8
(oo)\_______
9
(__)\ )\/\
10
||----w |
11
|| ||
You can add ~/.rye/shims to your $PATH to make these commands available everywhere.
I tried to install Datasette using rye install datasette and got this error:
1
% ~/.rye/shims/datasette
2
Traceback (most recent call last):
3
File "/Users/simon/.rye/shims/datasette", line 5, in <module>
4
from datasette.cli import cli
5
File "/Users/simon/.rye/tools/datasette/lib/python3.11/site-packages/datasette/cli.py", line 17, in <module>
6
from .app import (
7
File "/Users/simon/.rye/tools/datasette/lib/python3.11/site-packages/datasette/app.py", line 14, in <module>
8
import pkg_resources
9
ModuleNotFoundError: No module named 'pkg_resources'
Rye has strong opinions, including omitting pip and setuptools entirely from the environments that it creates.
[ UPDATE: I released Datasette 0.64.3 with a fix for this and now it installs correctly under Rye ]
It turns out Datasette includes code that imports pkg_resources, assuming that setuptools will be present because it’s usually there as a Python environment default!
I added setuptools to Datasette’s setup.py dependencies in an attempt to fix that, in issue #2065.
When I’m using pip I often install development copies of my projects by feeding them the URL to a .zip generated by GitHub, for example: