以太坊智能合约项目开发——Web3.py库安装及错误解决方案

不羁的心 提交于 2020-02-26 21:50:46

一、Web3.py库介绍

web3.js 是一组用来和本地或远程以太坊节点进行交互的 js 库,它可以使用 HTTP 或 IPC 建立与以太坊节点旳连接,那么 web3.py 则是一个使 python 应用与以太坊区块链进行交互的 python 库。关于web3

二、Web3.py库安装

安装前的准备

  • 安装python 3.5.4版本
    版本说明:我最初安装的3.5.2版本然后报错,报错内容中提示版本要>=3.5.3 <4,但是看到网上也有3.6报错的情况
G:\JsuBaron\bdss>python -V
Python 3.5.4

G:\JsuBaron\bdss>pip -V
pip 20.0.2 from f:\python3.5.4\lib\site-packages\pip (python 3.5)
  • 升级有pip:安装python的时候一般都同时安装了pip,将其升级;
You are using pip version 9.0.1, however version 20.0.2 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
**上面是前面的报错提示**
 
C:\Users>python -m pip install --upgrade pip
Collecting pip
  Using cached https://files.pythonhosted.org/packages/54/0c/d01aa759fdc501a58f431eb594a17495f15b88da142ce14b5845662c13f3/pip-20.0.2-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 9.0.1
    Uninstalling pip-9.0.1:
      Successfully uninstalled pip-9.0.1
Successfully installed pip-20.0.2

进行安装

  • 官方安装命令:pip install web3
  • 推荐使用命令 pip install web3 -i https://pypi.douban.com/simple,安装速度会比较快

我所遇到安装中的坑,及其解决办法

按理说输入安装命令就能安装成功,但是没有一次性成功。(我觉得我无论安装什么都会出点幺蛾子)

经过仔细研究报错语句,然后开始寻找解决办法,并从这篇文章 洪文扬. 关于安装python类库web3遇到的坑 包含解决办法 受到启发成功解决了错误。

我的错误和上面作者的错误一样,这里摘录一部分看一下:

Installing collected packages: pycryptodome, eth-hash, eth-typing, toolz, cytoolz, eth-utils, hexbytes, lru-dict, pywin32, pypiwin32, chardet, urllib3, idna, certifi, requests, six, parsimonious, eth-abi, attrdict, eth-keys, eth-keyfile, rlp, eth-rlp, eth-account, websockets, web3
    Running setup.py install for toolz ... done
    Running setup.py install for cytoolz ... error
    ERROR: Command errored out with exit status 1:
**后面还有好大一串,这里不展示了**
copying cytoolz\tests\test_signatures.py -> build\lib.win-amd64-3.5\cytoolz\tests
    copying cytoolz\tests\test_tlz.py -> build\lib.win-amd64-3.5\cytoolz\tests
    copying cytoolz\tests\test_utils.py -> build\lib.win-amd64-3.5\cytoolz\tests
    running build_ext
    building 'cytoolz.dicttoolz' extension
    error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'f:\python3.5.4\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\我的电~1\\AppData\\Local\\Temp\\pip-install-h9dmjret\\cytoolz\\setup.py'"'"'; __file__='"'"'C:\\Users\\我的电~1\\AppData\\Local\\Temp\\pip-install-h9dmjret\\cytoolz\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\我的电~1\AppData\Local\Temp\pip-record-8pc4lo2i\install-record.txt' --single-version-externally-managed --compile --install-headers 'f:\python3.5.4\Include\cytoolz' Check the logs for full command output.

分析报错内容发现:error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools"

那就安装 Microsoft Visual C++ 14.0,选择链接中的5.Microsoft Visual C++ 14.0 standalone: Visual C++ Build Tools 2015 (x86, x64, ARM),然后下载 cytoolz-0.10.1.tar.gz (475.6 kB),并cmd到解压后的目录中,输入下面的命令进行安装:python安装模块如何通过setup.py安装

F:> python setup.py build
F:> python setup.py install

这次仍有报错,分析报错内容发现关键是LINK : fatal error LNK1158: cannot run 'rc.exe' error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\link.exe' failed with exit status 1158不能运行rc.exe
python setup.py build 报错
然后从 win10 安装"pip install orange3" 出现LINK : fatal error LNK1158: cannot run ‘rc.exe’ 错误的解决办法 找到解决办法。再次执行上面两条命令,成功安装cytoolz。

Processing cytoolz-0.10.1-py3.5-win-amd64.egg
creating f:\python3.5.4\lib\site-packages\cytoolz-0.10.1-py3.5-win-amd64.egg
Extracting cytoolz-0.10.1-py3.5-win-amd64.egg to f:\python3.5.4\lib\site-packages
Adding cytoolz 0.10.1 to easy-install.pth file

Installed f:\python3.5.4\lib\site-packages\cytoolz-0.10.1-py3.5-win-amd64.egg
Processing dependencies for cytoolz==0.10.1
Searching for toolz==0.10.0
Best match: toolz 0.10.0
Adding toolz 0.10.0 to easy-install.pth file

Using f:\python3.5.4\lib\site-packages
Finished processing dependencies for cytoolz==0.10.1

我在安装的过程中发现大部分下载的都是.wl文件,只有cytoolzlru-dict.tar.gz文件,确实在https://pypi.org/sample/cytoolz/https://pypi.org/sample/lru-dict/中只是.tar.gz

然后还有一个问题,可能.wl文件也会下不下来,比如我在安装web3.py成功之前遇到过idna.wl安装失败的情况,意思是不能从https://pypi.python.org/simple/idna/把相应的文件下载下来,那就自己下载呗!

打开https://pypi.python.org/simple/idna/发现里面许多不同版本的idna.wl文件,于是找到指定范围的版本(我的报错里面有说要求该文件版本>=2.5 <4),下载到本地,并用命令pip install idna-2.5-py2.py3-none-any.whl进行安装。
在这里插入图片描述

E:\>pip install idna-2.5-py2.py3-none-any.whl
Processing e:\idna-2.5-py2.py3-none-any.whl
Installing collected packages: idna
Successfully installed idna-2.5

这次问题都解决完了,命令行成功安装web3.py。

Installing collected packages: pywin32, pypiwin32, eth-utils, eth-keys, hexbytes, eth-keyfile, rlp, eth-rlp, six, attrdict, eth-account, websockets, certifi, idna, urllib3, chardet, requests, lru-dict, parsimonious, eth-abi, web3
    Running setup.py install for lru-dict ... done
    Running setup.py install for parsimonious ... done
Successfully installed attrdict-2.0.1 certifi-2019.11.28 chardet-3.0.4 eth-abi-1.3.0 eth-account-0.3.0 eth-keyfile-0.5.1 eth-keys-0.2.4 eth-rlp-0.1.2 eth-utils-1.8.4 hexbytes-0.1.0 idna-2.9 lru-dict-1.1.6 parsimonious-0.8.1 pypiwin32-223 pywin32-227 requests-2.23.0 rlp-1.2.0 six-1.14.0 urllib3-1.25.8 web3-4.10.0 websockets-6.0

结语: 如果我的文章没有解决你的问题,或许你还可以找找其它办法,如:Python Web3.py 设置托管以太坊节点(Windows)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!