简体   繁体   English

无法修复:尝试相对导入超出顶级 package

[英]cannot fix: attempted relative import beyond top-level package

I have read many of the related posts about this but cannot figure out how to fix the following.我已经阅读了许多关于此的相关帖子,但无法弄清楚如何解决以下问题。

Here is a link to a zip of this super simple example.这是这个超级简单示例的 zip 的链接。 https://1drv.ms/u/s?Auri_uzbtK_to4AGo1yammz7PRrv6w?e=QOtQeL https://1drv.ms/u/s?Auri_uzbtK_to4AGo1yammz7PRrv6w?e=QOtQeL

I am using VS Code.我正在使用 VS 代码。 I have the following python program structure shown below.我有如下所示的 python 程序结构。

I am trying to run and debug "run_tests.py".我正在尝试运行和调试“run_tests.py”。 When I do, VS Code throws the error "attempted relative import beyond top-level package"当我这样做时,VS Code 抛出错误“尝试相对导入超出顶级包”

The line in question is in file helper.py "from...inpatient.model import Model"有问题的行在文件 helper.py“from...inpatient.model import Model”中

I am stuck on how to fix this.我坚持如何解决这个问题。 Any help would be greatly appreciated.任何帮助将不胜感激。

MyApp/
    inpatient/
        __init__.py
        model.py
    tests/
        __init__.py
        run_tests.py
        utils/
            __init__.py
            helper.py
    venv

run_tests.py运行测试.py

import unittest
from pathlib import Path
import os
import sys
from utills.helper import some_method

class BaseFludlTestCase(TestCase):
    def setUp(self) -> None:

if __name__ == "__main__":
    unittest.main() 

helper.py助手.py

from ...inpatient.model import Model

def some_method():
    model = Model()

Ultimately, my misunderstanding was that "import" in python were relatively to where the main program was executed.最终,我的误解是python中的“导入”是相对于主程序执行的位置。

with this new knowledge, I moved my running code to the root and now things operate as a expect.有了这些新知识,我将正在运行的代码移到了根目录,现在一切都按预期运行了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM