Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outdated error message from hatchling when default module name selection fails during hatch shell #1778

Open
petermorrowdev opened this issue Nov 5, 2024 · 0 comments

Comments

@petermorrowdev
Copy link

petermorrowdev commented Nov 5, 2024

The error raised here appears to be out of date. I couldn't find any documentation or issues about this change to the build config, so I might be mistaken, but I felt like it was worth reaching out since hatch seems like it's not behaving as expected.

When a project name does not match the (snake_cased) module name hatch shell will throw this error recommend adding this kind of config to your pyproject.toml:

[tool.hatch.build.targets.wheel]
packages = ["src/foo"]

This appears to be incorrect and the correct recommendation seems to be:

[tool.hatch.build]
packages = ["src/foo"]

Looking through the issues I did see #1245 on improving documentation, so maybe this issue is already known. Or maybe I'm configuring hatch incorrectly..

Steps to reproduce issue

I'm using the latest version of hatch.

hatch --version
# Hatch, version 1.13.0

First, create a fresh hatch project called exampleprefix-foo with one module foo.

hatch new exampleprefix-foo
cd exampleprefix-foo
mv src/exampleprefix_foo src/foo

Then adjust the pyproject.toml to reflect the new module name foo removing unnecessary configs along the way.

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "exampleprefix-foo"
dynamic = ["version"]
description = ''
readme = "README.md"
requires-python = ">=3.8"
license = "MIT"
keywords = []
authors = []
dependencies = []

[tool.hatch.version]
path = "src/foo/__about__.py"

hatch shell with the project in this state will throw the error mentioned above, as expected.

Next, inject the recommended TOML into pyproject.toml

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "exampleprefix-foo"
dynamic = ["version"]
description = ''
readme = "README.md"
requires-python = ">=3.8"
license = "MIT"
keywords = []
authors = []
dependencies = []

[tool.hatch.version]
path = "src/foo/__about__.py"

# Since we have a module named `foo` in this example we can copy/paste verbatim
[tool.hatch.build.targets.wheel]
packages = ["src/foo"]

This will setup the default env successfully, but the foo package is not installed.

hatch shell
python -c 'import foo'
# Traceback (most recent call last):
#   File "<string>", line 1, in <module>
#     import foo
# ModuleNotFoundError: No module named 'foo'

How to resolve

Swap [tool.hatch.build.targets.wheel] for [tool.hatch.build]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "exampleprefix-foo"
dynamic = ["version"]
description = ''
readme = "README.md"
requires-python = ">=3.8"
license = "MIT"
keywords = []
authors = []
dependencies = []

[tool.hatch.version]
path = "src/foo/__about__.py"

[tool.hatch.build]
packages = ["src/foo"]

Then rebuild the hatch shell environment and try again.

hatch env remove
hatch shell
python -c 'import foo'

This works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant