我想从位于当前目录之上的文件中的类继承。

是否可以相对导入该文件?


当前回答

@alex-martelli用pathlib优化的答案:

import pathlib
import sys

_parentdir = pathlib.Path(__file__).parent.parent.resolve()
sys.path.insert(0, str(_parentdir))

import module_in_parent_dir

sys.path.remove(str(_parentdir))

其他回答

从一个恰好比当前目录高一级的目录导入模块:

from .. import module
import sys
sys.path.append("..") # Adds higher directory to python modules path.

为了清晰起见,下面是ThorSummoner的回答的一个三步,有点简单的版本。它不完全是我想要的(我将在底部解释),但它工作正常。

步骤1:创建目录和setup.py

filepath_to/project_name/
    setup.py

在setup.py中,这样写:

import setuptools

setuptools.setup(name='project_name')

步骤2:将该目录作为包安装

在控制台运行以下代码:

python -m pip install --editable filepath_to/project_name

而不是python,你可能需要使用python3或其他东西,这取决于你的python是如何安装的。你也可以用-e代替——editable。

现在,您的目录将或多或少像这样。我不知道鸡蛋是什么东西。

filepath_to/project_name/
    setup.py
    test_3.egg-info/
        dependency_links.txt
        PKG-INFO
        SOURCES.txt
        top_level.txt

这个文件夹被认为是一个python包,即使你在电脑的其他地方写脚本,你也可以从这个父目录中的文件导入。

步骤3。从上面输入

假设您创建了两个文件,一个在项目的主目录中,另一个在子目录中。它看起来是这样的:

filepath_to/project_name/
    top_level_file.py
    subdirectory/
        subfile.py

    setup.py          |
    test_3.egg-info/  |----- Ignore these guys
        ...           |

现在,如果top_level_file.py看起来像这样:

x = 1

然后我可以从subfile。py中导入,或者从电脑上的其他文件中导入。

# subfile.py  OR  some_other_python_file_somewhere_else.py

import random # This is a standard package that can be imported anywhere.
import top_level_file # Now, top_level_file.py works similarly.

print(top_level_file.x)

这与我正在寻找的不同:我希望python有一种单行方式从上面的文件导入。相反,我必须像对待模块一样对待脚本,执行一堆样板文件,并全局安装它,以便整个python安装都能访问它。它是多余的。如果有人有一个更简单的方法,而不涉及上述过程或重要的恶作剧,请告诉我。

@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

现在是2022年,没有一个答案真的对我有用。下面是最后奏效的方法

import sys
sys.path.append('../my_class')
import my_class

我的目录结构:

src ——my_class.py 笔记本电脑 ——mynotebook.ipynb

我从mynotebook.ipynb导入my_class。