简体   繁体   English

开发一个新的包并获取ModuleNotFoundError:没有命名的模块

[英]developing a new package and getting ModuleNotFoundError: No module named

I was writing a little application where I wanted to create a module containg a small group of classes, but when I try to import the classes from the main application, I get the error: 我正在编写一个小应用程序,我想创建一个包含一小组类的模块,但是当我尝试从主应用程序导入类时,我收到错误:

 my_project python3 main.py 
1
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    import receivers
  File "/home/mario/Documents/python/my_project/receivers/__init__.py", line 2, in <module>
    from icinga import Icinga
ModuleNotFoundError: No module named 'icinga'

The file in the project are: 项目中的文件是:

├── main.py
└── receivers
    ├── icinga.py
    ├── __init__.py

where main.py 其中main.py

#!/usr/bin/env python
import receivers

icinga = receivers.icinga.Icinga()

the file receivers/icinga.py 文件receivers/icinga.py

class Icinga:

    def __init__(self):
        print("I'm Icinga!")

the file receivers/__init__.py 文件receivers/__init__.py

print('1')
from icinga import Icinga
print('2')

Can someone please tell me what I do wrong? 谁能告诉我我做错了什么?

Thanks in advance 提前致谢

If you just want to import the Icinga class, you can do it as 如果你只想导入Icinga类,你可以这样做

from receivers.icinga import Icinga

If you want to call the import statement on receivers, you should alter the init .py, on 2nd line, to: 如果要在接收器上调用import语句,则应将第二行的init .py更改为:

from .icinga import Icinga

I reproduced your problem here, and was able to solve it like that. 我在这里重现了你的问题,并且能够像那样解决它。

Edit: 编辑:

Doing this second thing (on __init__.py), you would be able to call it on the main.py as: 做第二件事(在__init__.py上),你可以在main.py上调用它:

import receivers
receivers.icinga.Icinga()

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

相关问题 ModuleNotFoundError: 没有名为“的模块”<package> &#39; - ModuleNotFoundError: No module named '<package>' Pipenv 安装本地 package 但收到“ModuleNotFoundError: No module named” - Pipenv installing a local package but getting "ModuleNotFoundError: No module named" ModuleNotFoundError:ubuntu 上没有名为“包”的模块 - ModuleNotFoundError: No module named 'package' on ubuntu 获取 ModuleNotFoundError:没有名为“加密”的模块 - Getting ModuleNotFoundError: No module named 'Crypto' 获取 ModuleNotFoundError:没有名为“azure”的模块 - Getting ModuleNotFoundError: No module named 'azure' 获取 ModuleNotFoundError:没有名为“MySQLdb”的模块 - Getting ModuleNotFoundError: No module named 'MySQLdb' Conda安装包ModuleNotFoundError:没有名为&#39;conda&#39;的模块 - Conda install package ModuleNotFoundError: No module named 'conda' `ModuleNotFoundError: No module named` 为 package 安装诗歌后 - `ModuleNotFoundError: No module named` after poetry install for package 如何解决笔记本中的“ModuleNotFoundError: No module named 'package'”? - How to resolve "ModuleNotFoundError: No module named 'package'" in notebook? ModuleNotFoundError: 没有名为“sqlalchemy”的模块——Conda 包管理器 - ModuleNotFoundError: No module named 'sqlalchemy' -- Conda package manager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM