【发布时间】:2021-11-13 10:43:39
【问题描述】:
在我向你展示问题之前,我将在这里举一个简单的例子: (请考虑阅读整个问题和重要说明)
-主文件夹:包含: +main.py +Extern 模块文件夹(命名为 ex_modules)
-外部模块文件夹:包含: +模块1.py +module2.py
main.py 需要 module1.py AND module2.py,但是 module1.py 只需要 module2.py
所以我想将module2.py导入module1.py,然后将module1.py导入主文件,我就是这样进行的:
module2.py:
def module2_function1():
return something
def module2_function2():
return something2
def module2_function3():
return something3
module1.py:
from module2 import * #as I said, they are both in the same folder
def module1_function():
module2_function1()
module2_function2()
main.py:
from ex_modules.module1 import *
module1_function() #a module1 function that uses module2 functions
module2_function3() #a module2 function
VS 代码在处理主文件时不显示任何警告 但是当我运行它时会出现这个错误:
ModuleNotFoundError: No module named 'module2'
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
c:\some path xD\Main folder\main.py in <module>
----> 2 from ex_modules.module1 import *
3
4 module1_function()
5 module2_function3()
c:\some path xD\Main folder\ex_modules\module1.py in <module>
1
----> 2 from module2 import * #as I said, they are both in the same folder
3
4 def module1_function():
5 module2_function1()
ModuleNotFoundError: No module named 'module2'
这是因为它导入了 module2(即在 ex_modules 文件夹中),就好像它在带有 main.py 的主文件夹中一样
我尝试将主文件中的两个模块作为“ex_modules.module1 和 ex_modules.module2”导入,是的,它不起作用
问题是: 我的语法错了吗?或者这只是一个 VS 代码错误?
【问题讨论】:
标签: python python-3.x module python-module