[英]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.