[英]how to do a relative import from subpackage to top folder workdirectory with 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'
如果要相對導入,則需要將 go 上一級:
from ..a import smallest_circle
我發現了錯誤,它在絕對路徑C:\\Users\\Manuel\\Desktop\\solution\\a.py
我 append 到 sys.path 變量,絕對路徑應該是目錄,而不是到目標文件,因此解決方案將其替換為C:\\Users\\Manuel\\Desktop\\solution
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.