Python 找不到模块
Python cant find module
我的文件结构:
-launch.py
---folder
-----folder
-------__init__py
-------test.py
-------test1.py
launch.py
os.system('python3.6 -m folder.folder.test')
test.py
import test1
test1.py
def test_print():
print("Testing testing 123")
我收到模块未找到错误,因为出于某种原因,python 正在寻找 launch.py
所在目录中的模块。我能够在 [= 中成功导入它29=] 使用 import folder.folder.test1
我只会使用它,但是我正在修改的程序已经有太多使用 import test1
的导入(因为它似乎在 Windows 中工作正常)。
先谢谢你了。
import test1
查找 顶级模块 。如果不明确告诉 Python 查看该包,则不能在同一个包中导入模块。
使用
from . import test1
或
from folder.folder import test1
import test1
仅当目录 folder/folder/
出现在 Python 模块搜索路径中时才有效。任何依赖 import test1
工作的代码只有在直接以 .../folder/folder
作为当前工作目录启动时才会这样做,或者当您将该目录显式添加到 sys.path
时(通过更新该列表来自 Python 代码,或通过设置 PYTHONPATH
环境变量)。
例如,在 folder.folder.test
模块中,您可以使用:
import sys, os
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
在使用 import test1
之前。 我建议不要这样做;修复项目以正确使用打包的命名空间。
我的文件结构:
-launch.py
---folder
-----folder
-------__init__py
-------test.py
-------test1.py
launch.py
os.system('python3.6 -m folder.folder.test')
test.py
import test1
test1.py
def test_print():
print("Testing testing 123")
我收到模块未找到错误,因为出于某种原因,python 正在寻找 launch.py
所在目录中的模块。我能够在 [= 中成功导入它29=] 使用 import folder.folder.test1
我只会使用它,但是我正在修改的程序已经有太多使用 import test1
的导入(因为它似乎在 Windows 中工作正常)。
先谢谢你了。
import test1
查找 顶级模块 。如果不明确告诉 Python 查看该包,则不能在同一个包中导入模块。
使用
from . import test1
或
from folder.folder import test1
import test1
仅当目录 folder/folder/
出现在 Python 模块搜索路径中时才有效。任何依赖 import test1
工作的代码只有在直接以 .../folder/folder
作为当前工作目录启动时才会这样做,或者当您将该目录显式添加到 sys.path
时(通过更新该列表来自 Python 代码,或通过设置 PYTHONPATH
环境变量)。
例如,在 folder.folder.test
模块中,您可以使用:
import sys, os
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
在使用 import test1
之前。 我建议不要这样做;修复项目以正确使用打包的命名空间。