复制了关于模块和包内导入的官方 Python 3 教程,并且仍然收到 ImportError 和 SystemError 异常
Replicated the official Python 3 tutorial on Modules and intra-package imports and still receiving ImportError and SystemError exceptions
我遵循了 Python 3 Modules tutorial,但我无法使绝对或相对包内导入工作。
具体来说,我从教程中复制了项目结构。 sound
文件夹位于我的 /home/user/
主目录中。所有项目文件(不包括 filters/vocoder.py
和 effects/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
根据说明 filters/vocoder.py
包含:
from sound.effects import echo
执行此文件时,会导致导入错误:
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'
同样,我在 effects/surround.py
中分别执行了以下每一行代码(我将这些行注释掉 #
并重新运行脚本):
from . import echo
from .. import formats
from ..filters import equalizer
执行结果为:
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
我做错了什么,为什么我不能在我的包中使用绝对和相对导入?
下面是一个有助于复制项目的脚本:
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
我只是在看教程。我没有读到from sound.effects import echo
应该像filters/vocoders.py
那样工作。教程中说的是:
Users of the package can import individual modules from the package,
for example:
import sound.effects.echo
这就好像用户可以从他们的当前目录引用 sound
目录一样。就像一些评论所说的那样,为了做到这一点,用户需要与 sound
位于同一目录中,或者将 sound
目录添加到您的 PATH
变量中。像这样:
import sys
sys.path.append( <path to sound> )
from sound.effects import echo
另请参阅:Import a module from a relative path
在教程的后面,他们谈到了您正在尝试做的事情,即 intra-package references。您可以在不将 sound
添加到 PATH
:
的情况下使用此技术
例如,从您的 filters/vocoders.py
模块,这应该可以导入 echo
:
from ..effects import echo
我遵循了 Python 3 Modules tutorial,但我无法使绝对或相对包内导入工作。
具体来说,我从教程中复制了项目结构。 sound
文件夹位于我的 /home/user/
主目录中。所有项目文件(不包括 filters/vocoder.py
和 effects/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
根据说明 filters/vocoder.py
包含:
from sound.effects import echo
执行此文件时,会导致导入错误:
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'
同样,我在 effects/surround.py
中分别执行了以下每一行代码(我将这些行注释掉 #
并重新运行脚本):
from . import echo
from .. import formats
from ..filters import equalizer
执行结果为:
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
我做错了什么,为什么我不能在我的包中使用绝对和相对导入?
下面是一个有助于复制项目的脚本:
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
我只是在看教程。我没有读到from sound.effects import echo
应该像filters/vocoders.py
那样工作。教程中说的是:
Users of the package can import individual modules from the package, for example:
import sound.effects.echo
这就好像用户可以从他们的当前目录引用 sound
目录一样。就像一些评论所说的那样,为了做到这一点,用户需要与 sound
位于同一目录中,或者将 sound
目录添加到您的 PATH
变量中。像这样:
import sys
sys.path.append( <path to sound> )
from sound.effects import echo
另请参阅:Import a module from a relative path
在教程的后面,他们谈到了您正在尝试做的事情,即 intra-package references。您可以在不将 sound
添加到 PATH
:
例如,从您的 filters/vocoders.py
模块,这应该可以导入 echo
:
from ..effects import echo