Installing Python module with pip
我正在尝试安装一个名为Scrapy的模块。 我用它安装了它
1 | pip install Scrapy |
我在/usr/local/lib/python2.7/site-packages中看到了'scrapy'文件夹,但是当我尝试在Python程序中导入它时,表示没有该名称的模块。 关于为什么会发生这种情况的任何想法?
编辑:这是pip命令的输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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位版本。
这是
1 | 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 |
并且
1 | #!/usr/local/opt/python/bin/python2.7 from scrapy.cmdline import execute execute() |
您使用的是Homebrew或MacPorts吗?正如@ J.F.Sebastian所说,听起来你在混淆OS X附带的默认python和通过包管理器安装的问题时会出现问题...尝试
如果可行,那么您可能需要考虑将该python可执行文件设为默认值。像
编辑:您可以强制使用pip安装到备用位置。详细信息如下:使用pip将Python包安装到不同的目录中。如果你确实在你的系统上有额外的Python文件夹,也许你可以尝试将scrapy引导到那些,即使只是为了临时解决方案。
你能发布pip命令的输出吗?也许它在某个地方失败了?
另外,你的机器上有两个版本的Python吗? Pip只安装到一个位置,但路径上的Python版本可能不同。
最后,有时给pip的包名称与用于导入的名称不完全相同。检查包的文档。我快速浏览一下,导入应该是小写的:
1 | import scrapy |
当所有其他方法都失败时,您始终可以将环境变量PYTHONPATH(请参阅永久性地向PYTHONPATH添加目录以获取帮助)设置为安装Scrapy的路径。 (等待你没有使用virtualenv - 如果你是请指定我们可以提供帮助,通常也提供操作系统也是一个好主意)
似乎安装在Python路径上的scrapy模块是一个可执行文件,它将为您引导Scrapy项目目录。
scrapy可执行文件中的Python代码如下所示:
1 2 3 4 | #!/usr/bin/env python from scrapy.cmdline import execute execute() |
这是打算从命令行运行而不是导入到您自己的Python项目模块中。
根据项目的文档,使用以下语法运行scrapy可执行文件:
1 | scrapy startproject <your-project-name> |
将引导具有以下目录结构的Scrapy项目:
1 2 3 4 5 6 7 8 9 10 | your-project-name/ scrapy.cfg tutorial/ __init__.py items.py pipelines.py settings.py spiders/ __init__.py ... |
文档中有许多示例演示如何创建和运行自己的蜘蛛,链接提取器等,以及如何操作使用应用程序检索的数据。它们每个都展示了scrapy包中子目录的相应Python导入,以帮助您启动和运行。
希望这会有所帮助。
如果你在Ubuntu上运行:
use the official Ubuntu Packages, which already solve all dependencies for you and are continuously updated with the latest bug fixes.
或者,即使它解决了您的问题,最好在虚拟环境中安装python库,使用virtualenvwrapper保持库分离,尝试检查apt-get安装日志以找出添加的工具,然后删除scrapy python库并在虚拟环境中重新安装它。用pip