【发布时间】:2021-09-09 21:00:43
【问题描述】:
下面是目录结构 -
./remote-control-spotify
├── client
│ ├── menu_item.py
│ └── panel.py
├── config.yaml
├── Pipfile
├── Pipfile.lock
├── pyotify
│ ├── auth
│ │ ├── auth_method.py
│ │ ├── authorization.py
│ │ ├── auth.py
│ │ └── __init__.py
│ └── core
│ ├── album.py
│ ├── artist.py
│ ├── config.py
│ ├── exceptions.py
│ ├── __init__.py
│ ├── parameter.py
│ ├── player.py
│ ├── request.py
│ ├── request_type.py
│ ├── search.py
│ └── search_type.py
├── README.md
├── spotify_auth.py
└── templates
└── index.html
以下是 spotify_auth.py 中的 相对导入 的 Python 代码,该代码会引发错误。
from .pyotify.core.config import read_config
from .pyotify.core.exceptions import BadRequestError
from .pyotify.auth.authorization import Authorization
from .pyotify.auth.auth import get_auth_key
这是尝试python spotify_auth.py时抛出的相对导入错误@
Traceback (most recent call last):
File "/home/prithvi/Desktop/remote-control-spotify/spotify_auth.py", line 10, in <module>
from .pyotify.core.config import read_config
ImportError: attempted relative import with no known parent package
现在,在我尝试如下修改导入之后 -
from pyotify.core.config import read_config
from pyotify.core.exceptions import BadRequestError
from pyotify.auth.authorization import Authorization
from pyotify.auth.auth import get_auth_key
但出现如下错误-
Traceback (most recent call last):
File "/home/prithvi/Desktop/remote-control-spotify/spotify_auth.py", line 10, in <module>
from pyotify.core.config import read_config
File "/home/prithvi/Desktop/remote-control-spotify/pyotify/core/__init__.py", line 1, in <module>
from .config import read_config
File "/home/prithvi/Desktop/remote-control-spotify/pyotify/core/config.py", line 4, in <module>
from pyotify.auth import authMethod
File "/home/prithvi/Desktop/remote-control-spotify/pyotify/auth/__init__.py", line 3, in <module>
from .auth import authenticate
File "/home/prithvi/Desktop/remote-control-spotify/pyotify/auth/auth.py", line 7, in <module>
from pyotify.core import BadRequestError
ImportError: cannot import name 'BadRequestError' from partially initialized module 'pyotify.core' (most likely due to a circular import) (/home/prithvi/Desktop/remote-control-spotify/pyotify/core/__init__.py)
【问题讨论】:
标签: python python-3.x python-import importerror relative-import