python - 如何使用 python 3.75 从子包到顶级文件夹工作目录的相对导入?
问题描述
Project_folder
│
├───Tests
│ │ __init__.py
│ │
│ │
│ ├───features
│ │ smallcircle.feature
│ │
│ ├───steps_tests
│ │ │ test_smallcircle.py
│ │ │ __init__.py
│ __init__.py
│ a.py
我正在尝试从test_smallcircle.py到a.py进行相对导入,如下所示
import sys
import os
sys.path.append(os.path.dirname("C:\\Users\\Manuel\\Desktop\\solution\\a.py"))
from a import smallest_circle # JUST HERE
from pytest_bdd import (
given,
scenario,
then,
when,
)
import pytest_bdd
from functools import partial
但它显示下一个错误
Unable to import 'a'
解决方案
如果你想要一个相对导入,你需要上一个级别:
from ..a import smallest_circle
推荐阅读
- python - 如何将 pytest 中的 cmdopts 传递给一个类?
- html - 阻止 url 转发仅包含应该是可见的
- android - 我想使用 instagram api 从 instagram 获取我的 android 项目的用户名
- python - 将列表从 Python 传递给 C,处理后列表值未更新
- javascript - 反应:未调用异步函数
- mysql - 带有 INNER JOIN 和子查询的 WHERE 子句
- python - Python 覆盖 3rd 方包单个文件
- mysql - 比较日期时间模式 'yMd H:m:s' 选择条件与日期类型
- tensorflow - 为什么我无法在 jupyter notebook 上启动 TensorBoard?
- yarnpkg - 纱线:新包装的永无止境的构建