如何配置PyCharm以使用内部dir作为模块根?

2024-05-15 04:19:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下结构(这是scrapy的标准结构):

root/
    .git/
    scrapers/
        scrapers/
            __init__.py
            pipelines.py
            settings.py
        scrapy.cfg
    .gitignore

pipelines.py中,我正在执行以下导入操作:

from scrapers.settings import API_URL, API_KEY

代码运行得很好!然而,PyCharm用消息Cannot find reference 'settings' in 'imported module scrapers'将此标记为错误。它认为外部scrapers/目录是模块根目录。它会错误地认为正确的导入是:

from scrappers.scrapers.settings import API_URL, API_KEY

我尝试将内部scrapers/目录标记为“sources root”,但没有任何帮助。如何将PyCharm配置为假定此代码的根在内部scrapers/目录中


Tags: keyfrompy标记import目录apiurl
1条回答
网友
1楼 · 发布于 2024-05-15 04:19:54

从技术上讲,内部scrapers目录是一个python包,请参见Packages

为了让Pycharm能够定位包,您需要将其父级目录-外部的scrapers目录-配置为Sources Root(即找到包和模块的目录)。从Content root types

These roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports.

相关问题 更多 >

    热门问题