Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Hatch: `ERROR: Package 'hatch-demo' requires a different Python: 3.10.5 not in '<3.10,>=3.9'`

hatch new "Hatch Demo"
cd hatch-demo

In pyproject.toml, I set:

requires-python = ">=3.9,<3.10"

I run:

hatch env create

I get:

ERROR: Package 'hatch-demo' requires a different Python: 3.10.5 not in '<3.10,>=3.9'

I have both Python 3.9 and 3.10 installed via Homebrew. Python 3.9.13 is set as the default.

python3 --version
Python 3.9.13

How do I fix this so that Hatch will use Python 3.9.x when creating the environment for this project?

like image 749
clay Avatar asked Mar 11 '26 16:03

clay


2 Answers

The way it works with hatch (as far as I understand) is that the (default) environment is configured independently from the project requirements (those are for building the packages sdist/wheel/...). By default it will use the first python interpreter found on your $PATH (according to this), which I'm guessing is 3.10.5 in this case. This is independent of the python executable hatch itself is run with.

The solution then is to tell hatch in the pyproject.toml which python version to create the default environment with as described in the official docs:

[project]
name = "myproject"
requires-python = ">=3.9,<3.10"
...

[tool.hatch.envs.default]
python="3.9"  # <--

# other optional environment configuration, e.g.
path = ".venv"
dependencies = [
  "black",
  "pytest",
  ...
]

For this, the requested version needs to be installed on the system. It can also be an absolute path to the python executable.

Then issuing a hatch env create or a hatch run myfile.py should create a virtual environment with the correct python version, project dependencies and the virtual environment dependencies.

like image 165
M4a1x Avatar answered Mar 14 '26 04:03

M4a1x


You can try to force the python interpreter with which hatch is executed (and which I would hope is then used for the env) like this:

/full/path/to/python.310 -m hatch env create
like image 30
renefritze Avatar answered Mar 14 '26 04:03

renefritze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!