首页 > 解决方案 > 从父目录导入 Python 模块/类对象?

问题描述

我有以下目录结构:

folder1
      \folder2
            file2.py
      \folder3
            file3.py

在 file2.py 中有一个我会在 file3.py 中使用的类

问题是,该from xx import yy命令不起作用,因为 file2.py 不在 file3.py 的子目录中

以下命令from ..folder2.file2 import myclass_in_file2也不起作用(Python 3.8)。

有没有简单明了的方法来做到这一点?

带有“sys-path”和“exec”修改的技巧可能有效,但我不会亲自使用它们。

标签: pythonpython-3.x

解决方案


推荐阅读