简体   繁体   English

来自同一包的python相对导入失败

[英]python relative import from same package fails

I am working on a project involving the popular aiortc/aioquic github package.我正在从事一个涉及流行的aiortc/aioquic github 包的项目。 Located at src/aioquic/buffer.py there is an import statement:位于src/aioquic/buffer.py有一个导入语句:

from ._buffer import Buffer, BufferReadError, BufferWriteError  # noqa

This line is meant to import a C/stub module _buffer.c / _buffer.pyi .此行用于导入 C/stub 模块 _buffer.c / _buffer.pyi 。 When used from the root directory to run examples found in the examples directory this import line will work, but when trying to run in a Docker container from the same directory this import fails and when importing from the /src directory the import fails.当从根目录运行在示例目录中找到的示例时,此导入行将起作用,但是当尝试从同一目录在 Docker 容器中运行时,此导入失败,并且从 /src 目录导入时,导入失败。 I have tried reading several other questions on python relative imports but none seem to make sense to me.我已经尝试阅读有关 python 相对导入的其他几个问题,但对我来说似乎没有任何意义。

To reproduce the issue:要重现该问题:

  1. open python in the src directory在src目录下打开python
  2. from aioquic import about (works)从 aioquic 进口关于(作品)
  3. from aioquic import buffer (fail)从 aioquic 导入缓冲区(失败)
from aioquic import buffer
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/aioquic/src/aioquic/buffer.py", line 1, in <module>
    from ._buffer import Buffer, BufferReadError, BufferWriteError  # noqa
ModuleNotFoundError: No module named 'aioquic._buffer'

I don't understand why the examples http3_client and http3_server work but when trying to import it directly it fails.我不明白为什么示例 http3_client 和 http3_server 可以工作,但是当尝试直接导入它时它会失败。 I thought I understood roughly how to use python imports but apparently not.我以为我大致了解如何使用 python 导入,但显然不是。 What would cause the same line to work when running from one directoy but fail when running from another directory?什么会导致从一个目录运行时同一行工作但从另一个目录运行时失败? Is it something magical with the PYTHONPATH? PYTHONPATH 有什么神奇之处吗? I'm hoping someone smarter than I am can explain why this is failing.我希望比我更聪明的人能解释为什么这会失败。 [EDIT and most importantly how to fix this so the import will work nomatter where the user runs python from. [编辑,最重要的是如何解决这个问题,以便无论用户从哪里运行 python,导入都可以正常工作。 I have tried several different combinations but none seem to work.] Thanks in advance.我尝试了几种不同的组合,但似乎都不起作用。]提前致谢。

I figured out what was wrong.我发现出了什么问题。 The stub/C module had to be built into a .pyd file before it could imported.存根/C 模块必须先构建到 .pyd 文件中,然后才能导入。 This was done by这是由

pip install -e .

on the parent directory在父目录上

Is it something magical with the PYTHONPATH? PYTHONPATH 有什么神奇之处吗?

Yes.是的。

Typically .通常. dot (current working directory) will appear in that exported env var, so where you started matters.点(当前工作目录)将出现在导出的 env var 中,所以你从哪里开始很重要。

Take care to ensure you're using the same env var setting, and the same pwd , during both the interactive and the Docker runs.请注意确保在交互和 Docker 运行期间使用相同的 env var 设置和相同的pwd

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

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