简体   繁体   English

ModuleNotFoundError 但 pkg_resources 找到了它

[英]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.我在一台 Linux 机器上使用一个带有 python 3.7.9 的 venv,并用 pip 安装了我的一个软件包。安装脚本应该没问题,因为在 windows 和 anaconda 下没有问题。

I installed the package in dev mode as also in normal mode.我在开发模式和正常模式下都安装了 package。

If I try to import my package with:如果我尝试使用以下命令导入我的 package:

import my_package

I got the well known ModuleNotFoundError .我得到了众所周知的ModuleNotFoundError So I checked the sys.path if the site-packages folder where my package is installed is included.所以我检查了sys.path是否包含安装我的 package 的 site-packages 文件夹。

Okay, So I checked which packages are available with pkg_resources :好的,所以我检查了哪些包可用pkg_resources

{'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'}

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.如您所见,我的 package 列在 future 之后。

I have no clue why python struggles with importing my_package.我不知道为什么 python 难以导入 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 在 pyenv virtualenv 下运行的 python 脚本使用什么 shebang

Python module not found in virtualenv Python 在 virtualenv 中找不到模块

Module installed with PIP in virtualenv not found 找不到在 virtualenv 中安装了 PIP 的模块


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' >>>

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...这个问题似乎是由于项目名称(可以安装的东西)和顶级名称 package (可以导入的东西)之间的轻微混淆,一些区分大小写的问题增加了混淆...

In that particular case the project is indeed named BoxSupDataset (that's what you want to install).在那种特殊情况下,该项目确实被命名为BoxSupDataset (这就是您想要安装的)。 But the actual top level package is boxsupdataset , which is the only thing that matters for the imports:但实际的顶级 packageboxsupdataset ,这是唯一对导入重要的东西:

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).旁白:虽然在某些(不区分大小写)平台(例如Windows ?)上,可能可以导入为import BoxSupDataset ,规范的方法是import boxsupdataset (与 package 或模块完全相同的名称和大小写)。 The (somewhat confusing) details can be found in PEP 235 .可以在PEP 235中找到(有点令人困惑的)细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM