用于相对导入的 python 包装

发布于 2024-10-06 02:16:33 字数 875 浏览 7 评论 0原文

首先:我很抱歉,我知道有很多关于相对导入的问题,但我只是没有找到解决方案。如果可能的话,我想使用以下目录布局:

myClass/
    __init__.py
    test/
        demo.py
        benchmark.py
        specs.py
    src/
        __init__.py
        myClass.py

现在我的问题是:

  • 如何正确导入包中的测试文件 myClass.py?

  • 假设您将 myClass 作为 libs/myClass 或 include/myClass 中的子模块,您将如何从外部导入包?

到目前为止我还没有找到一个优雅的解决方案。据我了解 Guido 的决定 应该是可能的执行 from ..src import myClass 但这会出错:

ValueError: Attemptedrelative import in non-package

看起来它不将 myClass 视为包。 阅读文档

需要 __init__.py 文件才能使 Python 将目录视为包含包;

我似乎缺少一些指定包脚本位置的内容,我应该使用 .pth 吗?

First off all: I'm sorry, I know there has been lots of question about relative imports, but I just didn't find a solution. If possible I would like to use the following directory layout:

myClass/
    __init__.py
    test/
        demo.py
        benchmark.py
        specs.py
    src/
        __init__.py
        myClass.py

Now my questions are:

  • How do the test files from within the package properly import myClass.py?

  • How would you import the package from outside, assuming you take myClass as submodule in libs/myClass or include/myClass?

So far I couldn't find an elegant solution for this. From what I understand Guido's Decision it should be possible to do from ..src import myClass but this will error:

ValueError: Attempted relative import in non-package

Which looks as it doesn't treat myClass as packages. Reading the docs:

The __init__.py files are required to make Python treat the directories as containing packages;

It seems I'm missing something that specifies where the scripts of the package are, should I use .pth ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

也只是曾经 2024-10-13 02:16:33

ValueError:尝试在非包中进行相对导入

表示您尝试在非包模块中使用相对导入。它的问题在于包含此 from ... import 语句的文件,而不是您尝试导入的文件。

因此,例如,如果您在测试中进行相对导入,则应该使测试成为包的一部分。这意味着

  1. __init__.py 添加到测试/
  2. 从某些外部脚本运行它们,例如nosetests

如果您以 python myClass/test/demo.py 运行某些内容,相对导入将不会也可以工作,因为您运行的是演示模块而不是包。相对导入要求使用它们的模块本身作为包模块导入,from myClass.test.demo import blabla,或者使用相对导入。

ValueError: Attempted relative import in non-package

Means you attempt to use relative import in the module which is not package. Its problem with the file which has this from ... import statement, and not the file which you are trying to import.

So if you are doing relative imports in your tests, for example, you should make your tests to be part of your package. This means

  1. Adding __init__.py to test/
  2. Running them from some outside script, like nosetests

If you run something as python myClass/test/demo.py, relative imports will not work too since you are running demo module not as package. Relative imports require that the module which uses them is being imported itself either as package module, from myClass.test.demo import blabla, or with relative import.

嗼ふ静 2024-10-13 02:16:33

经过昨晚几个小时的搜索,我找到了 python 中相对导入的答案!或者至少是一个简单的解决方案。解决此问题的最佳方法是从另一个模块调用模块。假设您希望 demo.py 导入 myClass.py。在子包根目录的 myClass 文件夹中,它们需要有一个调用其他两个文件的文件。据我所知,工作目录始终被认为是 __main__ 因此,如果您使用 demo.py 脚本测试从 demo.py 的导入,您将收到该错误。举例说明:

文件夹层次结构:

myClass/
    main.py #arbitrary name, can be anything
    test/
        __init__.py
        demo.py
    src/
        __init__.py
        myClass.py

myClass.py:

def randomMaths(x):
    a = x * 2
    y = x * a
    return y

demo.py:

from ..src import myClass

def printer():
    print(myClass.randomMaths(42))

main.py:

import test.demo

demo.printer()

如果在解释器中运行 demo.py ,则会生成错误,但运行 main.py< /code> 不会。虽然有点复杂,但是很有效:D

After hours of searching last night I found the answer to relative imports in python!! Or an easy solution at the very least. The best way to fix this is to have the modules called from another module. So say you want demo.py to import myClass.py. In the myClass folder at the root of the sub-packages they need to have a file that calls the other two. From what I gather the working directory is always considered __main__ so if you test the import from demo.py with the demo.py script, you will receive that error. To illustrate:

Folder hierarchy:

myClass/
    main.py #arbitrary name, can be anything
    test/
        __init__.py
        demo.py
    src/
        __init__.py
        myClass.py

myClass.py:

def randomMaths(x):
    a = x * 2
    y = x * a
    return y

demo.py:

from ..src import myClass

def printer():
    print(myClass.randomMaths(42))

main.py:

import test.demo

demo.printer()

If you run demo.py in the interpreter, you will generate an error, but running main.py will not. It's a little convoluted, but it works :D

子栖 2024-10-13 02:16:33

Intra-package-references 描述了如何 myClass< /code> 来自 test/*。要从外部导入包,您应该在运行导入器应用程序之前将其路径添加到 PYTHONPATH 环境变量中,或者在导入之前将其路径添加到代码中的 sys.path 列表中。

为什么 from ..src import myClass 失败:可能 src 不是 python 包,你无法从那里导入。您应该如上所述将其添加到 python 路径中。

Intra-package-references describes how to myClass from test/*. To import the package from outside, you should add its path to PYTHONPATH environment variable before running the importer application, or to sys.path list in the code before importing it.

Why from ..src import myClass fails: probably, src is not a python package, you cannot import from there. You should add it to python path as described above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文