python - 从父目录导入 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”修改的技巧可能有效,但我不会亲自使用它们。
解决方案
推荐阅读
- selenium-webdriver - 是什么阻止我在使用自动化测试时打开的浏览器窗口中下载文件?
- ruby - 如何正确使用 Ruby Tk 模块?
- header - 将归档名称(表格的标题)拆分为两个单独的行
- android - Flutter - 使用 android 下载指示器下载文件
- javascript - ReactJS:无法读取未定义的属性“值”
- c - 为什么我的 linux 系统调用在用户空间中返回 -1?
- django - 如何使用 python 请求以嵌套 JSON 格式提交数据
- dax - 如何将过滤后的表传递给 RankX
- javascript - 如何使用 Jquery 客户端或 C# 服务器端验证泛卡号格式
- c# - 使用带有旧 ascii postgresql 数据库的 npgsql entityFrameworkCore 的编码问题