使用pip安装Python模块

时间:2013-11-12 20:36:54

标签: python import module scrapy

我正在尝试安装一个名为Scrapy的模块。我用

安装了它
pip install Scrapy

我在/usr/local/lib/python2.7/site-packages中看到'scrapy'文件夹,但是当我尝试在Python程序中导入它时,说没有该名称的模块。关于为什么会发生这种情况的任何想法?

编辑:这是pip命令的输出:

    Downloading/unpacking Scrapy
  Downloading Scrapy-0.20.0.tar.gz (745kB): 745kB downloaded
  Running setup.py egg_info for package Scrapy

    no previously-included directories found matching 'docs/build'
Requirement already satisfied (use --upgrade to upgrade): Twisted>=10.0.0 in /usr/local/lib/python2.7/site-packages (from Scrapy)
Requirement already satisfied (use --upgrade to upgrade): w3lib>=1.2 in /usr/local/lib/python2.7/site-packages (from Scrapy)
Requirement already satisfied (use --upgrade to upgrade): queuelib in /usr/local/lib/python2.7/site-packages (from Scrapy)
Requirement already satisfied (use --upgrade to upgrade): lxml in /usr/local/lib/python2.7/site-packages (from Scrapy)
Requirement already satisfied (use --upgrade to upgrade): pyOpenSSL in /usr/local/lib/python2.7/site-packages (from Scrapy)
Requirement already satisfied (use --upgrade to upgrade): cssselect>=0.9 in /usr/local/lib/python2.7/site-packages (from Scrapy)
Requirement already satisfied (use --upgrade to upgrade): zope.interface>=3.6.0 in /usr/local/lib/python2.7/site-packages (from Twisted>=10.0.0->Scrapy)
Requirement already satisfied (use --upgrade to upgrade): six>=1.4.1 in /usr/local/lib/python2.7/site-packages (from w3lib>=1.2->Scrapy)
Requirement already satisfied (use --upgrade to upgrade): setuptools in /usr/local/lib/python2.7/site-packages/setuptools-1.1.6-py2.7.egg (from zope.interface>=3.6.0->Twisted>=10.0.0->Scrapy)
Installing collected packages: Scrapy
  Running setup.py install for Scrapy
    changing mode of build/scripts-2.7/scrapy from 644 to 755

    no previously-included directories found matching 'docs/build'
    changing mode of /usr/local/bin/scrapy to 755
Successfully installed Scrapy
Cleaning up...

当我运行/ usr / local / bin / scrapy时,我得到了命令和可用命令的用法。我注意到我的/ usr / local / bin中有一个python2.7和python2.7-32,我记得因为小牛的问题而安装32位版本。

以下是python /usr/local/bin/scrapy的输出:

Traceback (most recent call last): File "/usr/local/bin/scrapy", line 3, in <module> from scrapy.cmdline import execute ImportError: No module named scrapy.cmdline 

head /usr/local/bin/scrapy

#!/usr/local/opt/python/bin/python2.7 from scrapy.cmdline import execute execute()

5 个答案:

答案 0 :(得分:11)

您使用的是Homebrew或MacPorts吗?正如@JFSebastian所说,听起来你在混合OS X附带的默认python和通过包管理器安装的问题时会出现问题...尝试/usr/local/opt/python/bin/python2.7 -m scrapy并查看是否抛出{{1 }}

如果可行,那么您可能需要考虑将 python可执行文件设为默认值。类似于ImportError,然后始终使用alias python2.7=/usr/local/opt/python/bin/python2.7而不是默认python2.7。您也可以将python指向python位,但如果由于某种原因需要它,您将无法轻松访问系统(OS X提供的)python。

答案 1 :(得分:2)

编辑:您可以强制将pip安装到备用位置。详细信息如下:Install a Python package into a different directory using pip?。如果你确实在你的系统上有额外的Python文件夹,也许你可以尝试将scrapy引导给那些,即使只是为了临时解决方案。

你可以发布pip命令的输出吗?也许它在某个地方失败了?

另外,你的机器上有两个版本的Python吗? Pip只安装到一个位置,但路径上的Python版本可能不同。

最后,有时给pip的包名称与用于导入的名称不完全相同。检查包的文档。我快速浏览一下,导入应该是小写的:

import scrapy

答案 2 :(得分:1)

当其他所有方法都失败时,您始终可以将环境变量PYTHONPATH(请参阅Permanently add a directory to PYTHONPATH获取帮助)设置为安装Scrapy的路径。 (等待你不使用virtualenv - 如果你是请指定我们可以提供帮助,通常也提供操作系统也是一个好主意)

答案 3 :(得分:0)

如果你在Ubuntu上运行:

  

使用官方Ubuntu Packages,它已经为您解决了所有依赖项,并不断更新最新的错误修复。

可选地,即使它解决了您的问题,最好在虚拟环境中安装python库,使用virtualenvwrapper保持库分离,尝试检查apt-get安装日志以找出添加的工具,然后删除scrapy python库并在虚拟环境中重新安装它。使用pip

答案 4 :(得分:0)

安装在Python路径上的scrapy模块似乎是一个可执行文件,它将为您引导Scrapy项目目录。

scrapy executable中的Python代码如下所示:

   #!/usr/bin/env python

   from scrapy.cmdline import execute
   execute()

该命令旨在从命令行运行,而不是导入到您自己的Python项目模块中。

根据documentation for the project,使用以下语法运行scrapy可执行文件:

scrapy startproject <your-project-name>

将引导具有以下目录结构的Scrapy项目:

your-project-name/
scrapy.cfg
tutorial/
    __init__.py
    items.py
    pipelines.py
    settings.py
    spiders/
        __init__.py
        ...

文档中有许多示例演示了如何创建和运行自己的蜘蛛,链接提取器等,以及如何操作使用应用程序检索的数据。它们每个都展示了scrapy包中子目录的相应Python导入,以帮助您启动和运行。

希望这会有所帮助。