pythonpython-importpython-modulepython-class

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):

structure of folders

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 :(


Solution

  • 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.