pypa / hatch

Modern, extensible Python project management
https://hatch.pypa.io/latest/
MIT License
5.74k stars 280 forks source link

ModuleNotFoundError after Packaging Projects tutorial #870

Open tbenst opened 1 year ago

tbenst commented 1 year ago

Following the tutorial at https://packaging.python.org/en/latest/tutorials/packaging-projects/, I created the following pyproject.toml:

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

[project]
name = "safari"
version = "0.1.0"
description = "Convolutions for Sequence Modeling "
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: Apache Software License",
    "Operating System :: OS Independent",
]

[tool.setuptools.dynamic]
dependencies = {file = ["requirements.txt"]}

[project.urls]
"Homepage" = "https://github.com/HazyResearch/safari"
"Bug Tracker" = "https://github.com/HazyResearch/safari/issues"

pip install -e . works, as verified by $ pip list | grep safari. However, unless I am in parent directory of package, when I run import safari I get ModuleNotFoundError: No module named 'safari'

Presume I am missing something basic? I didn't have this issue in past with setup.py.

tbenst commented 1 year ago

I also need to add setup.py in order for a local install to succeed, eg pip install -e . to succeed. Is this expected behavior? Seems undocumented at best.

#!/usr/bin/env python

from distutils.core import setup

setup(name='Safari',
      version='0.1',
      description='Convolutions for Sequence Modeling',
      url='https://github.com/HazyResearch/safari',
      packages=['safari'],
      package_dir = {'safari': 'src'}
     )

Edit: it appears the issue is the use of the folder name src. If I rename to safari, then pip install -e . succeeds, even without a setup.py. Tutorial seems to suggest a bad folder structure..?

ofek commented 1 year ago

I think your Python environment is just not correct. Can you please show me the versions of everything and what OS you are on?

tbenst commented 1 year ago

Certainly! Thanks for the response:

❯ python --version
Python 3.10.11
❯ pip --version
pip 23.1.2 from /home/tyler/opt/anaconda/envs/gaddy/lib/python3.10/site-packages/pip (python 3.10)
❯ which python
/home/tyler/opt/anaconda/envs/gaddy/bin/python
❯ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.2 LTS
Release:        22.04
Codename:       jammy
❯ uname -r
5.19.0-41-generic
❯ pip list | grep hatch
❯ pip list | grep safari
safari                   0.1.0        /home/tyler/code/safari
ofek commented 1 year ago

Can you please show me a commit that I can try?

jinq0123 commented 1 year ago

https://github.com/psf/black/blob/eb32729ab562c010d9f644a3bbb2305196b2deb7/pyproject.toml#L115-L117

m-walters commented 8 months ago

Found the same issue

ofek commented 8 months ago

Please show your config.

m-walters commented 8 months ago

Ah. it's probably more my own misuse. As you've mentioned here, for example, and from @jinq0123 comment above, I have learned that the sources and targets need to be configured correctly. And/Or your project needs a standard structure with a src/ directory. I'm very new to this. My project had several filetypes within a directory that wasn't named src, etc. I was experimenting anyway and do not need to resolve, thanks @ofek !

leowonglaw commented 5 months ago

I have this problem when using WSL (without remote connection)

andriihomiak commented 3 weeks ago

Just in case somebody encounters a similar issue, I mistakenly added src/ in the .gitignore (which is respected by hatch) causing the src-layout from the guide not to work properly - the pip install -e my-pkg finished ok, but import my_pkg failed with ModuleNotFoundError. Removing the src/ from .gitignore resolved this issue.