ubuntu18.04 搭建scrapy環境(連環踩坑+解決辦法)


---恢復內容開始---

預期需求:

打算搭建scrapy環境,基於python3.x的

 

環境描述:

ubuntu18.04自帶了python3.6,打算在虛擬環境vlenv中跑scrapy,裝好虛擬環境后,直接pip3 install scrapy命令走起,然后,跑錯了

@ubuntu:~$ pip3 install scrapy
Collecting scrapy
Downloading https://files.pythonhosted.org/packages/29/4b/585e8e111ffb01466c59281f34febb13ad1a95d7fb3919fd57c33fc732a5/Scrapy-1.7.3-py2.py3-none-any.whl (234kB)
100% |████████████████████████████████| 235kB 23kB/s
Collecting pyOpenSSL (from scrapy)
Downloading https://files.pythonhosted.org/packages/01/c8/ceb170d81bd3941cbeb9940fc6cc2ef2ca4288d0ca8929ea4db5905d904d/pyOpenSSL-19.0.0-py2.py3-none-any.whl (53kB)
100% |████████████████████████████████| 61kB 18kB/s
Collecting Twisted>=13.1.0; python_version != "3.4" (from scrapy)

。。。

Downloading https://files.pythonhosted.org/packages/be/70/e5ea8afd6d08a4b99ebfc77bd1845248d56cfcf43d11f9dc324b9580a35c/pyasn1_modules-0.2.6-py2.py3-none-any.whl (95kB)
100% |████████████████████████████████| 102kB 29kB/s
Collecting cffi!=1.11.3,>=1.8 (from cryptography>=2.3->pyOpenSSL->scrapy)
Could not find a version that satisfies the requirement cffi!=1.11.3,>=1.8 (from cryptography>=2.3->pyOpenSSL->scrapy) (from versions: )
No matching distribution found for cffi!=1.11.3,>=1.8 (from cryptography>=2.3->pyOpenSSL->scrapy)

 

安裝失敗!似乎python版本不對?

@ubuntu:~$ which python
/usr/bin/python
@ubuntu:~$ which python3
/usr/bin/python3
@ubuntu:~$ which python3.6
/usr/bin/python3.6

上面是環境中的python版本,去scrapy官網查了一下,好像也有提到支持版本2.7和3.4的,

然后開始百度安裝python3.4的辦法。按照下面的文章的方法安裝成功了。感謝這個作者!

下載源碼直接編譯確實靠譜!哈哈

https://blog.csdn.net/zhongxing9006/article/details/80605126

 

下面是裝完的python3.4的路徑

@ubuntu:~/Python-3.4.10$ which python3.4
/usr/local/bin/python3.4

 

安裝完又用update-alternatives來控制python的版本,可以自定義python默認使用版本。這個方法也是上面鏈接里提供的,確實好用!

@ubuntu:~/Python-3.4.10$ sudo update-alternatives --config python
有 3 個候選項可用於替換 python (提供 /usr/bin/python)。

選擇 路徑 優先級 狀態
------------------------------------------------------------
* 0 /usr/local/bin/python3.4 3 自動模式
1 /usr/bin/python2.7 1 手動模式
2 /usr/bin/python3.6 2 手動模式
3 /usr/local/bin/python3.4 3 手動模式

要維持當前值[*]請按<回車鍵>,或者鍵入選擇的編號:
leiting@ubuntu:~/Python-3.4.10$
leiting@ubuntu:~/Python-3.4.10$
leiting@ubuntu:~/Python-3.4.10$ python
Python 3.4.10 (default, Aug 20 2019, 21:31:05)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
@ubuntu:~/Python-3.4.10$ python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
@ubuntu:~/Python-3.4.10$ python2
Python 2.7.15+ (default, Nov 27 2018, 23:36:35)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
@ubuntu:~/Python-3.4.10$ ipython3
Python 3.6.8 (default, Jan 14 2019, 11:02:34)
Type "copyright", "credits" or "license" for more information.

IPython 5.5.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]:
Do you really want to exit ([y]/n)? y
@ubuntu:~/Python-3.4.10$

 

創建虛擬環境,並指定python3.4版本,pip安裝scrapy,報錯...提示pip版本低,虛擬環境升級pip沒成功,又把系統環境的pip升級了。從9.0升級到19.1,虛擬環境還是pip安裝不了scrapy,有點崩潰了

@ubuntu:~/Python-3.4.10$ pip3 list|grep pip
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
pip (9.0.1)

@ubuntu:~/Python-3.4.10$ pip3 install pip -U
Collecting pip
Cache entry deserialization failed, entry ignored
Downloading https://files.pythonhosted.org/packages/8d/07/f7d7ced2f97ca3098c16565efbe6b15fafcba53e8d9bdb431e09140514b0/pip-19.2.2-py2.py3-none-any.whl (1.4MB)
100% |████████████████████████████████| 1.4MB 48kB/s
Installing collected packages: pip
Successfully installed pip-19.2.2
leiting@ubuntu:~/Python-3.4.10$

 

升級完pip,好像pip3就命令就沒了,全是pip,原因不明

 

升級完pip,創建新虛擬環境,還是pip安裝不起scrapy,煩~后面想,還是不在虛擬環境安裝scrapy,不然每個環境都要按照一次scrapy,還是麻煩,打算還是在默認環境安裝scrapy,每個虛擬環境創建時,都帶上默認環境的包,就可以了,命令用下面這個--system-site-packages

https://virtualenv.pypa.io/en/latest/reference/#virtualenv-command

 

繼續默認環境裝scrapy,還是pip install scrapy,走起,然后報錯了

@ubuntu:~/Python-3.4.10$ pip install scrapy
Collecting scrapy

。。。

ERROR: pyopenssl 19.0.0 has requirement cryptography>=2.3, but you'll have cryptography 2.1.4 which is incompatible.
Installing collected packages: cssselect, pyasn1, attrs, pyasn1-modules, service-identity, queuelib, pyOpenSSL, incremental, Automat, zope.interface, constantly, PyHamcrest, hyperlink, Twisted, PyDispatcher, w3lib, parsel, scrapy
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] 權限不夠: '/usr/local/lib/python3.6/dist-packages/cssselect'
Consider using the `--user` option or check the permissions.

 

真的快崩潰了!!!

百度找了一圈辦法,用下面的命令,問題解決!

pip install --user scrapy

加上--user參數

 

@ubuntu:~/Python-3.4.10$ scrapy
Scrapy 1.7.3 - no active project

Usage:
scrapy <command> [options] [args]

Available commands:
bench Run quick benchmark test
fetch Fetch a URL using the Scrapy downloader
genspider Generate new spider using pre-defined templates
runspider Run a self-contained spider (without creating a project)
settings Get settings values
shell Interactive scraping console
startproject Create new project
version Print Scrapy version
view Open URL in browser, as seen by Scrapy

[ more ] More commands available when run from project directory

Use "scrapy <command> -h" to see more info about a command

真不容易~~~~

 

總結:

1、用源碼安裝python各個版本,用update-alternatives來控制python的版本,可以自定義python默認使用版本

2、pip3 install pip -U,這個-U,意思把pip包升級到最新版本

3、pip install報錯提示Consider using the `--user` option or check the permissions.,就加一個--user參數,有奇效!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM