如何导入给定相对路径的Python模块?

例如,如果dirFoo包含Foo.py和dirBar,而dirBar包含Bar.py,我如何将Bar.py导入Foo.py?

下面是一个视觉表示:

dirFoo\
    Foo.py
    dirBar\
        Bar.py

Foo希望包含Bar,但重组文件夹层次结构不是一个选项。


当前回答

最简单的方法是使用sys.path.append()。

然而,您可能也对imp模块感兴趣。它提供对内部导入功能的访问。

# mod_name is the filename without the .py/.pyc extention
py_mod = imp.load_source(mod_name,filename_path) # Loads .py file
py_mod = imp.load_compiled(mod_name,filename_path) # Loads .pyc file 

当您不知道模块的名称时,这可以用于动态加载模块。

我在过去使用过这个来创建应用程序的插件类型接口,用户可以在其中编写带有应用程序特定函数的脚本,然后将脚本放到特定目录中。

此外,这些功能可能有用:

imp.find_module(name[, path])
imp.load_module(name, file, pathname, description)

其他回答

如果您以这种方式构建项目:

src\
  __init__.py
  main.py
  dirFoo\
    __init__.py
    Foo.py
  dirBar\
    __init__.py
    Bar.py

然后,从Foo.py你应该能够做到:

import dirFoo.Foo

Or:

from dirFoo.Foo import FooObject

根据Tom的评论,这确实需要通过site_packages或搜索路径访问src文件夹。此外,正如他所提到的,__init__.py是在您第一次导入该包/目录中的模块时隐式导入的。通常__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使用您的模块,而不是安装在系统上的模块。

警告我真的不知道当当前模块在蛋文件中时会发生什么。它可能也会失败。

称我过于谨慎,但我喜欢让我的更便携,因为假设文件总是在每台计算机上的同一位置是不安全的。就我个人而言,我让代码先查找文件路径。我使用Linux,所以我的看起来像这样:

import os, sys
from subprocess import Popen, PIPE
try:
    path = Popen("find / -name 'file' -type f", shell=True, stdout=PIPE).stdout.read().splitlines()[0]
    if not sys.path.__contains__(path):
        sys.path.append(path)
except IndexError:
    raise RuntimeError("You must have FILE to run this program!")

当然,除非你打算把这些包装在一起。但如果是这样的话,你实际上不需要两个单独的文件。

相对sys.path示例:

# /lib/my_module.py
# /src/test.py


if __name__ == '__main__' and __package__ is None:
    sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../lib')))
import my_module

基于这个答案。

您还可以将子目录添加到Python路径中,以便将其作为普通脚本导入。

import sys
sys.path.insert(0, <path to dirFoo>)
import Bar