1

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'
>>> 
sinoroc
  • 18,409
  • 2
  • 39
  • 70
MaKaNu
  • 762
  • 8
  • 25
  • can you try renaming your package – Sandeep Kothari Oct 11 '20 at 12:06
  • Do you see any meaningful files, modules, packages in `python -m pip show --files 'my_package'`? If not, then there might be something wrong with your packaging, and you might want to edit the question to show the directory structure of the project as well as `setup.py` (or `pyproject.toml`). – sinoroc Oct 11 '20 at 12:23
  • @Saandeep Kothari the actual package Name is BoxSupDataset – MaKaNu Oct 11 '20 at 12:54
  • @sinroc this is the result of your given command changed to the actual Package name: https://pastebin.com/mnWxtyDX I also included the import with the result below in the pastebin – MaKaNu Oct 11 '20 at 12:58
  • The top level package is `boxsupdataset` (all lower case). Can you try `import boxsupdataset` instead of `import BoxSupDataset`? – sinoroc Oct 11 '20 at 13:11
  • okay this worked, but why does this work on windows with PascalCase? – MaKaNu Oct 11 '20 at 14:16

1 Answers1

1

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.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
  • One small question on the Aside: Is there any possibility to check code against pep235, as example with pylint? – MaKaNu Oct 14 '20 at 09:07
  • I do not know of anything like that specifically. I would have assumed that _pylint_ itself would notify that the import is wrong. Have you tried it explicitly? I can not try it, I do not have Windows. – sinoroc Oct 14 '20 at 09:42
  • I tested it and my pylint setup is showing as much as it can, so far I know on my WIndows. – MaKaNu Oct 15 '20 at 12:27