简体   繁体   English

在父文件夹或祖父文件夹中引用自定义模块会导致 Python 中出现“ModuleNotFoundError: No module named”

[英]Referencing a custom module in a parent or grandparent folder results in 'ModuleNotFoundError: No module named' in Python

Explanation解释

Use Case : Reference a top-level <module_name>.py module from a low-level (subfolder) <script_name>.py用例:从低级(子文件夹)<script_name>.py 引用顶级 <module_name>.py 模块

I was under the impression one could import python scripts and their respective variables into other scripts within a project directory as long as they all had a main directory in common.我的印象是可以将 python 脚本及其各自的变量导入到项目目录中的其他脚本中,只要它们都有一个共同的主目录。

.
└── spam/                    Main directory (git repo)
    ├── spam/                Main project directory
    │   ├── __init__.py
    │   ├── ham/             Directory within main project spam/
    │   │   ├── __init__.py
    │   │   └── eggs.py      Script within sub directory ham/
    │   └── config.py        Config with contents eggs.py needs
    └── README.md

(Note: Yes, the main directory and the main project directory have the same name.) (注:是的,主目录和主项目目录同名。)

For example, I thought the above structure would allow this behavior: use contents from config with eggs.py例如,我认为上面的结构会允许这种行为:使用来自配置的内容和 egg.py

#! /spam/spam/ham/eggs.py

from spam import config

Well, not quite...嗯,不完全...

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'spam'

So I took one step back.于是我退了一步。

#! /spam/spam/ham/eggs.py

import spam
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'spam'

I was wrong.我错了。

I am not so much looking for a solution at this point (since I found a few different ones including here and here (though the explanations did not really get through my thick skull). Would anyone like to explain it to me like I'm a 5th grader?在这一点上,我并不是在寻找解决方案(因为我发现了一些不同的解决方案,包括这里这里(尽管解释并没有真正通过我厚厚的头骨)。有人愿意像我一样向我解释吗?五年级学生?

I will continue to collect what I consider to be valuable resources for finding the answers to my question.我将继续收集我认为有价值的资源来寻找我的问题的答案。 I appreciate all contributions that help clear up this picture.我感谢所有有助于澄清这一情况的贡献。

Ongoing Discoveries正在进行的发现

Exploratory Testing探索性测试

../垃圾邮件

Imports work for the above image.导入适用于上述图像。

# ../spam
# Direct import statements.
import spam
import config

# Indirect to get ham.
import spam.ham
from spam import ham

# Indirect to get eggs.
import spam.ham.eggs
from spam.ham import eggs

../垃圾邮件/垃圾邮件

Imports work for the above image.导入适用于上述图像。

# ../spam/spam
# Direct import statements.
import ham

# Indirect to get eggs.
import ham.eggs
from ham import eggs

../垃圾邮件/垃圾邮件/火腿

Imports work for the above image.导入适用于上述图像。

# ../spam/spam/ham
# Direct import statements.
import eggs

Observations观察

  • when doing import <module_name> , the current directory is checked but "backwards blind"执行import <module_name>时,会检查当前目录但“向后盲”
  • namespaces play a key role here命名空间在这里起着关键作用
  • relative imports are only for use within module files (ie, not within interactive interpreters)相对导入仅用于模块文件中(即,不在交互式解释器中)

Resources for Better Understanding更好理解的资源

*Solutions I test and find acceptable. *我测试并认为可以接受的解决方案。


Tools and Versions工具和版本

Python 3.8.6 Python 3.8.6

A python module is not a folder, it's a python file (eg iamapythonfile.py) python 模块不是文件夹,它是 python 文件(例如 iamapythonfile.py)

Then your "spam" folder could not be resolved as a python module然后您的“垃圾邮件”文件夹无法解析为 python 模块

you may use你可以使用

 import modulename.py

#or

from modulename.py import yourfunction

Then you should care about the path (absolute or relative )那么你应该关心路径(绝对或相对

More informations at: https://www.learnpython.org/en/Modules_and_Packages#:~:text=Modules%20in%20Python%20are%20simply,or%20variables%20defined%20and%20implemented .更多信息请访问: https://www.learnpython.org/en/Modules_and_Packages#:~:text=Modules%20in%20Python%20are%20simply,or%20variables%20defined%20and%20implemented

EDIT : i've just found this https://realpython.com/absolute-vs-relative-python-imports/#:~:text=You%20need%20to%20have%20a,init__.py%20file ).编辑:我刚刚发现了这个https://realpython.com/absolute-vs-relative-python-imports/#:~:text=You%20need%20to%20have%20a,init__.py%20file )。 , it's well explained and clear. ,解释得很好,很清楚。 Enjoy.享受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM