简体   繁体   English

ImportError:尝试在没有已知父包的情况下进行相对导入 - Python

[英]ImportError: attempted relative import with no known parent package - Python

My folder structure is (using Pycharm),我的文件夹结构是(使用 Pycharm),

project
-testclass.py
-__test__.py

i am trying to call the test class method from test.py.我正在尝试从 test.py 调用测试类方法。 But getting below exception但低于例外

    from .testclass import Scheduler
ImportError: attempted relative import with no known parent package

test.py:测试.py:

import asyncio
import sys
from .testclass import Scheduler

async def main():
    print("main")
    scheduler = Scheduler()
    scheduler.run()

if __name__ == "__test__":
    main()
    try:
        sys.exit(asyncio.run(main()))
    except:
        print("application exception")

testclass.py:测试类.py:

class Scheduler:
    def __init__(self):
        print("init")

    async def run(self):
        print("run")

How to resolve the correct import & it says relative import!如何解决正确的导入&它说相对导入! How do i get this working in pycharm.我如何让它在 pycharm 中工作。

Edit: folder structure now changed as below as suggested.编辑:文件夹结构现在按照建议更改如下。

project_folder
 testpack (->python package)
  - __init__.py
  - testclass.py
 -__init__.py
 -__test__.py

I've had similar issues and I've created an experimental new import library ultraimport that allows to do file system based imports to solve your issue.我也遇到过类似的问题,我创建了一个实验性的新导入库ultraimport ,它允许进行基于文件系统的导入来解决您的问题。

In your test.py you could then write:在您的 test.py 中,您可以编写:

import ultraimport
Scheduler = ultraimport('__dir__/testclass.py', 'Scheduler')

This will always work, no matter what is your folder structure, no matter if you run the code as a script or module and it does not care about sys.path or your current working directory.这将始终有效,无论您的文件夹结构是什么,无论您是将代码作为脚本还是模块运行并且它不关心 sys.path 或您当前的工作目录。 It's also not necessary to create __init__.py files.也没有必要创建 __init__.py 文件。

One caveat when importing scripts like this is if they contain further relative imports.导入这样的脚本时需要注意的一个问题是它们是否包含进一步的相对导入。 ultraimport has a builtin preprocessor to rewrite subsequent relative imports to ultraimports so they continue to work. ultraimport 有一个内置的预处理器,可以将后续的相对导入重写为 ultraimports,以便它们继续工作。 Though, this is currently somewhat limited as original Python imports are ambiguous and there's only so much you can do about it.不过,目前这在一定程度上受到限制,因为原始 Python 导入是模棱两可的,而且您只能做很多事情。

暂无
暂无

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

相关问题 ImportError:尝试相对导入,没有已知的父包 - ImportError: attempted relative import with no known parent package 导入错误 - 在没有已知父包的情况下尝试相对导入 - ImportError - attempted relative import with no known parent package 导入错误:尝试在没有已知父包的情况下进行相对导入? - ImportError: attempted relative import with no known parent package? ImportError:尝试在没有已知父 package 的情况下进行相对导入:( - ImportError: attempted relative import with no known parent package :( Python/Flask ImportError:尝试在没有已知父包的情况下进行相对导入 - Python/Flask ImportError: attempted relative import with no known parent package Python ImportError:尝试在没有已知父包的情况下进行相对导入 - Python ImportError: attempted relative import with no known parent package Python:“ImportError:在没有已知父包的情况下尝试相对导入” - Python: “ImportError: attempted relative import with no known parent package” Python3 ImportError:尝试在没有已知父项的情况下进行相对导入 package - Python3 ImportError: attempted relative import with no known parent package Python + 子模块:ImportError:尝试在没有已知父 package 的情况下进行相对导入 - Python + Submodules: ImportError: attempted relative import with no known parent package Python - ImportError:尝试在没有已知父 package 的情况下进行相对导入 - Python - ImportError: attempted relative import with no known parent package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM