python - 导入的模块导入子模块失败
问题描述
我有以下文件夹结构
premier_league/
|_cli_stats/
|__ __init__.py
|__cli_stats.py
|__get_data/
|__get_stats.py
|__get_id.py
|__api_scraper/
|__api_scraper.py
在cli_stats.py
我有以下导入:
from get_data.get_stats import SeasonStats
在get_stats.py
我有以下导入:
from api_scraper.api_scraper import Football
.
python cli_stats.py
从文件夹运行cli_stats
时会出现以下错误。
File "cli_stats.py", line 36, in <module>
from get_data.get_stats import SeasonStats
File "/Users/name/Desktop/Projekt/premier_league_api/cli_stats/get_data/get_stats.py", line 12, in <module>
from api_scraper.api_scraper import Football
ModuleNotFoundError: No module named 'api_scraper'
但是python get_stats.py
从get_data
文件夹运行时,导入成功。cli_stats.py
为什么从文件夹运行时导入不起作用cli_stats
?
解决方案
您必须将导入调整为相对的。从get_stats.py
你必须步入目录。错误是from api_scraper.api_scraper import Football
绝对导入。
尝试:在 get_stats.py
from .api_scraper.api_scraper import Football
(api_scraper 前 1 个点)
推荐阅读
- vue.js - Vuetify,工具提示:“on”和“attrs”有什么用?
- html - 向下滚动页面时,如何在其上方隐藏 div 的粘性/固定导航栏?
- c# - FirstOrDefault 如何测试平等?
- javascript - 如何使用 JavaScript 突出显示包含数组中关键字的所有链接
- javascript - CkEditor 5:工具栏没有响应
- python - Python - hashlib 的“哈希”对象是否包含对原始输入值的任何引用?
- flutter - Perform async operation on list in dart
- python - Python/PySpark spacy 返回数组而不是单个字符串
- java - 我不能使用在代码块中声明的变量调用方法
- java - 修改java代码以从Linux命令行读取输入文件