[英]Can't import module from local folder? "No module named"
I don't find the mistake in the code nor in the call when trying an import test.尝试导入测试时,我在代码和调用中都没有发现错误。
My directory structure is the following: (from /home/user1/python_test/
)我的目录结构如下:(来自
/home/user1/python_test/
)
main.py
Aux/lib1.py
Aux/__init__.py
main.py:主要.py:
from Aux.lib1 import fun_A
if __name__ == "__main__":
fun_A()
print("all done")
Aux/lib1.py:辅助/lib1.py:
def fun_A():
print("A function called")
I'm executing from terminal ( python main.py
from directory where main.py is), maybe is there a need to set pythonpath?我正在从终端执行(来自
python main.py
所在目录的python main.py
),也许需要设置 pythonpath? I don't recall the need before, I've made some python programs like these some time ago (2/3 years)我以前不记得需要了,我前段时间(2/3 年)制作了一些这样的 python 程序
I have also tried from .Aux.lib1 import fun_A
instead of from Aux.lib1 import fun_A
but nothing works.我也试过
from .Aux.lib1 import fun_A
而不是from Aux.lib1 import fun_A
但没有任何效果。 The error is:错误是:
File "main.py", line 1, in <module>
from Aux.lib1 import fun_A
ImportError: No module named Aux.lib1
Create a blank file with name __init__.py
under the folder Aux.在 Aux 文件夹下创建一个名为
__init__.py
的空白文件。 Without this file a directory can not be a module.没有这个文件,目录就不能成为模块。
Oh my, It was a super silly mistake.哦,天哪,这是一个超级愚蠢的错误。 I'm so used to execute inside a virtual environment that I was using python2 by mistake using
python main.py
.我习惯于在虚拟环境中执行,以至于我错误地使用
python main.py
使用了 python2 。 Executing python3 main.py
worked for me.执行
python3 main.py
对我python3 main.py
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.