英文:
How to resolve ModuleNotFoundError when importing a local Python file?
问题
I am studying python. I'm trying to do a simple exercise from the course I'm studying. I tried to separate the classes into different files to make it easier to keep track of the inheritance and to be able to update the program in the future with new features. The problem is that when I try to instantiate the objects in a different file where I have to import the classes, it always throws me this error:
Traceback (most recent call last):
File "c:\Users\ZeroX\OneDrive\Escritorio\ejemplos html\modulo 4\ABP - Ejercicio Individual 4\abp_individual_4.py", line 6, in <module>
from models.reponedor import Reponedor
File "c:\Users\ZeroX\OneDrive\Escritorio\ejemplos html\modulo 4\ABP - Ejercicio Individual 4\models\reponedor.py", line 2, in <module>
from persona import Persona
ModuleNotFoundError: No module named 'persona'
This is the project file structure and the classes it contains (if you need to see some class, just ask me):
I am trying to instantiate and run the methods created in different classes and files in a single place within the project.
What could be the problem? I've tried imports and fixing classes with inheritance, but nothing
英文:
I am studying python. I'm trying to do a simple exercise from the course I'm studying. I tried to separate the classes into different files to make it easier to keep track of the inheritance and to be able to update the program in the future with new features. The problem is that when I try to instantiate the objects in a different file where I have to import the classes, it always throws me this error:
`Traceback (most recent call last):
File "c:\Users\ZeroX\OneDrive\Escritorio\ejemplos html\modulo 4\ABP - Ejercicio Individual 4\abp_individual_4.py", line 6, in <module>
from models.reponedor import Reponedor
File "c:\Users\ZeroX\OneDrive\Escritorio\ejemplos html\modulo 4\ABP - Ejercicio Individual 4\models\reponedor.py", line 2, in <module>
from persona import Persona
ModuleNotFoundError: No module named 'persona'`
This is the project file structure and the classes it contains (if you need to see some class, just ask me):
I am trying to instantiate and run the methods created in different classes and files in a single place within the project.
What could be the problem? I've tried imports and fixing classes with inheritance, but nothing
答案1
得分: 1
Your question has most likely already been answered on this site. This answer to this question is something that I found quite helpful.
In short, changing your the import line to from modules.persona import Persona
will probably do the trick in this case. However, in general, it would be beneficial to learn to group your Python files into packages if you plan on using them in other projects. Here is the Python documentation on packages.
英文:
Your question has most likely already been answered on this site. This answer to this question is something that I found quite helpful.
In short, changing your the import line to from modules.persona import Persona
will probably do the trick in this case. However, in general, it would be beneficial to learn to group your Python files into packages if you plan on using them in other projects. Here is the Python documentation on packages.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论