简体   繁体   English

复制了有关模块和包内导入的官方Python 3教程,并且仍然收到ImportError和SystemError异常

[英]Replicated the official Python 3 tutorial on Modules and intra-package imports and still receiving ImportError and SystemError exceptions

I followed Python 3 Modules tutorial and I cannot get absolute or relative intra-package imports to work. 我遵循了Python 3 Modules教程 ,但无法使绝对或相对的包内导入正常工作。

Specifically I replicated the project structure from the tutorial. 具体来说,我从教程中复制了项目结构。 The sound folder is located in my home /home/user/ directory. sound文件夹位于我的主目录/home/user/目录中。 All project files (excluding filters/vocoder.py and effects/surround.py ) are empty and have been generated using touch . 所有项目文件(不包括filters/vocoder.pyeffects/surround.py )都是空的,并且是通过touch生成的。

sound/                          Top-level package
      __init__.py               Initialize the sound package
      formats/                  Subpackage for file format conversions
              __init__.py
              wavread.py
              wavwrite.py
              aiffread.py
              aiffwrite.py
              auread.py
              auwrite.py

      effects/                  Subpackage for sound effects
              __init__.py
              echo.py
              surround.py
              reverse.py

      filters/                  Subpackage for filters
              __init__.py
              equalizer.py
              vocoder.py
              karaoke.py

As per instruction filters/vocoder.py contains: 按照指令, filters/vocoder.py包含:

from sound.effects import echo

When this file is executed, it results in an import error: 执行此文件时,将导致导入错误:

user@pc:~/sound$ python filters/vocoder.py
Traceback (most recent call last):
  File "filters/vocoder.py", line 1, in <module>
    from sound.effects import echo
ImportError: No module named 'sound'

user@pc:~/sound$ cd filters/

user@pc:~/sound/filters$ python vocoder.py 
Traceback (most recent call last):
  File "vocoder.py", line 1, in <module>
    from sound.effects import echo
ImportError: No module named 'sound'

Likewise I have executed each of the following lines of code in effects/surround.py separately (I commented the lines out # and rerun the script): 同样,我已经分别在effects/surround.py执行了以下每行代码(我在#注释了这些行并重新运行了脚本):

from . import echo
from .. import formats
from ..filters import equalizer

Which when it is executed results in: 执行时会导致:

user@pc:~/sound$ python effects/surround.py
Traceback (most recent call last):
  File "effects/surround.py", line 1, in <module>
    from . import echo
SystemError: Parent module '' not loaded, cannot perform relative import

user@pc:~/sound$ cd effects/

user@pc:~/sound/effects$ python surround.py
Traceback (most recent call last):
  File "surround.py", line 1, in <module>
    from . import echo
SystemError: Parent module '' not loaded, cannot perform relative import

What am I doing wrong, why can I not get absolute and relative imports to work in my package? 我在做错什么,为什么不能在软件包中使用绝对导入和相对导入?

Below is a script which should help replicate the project: 下面是一个脚本,该脚本应有助于复制项目:

mkdir ~/sound
touch ~/sound/__init__.py

mkdir ~/sound/formats
touch ~/sound/formats/__init__.py
touch ~/sound/folder/wavread.py
touch ~/sound/folder/wavwrite.py
touch ~/sound/folder/aiffread.py
touch ~/sound/folder/aiffwrite.py
touch ~/sound/folder/auread.py
touch ~/sound/folder/auwrite.py

mkdir ~/sound/effects
touch ~/sound/effects/__init__.py
touch ~/sound/folder/echo.py
touch ~/sound/folder/surround.py
touch ~/sound/folder/reverse.py

mkdir ~/sound/filters
touch ~/sound/filters/__init__.py
touch ~/sound/folder/equalizer.py
touch ~/sound/folder/vocoder.py
touch ~/sound/folder/karaoke.py


echo "from sound.effects import echo" >> ~/sound/filters/vocoder.py

echo "from . import echo" >> ~/sound/effects/surround.py
echo "from .. import formats" >> ~/sound/effects/surround.py
echo "from ..filters import equalizer" >> ~/sound/effects/surround.py

I was just looking at the tutorial. 我只是在看教程。 I don't read that from sound.effects import echo is supposed to work as is from filters/vocoders.py . 没有 from sound.effects import echo读取它应该像从filters/vocoders.py from sound.effects import echo中那样工作。 What is says there in the tutorial is: 教程中所说的是:

Users of the package can import individual modules from the package, for example: 包的用户可以从包中导入各个模块,例如:

import sound.effects.echo

So that would be as if a user could reference the sound directory from their current directory. 这样就好像用户可以从其当前目录引用sound目录一样。 Like some of the comments said, in order to do that, the user needs to either be in the same directory as sound or add the sound directory to your PATH variable. 就像一些评论所说的那样,为了做到这一点,用户需要与sound在同一目录中或将sound目录添加到PATH变量中。 Like this: 像这样:

import sys
sys.path.append( <path to sound> )
from sound.effects import echo

See also: Import a module from a relative path 另请参阅: 从相对路径导入模块

Further down the tutorial, they touch on what you are trying to do, which is intra-package references . 在本教程的后续部分,他们触及了您要尝试做的事情,即包内引用 You could use this technique without adding sound to your PATH : 您可以在不向PATH添加sound情况下使用此技术:

For example from your filters/vocoders.py module, this should work to import echo : 例如,从您的filters/vocoders.py模块中,这应该可以导入echo

from ..effects import echo

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

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