为什么python无法看到scrapy项目的根目录

时间:2019-06-11 22:29:38

标签: python scrapy python-import

我有这个 folder_tree 而且我想从rawlers.py中的runner.py导入类

from scraper.runner import Runner

runner = Runner([{'name': 'my_name', 'urls': ["https://www.exaple.com/"]}])
runner.crawl()

但是我出错了

Traceback (most recent call last):
  File "./scraper/crawlers/actor_crawler.py", line 3, in <module>
    from scraper.runner import Runner
ModuleNotFoundError: No module named 'scraper'

此外,我尝试了相对导入:

from ..runner import Runner

知道了

ValueError: attempted relative import beyond top-level package

1 个答案:

答案 0 :(得分:0)

我发现以下link对理解Python如何导入非常有用。

尽管将模块添加到os.syspath中,但它仍然可以确保导入模块的surefire方法。尝试将以下内容添加到脚本的开头:

import os
import sys
sys.path.append(/path/to/scraper/scraper/runner.py)

from scraper.runner import Runner

runner = Runner([{'name': 'my_name', 'urls': ["https://www.exaple.com/"]}])
runner.crawl()