Search code examples
pythonpython-packaging

ModuleNotFoundError but pkg_resources found it


I am using on a Linux machine a venv with python 3.7.9 and installed one of my packages with pip. The install script should be fine because had no problem under windows with anaconda.

I installed the package in dev mode as also in normal mode.

If I try to import my package with:

import my_package

I got the well known ModuleNotFoundError. So I checked the sys.path if the site-packages folder where my package is installed is included.

Okay, So I checked which packages are available with pkg_resources:

import pkg_resources
installed_packages = {d.project_name: d.version for d in pkg_resources.working_set}
print(installed_packages)

The result:

{'torchvision': '0.7.0', 'torch': '1.6.0', 'tensorboardX': '2.1', 'six': '1.15.0', 
'setuptools': '47.1.0', 'protobuf': '3.13.0', 'pip': '20.2.3', 'Pillow': '7.2.0', 
'numpy': '1.19.2', 'future': '0.18.2', 'my_package': '1.0', 'absl-py': '0.10.0'}

As you can see my package is listed after future.

I have no clue why python struggles with importing my_package. Anyone a Idea what am I doing wrong?

I already tested those solutions with no result:

what shebang to use for python scripts run under a pyenv virtualenv

Python module not found in virtualenv

Module installed with PIP in virtualenv not found


python -m pip show --files 'BoxSupDataset'

Name: BoxSupDataset
Version: 1.0
Summary: UNKNOWN
Home-page: https://github.com/MaKaNu/PyTorch_Nasa_Dataset
Author: MaKaNu
Author-email: UNKNOWN
License: UNKNOWN
Location: /home/matti/GIT/PytorchLabs/Pytrochlabs-venv-3_7/lib/python3.7/site-packages
Requires: 
Required-by: 
Files:
  BoxSupDataset-1.0-py3.7.egg-info/PKG-INFO
  BoxSupDataset-1.0-py3.7.egg-info/SOURCES.txt
  BoxSupDataset-1.0-py3.7.egg-info/dependency_links.txt
  BoxSupDataset-1.0-py3.7.egg-info/top_level.txt
  boxsupdataset/__init__.py
  boxsupdataset/__pycache__/__init__.cpython-37.pyc
  boxsupdataset/__pycache__/nasa_box_sup_dataset.cpython-37.pyc
  boxsupdataset/__pycache__/utils.cpython-37.pyc
  boxsupdataset/nasa_box_sup_dataset.py
  boxsupdataset/transforms/__init__.py
  boxsupdataset/transforms/__pycache__/__init__.cpython-37.pyc
  boxsupdataset/transforms/__pycache__/denoise.cpython-37.pyc
  boxsupdataset/transforms/__pycache__/utils.cpython-37.pyc
  boxsupdataset/transforms/denoise.py
  boxsupdataset/transforms/utils.py
  boxsupdataset/utils.py
Python 3.7.9 (default, Aug 18 2020, 06:22:45) 
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import BoxSupDataset
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'BoxSupDataset'
>>> 

Solution

  • The issue seems to be due to a slight confusion between the name of the project (the thing that can be installed) and the name of the top level package (the thing that can be imported), with some case sensitivity issues adding to the confusion...

    In that particular case the project is indeed named BoxSupDataset (that's what you want to install). But the actual top level package is boxsupdataset, which is the only thing that matters for the imports:

    import boxsupdataset
    

    Aside: While on some (case insensitive) platforms (such as Windows?) it might be possible to import as import BoxSupDataset, the canonical way is import boxsupdataset (the exact same name and casing as the package or module). The (somewhat confusing) details can be found in PEP 235.