Macbook上的scrapy错误:模块“教程”已经存在

时间:2018-06-08 17:21:15

标签: python macos scrapy

安装scrapy后, 我使用'scrapy startproject tutorial'开始,但它显示如下:

Icelesss-MacBook-Pro:tutorial iceless$ scrapy startproject tutorial
Error: Module 'tutorial' already exists

当我输入import scrapy时,它会显示:

Icelesss-MacBook-Pro:~ iceless$ import scrapy
-bash: import: command not found
Icelesss-MacBook-Pro:~ iceless$ scrapy.item
-bash: scrapy.item: command not found
Icelesss-MacBook-Pro:~ iceless$ create scrapy.Item
-bash: create: command not found
Icelesss-MacBook-Pro:~ iceless$ items.py
-bash: items.py: command not found
Icelesss-MacBook-Pro:~ iceless$ cd tutorial
Icelesss-MacBook-Pro:tutorial iceless$ import scrapy
-bash: import: command not found

1 个答案:

答案 0 :(得分:1)

这应该是一个相对简单的修复。在某些平台上,似乎在安装scrapy时没有创建符号链接,或者没有将cli工具添加到$PATH。需要做的第一件事是找到当前Python和版本的位置:

$ which python
/opt/local/bin/python # your python location may be different

$ python -V
Python 3.6.5 # your version may be different (we need the first two digits)

获取which python命令的前两个路径并将其添加到:

/Library/Frameworks/Python.framework/Versions/3.6/bin/scrapy

所以你最终得到:

/opt/local/Library/Frameworks/Python.framework/Versions/3.6/bin/scrapy

现在最后一步是在~/.bash_profile

中为命令创建别名
alias scrapy="/opt/local/Library/Frameworks/Python.framework/Versions/3.6/bin/scrapy"
  

我已经创建了一个Bash脚本,应该(希望)自动执行此操作   过程:

<强> scrapy_alias.sh

#!/bin/bash

a=$(command -v python)
b=$(python -V | grep -oE "\\d\\.\\d")
c="${a%/bin*}/Library/Frameworks/Python.framework/Versions/${b}/bin/scrapy"

printf "\\n# Scrapy alias\\nalias scrapy=\"${c}\"\\n" | sudo tee -a ~/.bash_profile

在终端中运行脚本,然后将更改发送到~/.bash_profile

$ ./scrapy_alias.sh
$ . ~/.bash_profile

现在你应该可以开始教程了:

$ scrapy startproject tutorial

New Scrapy project 'tutorial', using template directory '/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scrapy/templates/project', created in:
    /Users/Username/Desktop/Scrapy_Tutorial

You can start your first spider with:
    cd tutorial
    scrapy genspider example example.com