我想从位于当前目录之上的文件中的类继承。
是否可以相对导入该文件?
我想从位于当前目录之上的文件中的类继承。
是否可以相对导入该文件?
当前回答
您可以使用sys.path.append()方法将包含包的目录添加到搜索模块的路径列表中。例如,如果包位于当前目录以上两个目录,您可以使用以下代码:
import sys
sys.path.append("../../")
如果包位于当前目录上方的一个目录,你可以使用下面的代码:
import sys
sys.path.append("..")
其他回答
从一个恰好比当前目录高一级的目录导入模块:
from .. import module
Python是一个模块化的系统
Python不依赖于文件系统
要可靠地加载python代码,请将该代码放在一个模块中,并将该模块安装在python的库中。
安装的模块总是可以使用import <name>从顶级命名空间加载
这里有一个很好的示例项目:https://github.com/pypa/sampleproject
基本上,你可以有一个这样的目录结构:
the_foo_project/
setup.py
bar.py # `import bar`
foo/
__init__.py # `import foo`
baz.py # `import foo.baz`
faz/ # `import foo.faz`
__init__.py
daz.py # `import foo.faz.daz` ... etc.
.
确保在setup.py中声明setuptools.setup(),
官方示例:https://github.com/pypa/sampleproject/blob/master/setup.py
在我们的例子中,我们可能想要导出bar.py和foo/__init__.py,我的简单例子:
setup . py
#!/usr/bin/env python3
import setuptools
setuptools.setup(
...
py_modules=['bar'],
packages=['foo'],
...
entry_points={},
# Note, any changes to your setup.py, like adding to `packages`, or
# changing `entry_points` will require the module to be reinstalled;
# `python3 -m pip install --upgrade --editable ./the_foo_project
)
.
现在我们可以将模块安装到python库中; 使用pip,你可以在编辑模式下将_foo_project安装到python库中, 这样我们就能实时处理了
python3 -m pip install --editable=./the_foo_project
# if you get a permission error, you can always use
# `pip ... --user` to install in your user python library
.
现在,我们可以从任何python上下文中加载共享的py_modules和包
foo_script.py
#!/usr/bin/env python3
import bar
import foo
print(dir(bar))
print(dir(foo))
import sys
sys.path.append("..") # Adds higher directory to python modules path.
@gimel's answer is correct if you can guarantee the package hierarchy he mentions. If you can't -- if your real need is as you expressed it, exclusively tied to directories and without any necessary relationship to packaging -- then you need to work on __file__ to find out the parent directory (a couple of os.path.dirname calls will do;-), then (if that directory is not already on sys.path) prepend temporarily insert said dir at the very start of sys.path, __import__, remove said dir again -- messy work indeed, but, "when you must, you must" (and Pyhon strives to never stop the programmer from doing what must be done -- just like the ISO C standard says in the "Spirit of C" section in its preface!-).
下面是一个可能对你有用的例子:
import sys
import os.path
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
import module_in_parent_dir
从…import subpkg2
根据Python文档:当在包层次结构中,使用两个点,就像import语句doc说的那样:
When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using leading dots in the specified module or package after from you can specify how high to traverse up the current package hierarchy without specifying exact names. One leading dot means the current package where the module making the import exists. Two dots means up one package level. Three dots is up two levels, etc. So if you execute from . import mod from a module in the pkg package then you will end up importing pkg.mod. If you execute from ..subpkg2 import mod from within pkg.subpkg1 you will import pkg.subpkg2.mod. The specification for relative imports is contained within PEP 328.
PEP 328涉及绝对/相对进口。