简体   繁体   English

无法从本地文件夹导入模块? “没有命名的模块”

[英]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.

相关问题 无法导入本地模块 - Can't import local module 无法从名为“代码”的模块中导入任何功能 - Can't import any functions from module named 'code' Python - 无法从集合中导入Set(“没有名为sets的模块”) - Python - can't import Set from sets (“no module named sets”) 无法导入模块-ImportError:未命名模块 - Can't import modules - ImportError: No module named 导入模块失败:ModuleNotFoundError:没有名为“文件夹”的模块 - Fail to import a Module: ModuleNotFoundError: No module named 'folder' 在 Python 中导入本地模块 - ImportError: Not module named * - Import a local module in Python - ImportError: Not module named * 无法从Python中的另一个文件夹导入模块 - Can't import module from another folder in Python ModuleNotFoundError:没有名为“游戏”的模块 | 如何从完全相同的文件夹中的文件中导入内容 - ModuleNotFoundError: No module named 'game' | how can I import stuff from a file that is in the exact same folder ModuleNotFoundError: No module named - 尝试从另一个文件夹导入文件时 - ModuleNotFoundError: No module named - when attempting to import files from another folder 单元测试中的导入错误:本地库中的“未命名模块……” - Import error with unit tests: “No module named …” from local libary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM