简体   繁体   English

如何解决“ ValueError:尝试相对顶级包进行相对导入”

[英]How to resolve “ValueError: attempted relative import beyond top-level package”

I have the following problem with my project, help me please! 我的项目存在以下问题,请帮帮我! Here is the structure of my package: 这是我的包裹的结构:

/pkg

/pkg/__init__.py
/pkg/sub1/__init__.py
/pkg/sub2/__init__.py

/pkg/sub1/foo1.py
/pkg/sub2/foo2.py

Here is implementation of foo1.py: 这是foo1.py的实现:

from ..sub2 import foo2

def f():
    print("Hello!")

When I run foo1 I get error: ValueError: attempted relative import beyond top-level package . 当我运行foo1时,出现错误: ValueError:尝试了相对于顶级包的相对导入

I can solve it doing the following adjustment: 我可以通过以下调整来解决它:

import sys
import os
sys.path.append(os.path.abspath(os.path.pardir))

from sub2 import foo2
def f():
    print("Hello!")

But I wonder if there is a way to do it without importing sys and appending parent directory in it. 但是我想知道是否有一种方法可以不导入sys并在其中附加父目录。

I heard that if I had .py file '/pkg/start.py' for example which called my foo1 module, then two dots would work. 我听说,例如,如果我有.py文件'/pkg/start.py',它调用了我的foo1模块,那么两个点就可以了。 However, is there any way to call foo2 from foo1 directly? 但是,有什么方法可以直接从foo1调用foo2吗?

It seems to me, that without adding pkg to my PATH it is impossible to import modules from sub2 in sub1. 在我看来,如果不将pkg添加到我的PATH中,就不可能从sub1的sub2导入模块。 Here is explanation why: 这是为什么的解释:

Relative imports use a module's name attribute to determine that module's position in the package hierarchy. 相对导入使用模块的名称属性来确定该模块在包层次结构中的位置。 If the module's name does not contain any package information (eg it is set to ' main ') then relative imports are resolved as if the module were a top level module, regardless of where the module is actually located on the file system. 如果模块的名称不包含任何软件包信息(例如,将其设置为' main '),则相对导入的解析就好像该模块是顶级模块一样,无论该模块实际位于文件系统上的哪个位置。

Here is official python web site, where it is explained 这是python官方网站,在这里进行了解释

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

相关问题 ValueError:尝试在顶级包python之外进行相对导入 - ValueError: attempted relative import beyond top-level package python ValueError:尝试相对导入超出顶级包(Scrapy) - ValueError: attempted relative import beyond top-level package (Scrapy) ValueError:尝试运行包时尝试相对导入超出顶级包 - ValueError: attempted relative import beyond top-level package while try to run package ValueError:尝试相对导入超出顶级 package - 导入文件时 - ValueError: attempted relative import beyond top-level package - when import file 从celery导入current_app给出ValueError:尝试了相对顶级包之外的相对导入 - importing current_app from celery is gives ValueError: attempted relative import beyond top-level package 无法解决“ ValueError:尝试相对顶级包进行相对导入” - Cannot Solve “ValueError: attempted relative import beyond top-level package” 尽管PyCharm具有自动填充功能,但ValueError尝试了相对导入而不是顶级软件包 - ValueError attempted relative import beyond top-level package, despite PyCharm autofilling suggestions Python 错误:尝试相对导入超出顶级 package - Python error: attempted relative import beyond top-level package “尝试相对导入超出顶级包” - “Attempted relative import beyond top-level package” ImportEror:尝试相对导入超出顶级包 - ImportEror: attempted relative import beyond top-level package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM