'How can I fix this error installing web3 on Python

ERROR: Command errored out with exit status 1:
     command: 'C:\Program Files\Python39\python.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('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\KimXD\AppData\Local\Temp\pip-record-f8bq9svc\install-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:\Users\KimXD\AppData\Roaming\Python\Python39\Include\bitarray'
         cwd: C:\Users\KimXD\AppData\Local\Temp\pip-install-7pjpmdge\bitarray_3a1e8028774c4a059cdba67b160795fd\
    Complete output (17 lines):
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.9
    creating build\lib.win-amd64-3.9\bitarray
    copying bitarray\test_bitarray.py -> build\lib.win-amd64-3.9\bitarray
    copying bitarray\test_util.py -> build\lib.win-amd64-3.9\bitarray
    copying bitarray\util.py -> build\lib.win-amd64-3.9\bitarray
    copying bitarray\__init__.py -> build\lib.win-amd64-3.9\bitarray
    running build_ext
    building 'bitarray._bitarray' extension
    creating build\temp.win-amd64-3.9
    creating build\temp.win-amd64-3.9\Release
    creating build\temp.win-amd64-3.9\Release\bitarray
    cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Program Files\Python39\include -IC:\Program Files\Python39\include /Tcbitarray/_bitarray.c /Fobuild\temp.win-amd64-3.9\Release\bitarray/_bitarray.obj
    error: command 'cl.exe' failed: None
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'C:\Program Files\Python39\python.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('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\KimXD\AppData\Local\Temp\pip-record-f8bq9svc\install-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:\Users\KimXD\AppData\Roaming\Python\Python39\Include\bitarray' Check the logs for full command output.


Solution 1:[1]

I assume you were trying to install web3 using the pip command python -m pip install web3 (or pip install web3 for short, although the first form is better). I got roughly the same error when performing this command.

The problem isn't in web3, however, but in one of its dependencies. You can check this by installing it with --no-deps, which works fine:

C:\temp\stack_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

But this probably doesn't solve your problem, because the module likely doesn't work without all its dependencies. The dependency that is failing in this case is bitarray. This is because bitarray is a source package. The documentation of this library contains installation instructions:

If you have a working C compiler, you can simply:

$ pip install bitarray

If you rather want to use precompiled binaries, you can:

  • conda install bitarray (both the default Anaconda repository as well as conda-forge support bitarray)
  • download Windows wheels from Chris Gohlke

I did the last step: downloading the wheel from the mentioned website, and installing it with pip install bitarray-2.3.4-cp38-cp38-win_amd64.whl (your version may be different of course). This worked like a charm.

However, web3 also has a dependency on lru-dict, which resulted in the same issue. Unfortunately, I could not find pre-compiled binaries for this package, so if you encounter the same problem, you now have two options:

  1. Use conda-forge to install your packages. I personally don't have experience with conda, but conda-forge seems to support the lru-dict package (as well as the above-mentioned bitarray package).

  2. Make sure to install Microsoft Visual C++ 14.0 or greater. As mentioned in the error message: Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/

    This page contains some more detailed instructions on how to download and install it.

I got it working by updating the Visual C++ compiler to the latest version and make sure to include the Windows 10 SDK, as explained in this SO answer.

(venv) C:\temp\stack_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:\users\wovano\appdata\local\pip\cache\wheels\86\3f\f6\6efebdbc72403e91c39f8cc817f61627d065eed97921b8e5b8
  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:\users\wovano\appdata\local\pip\cache\wheels\f6\b3\8d\0bb7212600c4671dcb713dec6574c37b118c2c321e6ed52fe3
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

NB: It is recommended to install web3 in a clean virtual environment, to avoid version conflicts with other installed modules.

Solution 2:[2]

Let me tell you one perfect solution for such problems. It is always better to have a virtual environment which solves the version problems of the different problems ... you may follow the below process and it will help

# 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 

Solution 3:[3]

I have an equal problem and I resolved with anaconda.

First check your Microsoft Visual C++, install and restart your computer. Second, you should watch a some Anaconda's tutorial, after that, you need to create a virtual environment, pick the folder and done. Example in code : In to cmd

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

After that, go to your text editor and pick how python interpreter (alt+shift+p in VSC), the one you already created like ENVS, look in C:\Users'youruser'.conda\envs'yournameENVS.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 wovano
Solution 2 Eric Aya
Solution 3 Ryan M