使用Scrapy,得到“Error:ImportError:No module namedtestspider.spider.followall测试蜘蛛"

2024-10-01 15:32:45 发布

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

我正在尝试从一个脚本运行Scrapy,并一直遵循教程here。我遇到了一个错误消息,它声明Error: ImportError: No module named testspiders.spiders.followall。我一直在寻找解决办法,但还没有找到匹配的。在

实际上,我正在运行这个python脚本节点.js,它有一个名为python-shell的模块,该模块允许您使用以下简单代码运行python脚本:

var PythonShell = require('python-shell');

PythonShell.run('my_script.py', function (err) {
  if (err) throw err;
  console.log('finished');
});

一字不差,我的代码是从scrapy网站复制的:

^{pr2}$

我的目录结构只是通过添加python目录和文件以及使用python shell的几行代码从express framework进行了修改:

-python-node
    -bin
    -node_modules
    -public
    -python 
        -my_script.py
    -routes
    -views
    -app.js
    -package.json 

注意:如果我进入python目录并运行python my_script.py,并且我得到相同的错误消息:ImportError: No module named testspiders.spiders.followall


Tags: nopy目录脚本消息my错误script
1条回答
网友
1楼 · 发布于 2024-10-01 15:32:45

使用scrapy运行crawler时,scraper root dir(testspiders/的父目录)将自动添加到路径中。使用python运行脚本时,情况并非如此。您有工作目录以及在PATH和PYTHONPATH中定义的任何内容。在

可以使用sys.path检查python中的当前路径

因此,要使import语句与python一起工作,您可以:

  • 将testspider/parent dir添加到路径系统路径追加()(必须在导入testspider之前执行此操作。。。声明)
  • 将parent dir添加到PYTHONPATH系统变量
  • 从testspider/
  • 编辑导入语句(以便它们根据您的路径工作)

相关问题 更多 >

    热门问题