[英]Imported module fails to import sub-module
I have the following folder-structure我有以下文件夹结构
premier_league/
|_cli_stats/
|__ __init__.py
|__cli_stats.py
|__get_data/
|__get_stats.py
|__get_id.py
|__api_scraper/
|__api_scraper.py
In cli_stats.py
I have the following import:在
cli_stats.py
我有以下导入:
from get_data.get_stats import SeasonStats
In get_stats.py
I have the following import:在
get_stats.py
我有以下导入:
from api_scraper.api_scraper import Football
. from api_scraper.api_scraper import Football
。
When running python cli_stats.py
from the cli_stats
folder the following error occurs.从
cli_stats
文件夹运行python cli_stats.py
时,会发生以下错误。
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'
But when running python get_stats.py
from the get_data
folder, the import is successful.但是从
get_data
文件夹运行python get_stats.py
时,导入成功。 Why does the import not work when running cli_stats.py
from the cli_stats
folder?为什么从
cli_stats
文件夹运行cli_stats.py
时导入不起作用?
You have to adjust the import to a relativ one.您必须将导入调整为相对的。 From the
get_stats.py
you have to step into the directory.您必须从
get_stats.py
进入该目录。 The error is that from api_scraper.api_scraper import Football
is an absolut import.错误是
from api_scraper.api_scraper import Football
是绝对导入。
Try: in get_stats.py尝试:在 get_stats.py
from .api_scraper.api_scraper import Football
(1 dot before the api_scraper) (api_scraper 前 1 个点)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.