【问题标题】:Relative imports error. How to import files from higher level directory in the python?相对导入错误。如何从python中的更高级别目录导入文件?
【发布时间】: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


    【解决方案1】:

    试试这个:

    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
    

    【讨论】:

    • 我收到以下错误 - importError: cannot import name 'BadRequestError' from partially initialized module 'pyotify.core' (most likely due to a circular import)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 2018-12-20
    • 2018-01-13
    相关资源
    最近更新 更多