How to resolve ModuleNotFoundError when importing a local Python file?

Question:

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:UsersZeroXOneDriveEscritorioejemplos htmlmodulo 4ABP - Ejercicio Individual 4abp_individual_4.py", line 6, in <module>
    from models.reponedor import Reponedor
  File "c:UsersZeroXOneDriveEscritorioejemplos htmlmodulo 4ABP - Ejercicio Individual 4modelsreponedor.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 🙁

Asked By: Leonardo Rodenas

||

Answers:

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.

Answered By: Joshua Shew