如何解决在导入本地Python文件时出现的ModuleNotFoundError错误?

huangapple go评论78阅读模式
英文:

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

如何解决在导入本地Python文件时出现的ModuleNotFoundError错误?

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 如何解决在导入本地Python文件时出现的ModuleNotFoundError错误?

英文:

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 &quot;c:\Users\ZeroX\OneDrive\Escritorio\ejemplos html\modulo 4\ABP - Ejercicio Individual 4\abp_individual_4.py&quot;, line 6, in &lt;module&gt;
    from models.reponedor import Reponedor
  File &quot;c:\Users\ZeroX\OneDrive\Escritorio\ejemplos html\modulo 4\ABP - Ejercicio Individual 4\models\reponedor.py&quot;, line 2, in &lt;module&gt;
    from persona import Persona
ModuleNotFoundError: No module named &#39;persona&#39;`

This is the project file structure and the classes it contains (if you need to see some class, just ask me):

如何解决在导入本地Python文件时出现的ModuleNotFoundError错误?

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 如何解决在导入本地Python文件时出现的ModuleNotFoundError错误?

答案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.

huangapple
  • 本文由 发表于 2023年5月13日 11:03:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定