如何修复在Python上安装web3的错误?


ERROR: Command errored out with exit status 1:
command: 'C:Program FilesPython39python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\KimXD\AppData\Local\Temp\pip-install-7pjpmdge\bitarray_3a1e8028774c4a059cdba67b160795fd\setup.py'"'"'; __file__='"'"'C:\Users\KimXD\AppData\Local\Temp\pip-install-7pjpmdge\bitarray_3a1e8028774c4a059cdba67b160795fd\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:UsersKimXDAppDataLocalTemppip-record-f8bq9svcinstall-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:UsersKimXDAppDataRoamingPythonPython39Includebitarray'
cwd: C:UsersKimXDAppDataLocalTemppip-install-7pjpmdgebitarray_3a1e8028774c4a059cdba67b160795fd
Complete output (17 lines):
running install
running build
running build_py
creating build
creating buildlib.win-amd64-3.9
creating buildlib.win-amd64-3.9bitarray
copying bitarraytest_bitarray.py -> buildlib.win-amd64-3.9bitarray
copying bitarraytest_util.py -> buildlib.win-amd64-3.9bitarray
copying bitarrayutil.py -> buildlib.win-amd64-3.9bitarray
copying bitarray__init__.py -> buildlib.win-amd64-3.9bitarray
running build_ext
building 'bitarray._bitarray' extension
creating buildtemp.win-amd64-3.9
creating buildtemp.win-amd64-3.9Release
creating buildtemp.win-amd64-3.9Releasebitarray
cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:Program FilesPython39include -IC:Program FilesPython39include /Tcbitarray/_bitarray.c /Fobuildtemp.win-amd64-3.9Releasebitarray/_bitarray.obj
error: command 'cl.exe' failed: None
----------------------------------------
ERROR: Command errored out with exit status 1: 'C:Program FilesPython39python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\KimXD\AppData\Local\Temp\pip-install-7pjpmdge\bitarray_3a1e8028774c4a059cdba67b160795fd\setup.py'"'"'; __file__='"'"'C:\Users\KimXD\AppData\Local\Temp\pip-install-7pjpmdge\bitarray_3a1e8028774c4a059cdba67b160795fd\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:UsersKimXDAppDataLocalTemppip-record-f8bq9svcinstall-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:UsersKimXDAppDataRoamingPythonPython39Includebitarray' Check the logs for full command output.

我假设您正在尝试使用pip命令python -m pip install web3(或简称pip install web3,尽管第一种形式更好)安装web3。当执行这个命令时,我得到了大致相同的错误。

问题不在web3,但是,在它的一个依赖。您可以通过使用--no-deps安装它来检查这一点,它工作得很好:

C:tempstack_overflow>pip install --no-deps web3
Collecting web3
Using cached web3-5.24.0-py3-none-any.whl (487 kB)
Installing collected packages: web3
Successfully installed web3-5.24.0

但是这可能并不能解决您的问题,因为如果没有所有的依赖项,模块可能无法工作。在这种情况下,失败的依赖项是bitarray。这是因为bitarray是一个源包。此库的文档包含安装说明:

如果你有一个工作的C编译器,你可以简单地:

$ pip install bitarray

如果您希望使用预编译的二进制文件,您可以:

  • conda install bitarray(默认的Anaconda存储库和conda-forge都支持bitarray)
  • 下载Windows wheel from Chris Gohlke

我做了最后一步:从提到的网站下载轮子,并安装它与pip install bitarray-2.3.4-cp38-cp38-win_amd64.whl(你的版本可能是不同的当然)。

然而,web3也依赖于lru-dict,这导致了同样的问题。不幸的是,我找不到这个包的预编译二进制文件,所以如果遇到同样的问题,现在有两个选项:

  1. 使用conda-forge来安装您的软件包。我个人没有使用conda的经验,但是conda-forge似乎支持lru-dict包(以及上面提到的bitarray包)。

  2. 确保安装Microsoft Visual c++ 14.0或更高版本。如错误信息中所述:使用"Microsoft c++ Build Tools"获取:https://visualstudio.microsoft.com/visual-cpp-build-tools/

    这个页面包含一些关于如何下载和安装它的更详细的说明。

我通过将Visual c++编译器更新到最新版本并确保包含Windows 10 SDK来实现它,正如在这个SO答案中解释的那样。

(venv) C:tempstack_overflow>python.exe -m pip install web3
Collecting web3
Using cached web3-5.24.0-py3-none-any.whl (487 kB)
(...lots of lines listing requirements already satisfied...)
Building wheels for collected packages: lru-dict, bitarray
Building wheel for lru-dict (setup.py) ... done
Created wheel for lru-dict: filename=lru_dict-1.1.7-cp39-cp39-win_amd64.whl size=12711 sha256=a6ccbda691f0a941591c1b9c1ae24a5a4ee26d726685f2e71aa382e5b77a3d65
Stored in directory: c:userswovanoappdatalocalpipcachewheels863ff66efebdbc72403e91c39f8cc817f61627d065eed97921b8e5b8
Building wheel for bitarray (setup.py) ... done
Created wheel for bitarray: filename=bitarray-1.2.2-cp39-cp39-win_amd64.whl size=55817 sha256=a03c3e6ac482b1cbe4adce197f0bd4333c7094af2d785f5575bdff16a024710d
Stored in directory: c:userswovanoappdatalocalpipcachewheelsf6b38dbb7212600c4671dcb713dec6574c37b118c2c321e6ed52fe3
Successfully built lru-dict bitarray
Installing collected packages: bitarray, attrs, async-timeout, websockets, pywin32, protobuf, lru-dict, jsonschema, ipfshttpclient, eth-account, aiohttp, web3
Successfully installed aiohttp-3.7.4.post0 async-timeout-3.0.1 attrs-21.2.0 bitarray-1.2.2 eth-account-0.5.6 ipfshttpclient-0.8.0a2 jsonschema-3.2.0 lru-dict-1.1.7 protobuf-3.18.1 pywin32-302 web3-5.24.0 websockets-9.1

注意:建议将web3安装在一个干净的虚拟环境中,以避免与其他已安装模块的版本冲突。

让我告诉你一个解决这类问题的完美方法。拥有一个解决不同问题的版本问题的虚拟环境总是更好的……你可以按照下面的步骤来做,它会有帮助的

# Install pip if it is not available:
$ which pip || curl https://bootstrap.pypa.io/get-pip.py | python
# Install virtualenv if it is not available:
$ which virtualenv || pip install --upgrade virtualenv
# *If* the above command displays an error, you can try installing as root:
$ sudo pip install virtualenv
# Create a virtual environment:
$ virtualenv -p python3 ~/.venv-py3
# Activate your new virtual environment:
$ source ~/.venv-py3/bin/activate
# With virtualenv active, make sure you have the latest packaging tools
$ pip install --upgrade pip setuptools
# Now we can install web3.py...
$ pip install --upgrade web3 

我有一个同样的问题,我用anaconda解决了。

首先检查你的Microsoft Visual c++,安装并重新启动你的计算机。其次,您应该观看一些Anaconda的教程,之后,您需要创建一个虚拟环境,选择文件夹并完成。代码示例:in to cmd

1) conda create --name 'pick a name'
2) conda activate 'yourname'
3) conda install -c conda-forge web3

之后,进入文本编辑器,选择python解释器(在VSC中alt+shift+p),您已经创建的ENVS,查看C:Users'youruser'.conda ENVS ' yournameenvs .

最新更新