如何导入给定相对路径的Python模块?
例如,如果dirFoo包含Foo.py和dirBar,而dirBar包含Bar.py,我如何将Bar.py导入Foo.py?
下面是一个视觉表示:
dirFoo\
Foo.py
dirBar\
Bar.py
Foo希望包含Bar,但重组文件夹层次结构不是一个选项。
如何导入给定相对路径的Python模块?
例如,如果dirFoo包含Foo.py和dirBar,而dirBar包含Bar.py,我如何将Bar.py导入Foo.py?
下面是一个视觉表示:
dirFoo\
Foo.py
dirBar\
Bar.py
Foo希望包含Bar,但重组文件夹层次结构不是一个选项。
当前回答
只需执行简单的操作,即可从其他文件夹导入.py文件。
假设您有一个目录,如:
lib/abc.py
然后在lib文件夹中保留一个空文件
__init__.py
然后使用
from lib.abc import <Your Module name>
在导入模块层次结构的每个文件夹中保留__init__.py文件。
其他回答
假设您的两个目录都是真正的Python包(其中确实包含__init__.py文件),这里有一个相对于脚本位置包含模块的安全解决方案。
我假设您想这样做,因为您需要在脚本中包含一组模块。我在一些产品的生产中使用了这一点,并在许多特殊场景中工作,例如:从另一个目录调用或使用python执行的脚本,而不是打开新的解释器。
import os, sys, inspect
# realpath() will make your script run, even if you symlink it :)
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
if cmd_folder not in sys.path:
sys.path.insert(0, cmd_folder)
# Use this if you want to include modules from a subfolder
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"subfolder")))
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
# Info:
# cmd_folder = os.path.dirname(os.path.abspath(__file__)) # DO NOT USE __file__ !!!
# __file__ fails if the script is called in different ways on Windows.
# __file__ fails if someone does os.chdir() before.
# sys.argv[0] also fails, because it doesn't not always contains the path.
另外,这种方法允许您强制Python使用您的模块,而不是安装在系统上的模块。
警告我真的不知道当当前模块在蛋文件中时会发生什么。它可能也会失败。
确保dirBar具有__init__.py文件——这会将一个目录放入Python包中。
这同样有效,而且比使用sys模块简单得多:
with open("C:/yourpath/foobar.py") as f:
eval(f.read())
在我看来,最好的选择是将__init__.py放在文件夹中,并使用
from dirBar.Bar import *
不建议使用sys.path.append(),因为如果使用与现有python包相同的文件名,可能会出现问题。我还没有测试过,但这将是模棱两可的。
import os
import sys
lib_path = os.path.abspath(os.path.join(__file__, '..', '..', '..', 'lib'))
sys.path.append(lib_path)
import mymodule