Search code examples
pythoncryptographysolidityapple-m1web3py

Web3 python crypto cypher issue on M1 Mac


When I try to build a blockchain transaction using Web3 on python, I'm getting an error that is apparently because I'm using an Apple Silicon computer (2020 M1 MacBook Pro). I'm following a popular Solidity, Blockchain, and Smart Contract course on YouTube and I'm unable to get it working. Could someone please help me out?

My code fails when I try to build a transaction.

The error message I get when I run my code is below:


(myvenv) leonkc@Leons-MacBook-Pro web3_py_simple_storage % python deploy.py
Traceback (most recent call last):
File "/Users/leonkc/solidityDemo/web3_py_simple_storage/deploy.py", line 3, in \<module\>
from web3 import Web3
File "/Users/leonkc/solidityDemo/web3_py_simple_storage/myvenv/lib/python3.9/site-packages/web3/__init__.py", line 6, in \<module\>
from eth_account import (
File "/Users/leonkc/solidityDemo/web3_py_simple_storage/myvenv/lib/python3.9/site-packages/eth_account/__init__.py", line 1, in \<module\>
from eth_account.account import (
File "/Users/leonkc/solidityDemo/web3_py_simple_storage/myvenv/lib/python3.9/site-packages/eth_account/account.py", line 11, in \<module\>
from eth_keyfile import (
File "/Users/leonkc/solidityDemo/web3_py_simple_storage/myvenv/lib/python3.9/site-packages/eth_keyfile/__init__.py", line 7, in \<module\>
from eth_keyfile.keyfile import (  # noqa: F401
File "/Users/leonkc/solidityDemo/web3_py_simple_storage/myvenv/lib/python3.9/site-packages/eth_keyfile/keyfile.py", line 6, in \<module\>
from Crypto.Cipher import AES
File "/Users/leonkc/solidityDemo/web3_py_simple_storage/myvenv/lib/python3.9/site-packages/Crypto/Cipher/__init__.py", line 27, in \<module\>
from Crypto.Cipher.\_mode_ecb import \_create_ecb_cipher
File "/Users/leonkc/solidityDemo/web3_py_simple_storage/myvenv/lib/python3.9/site-packages/Crypto/Cipher/\_mode_ecb.py", line 35, in \<module\>
raw_ecb_lib = load_pycryptodome_raw_lib("Crypto.Cipher.\_raw_ecb", """
File "/Users/leonkc/solidityDemo/web3_py_simple_storage/myvenv/lib/python3.9/site-packages/Crypto/Util/\_raw_api.py", line 309, in load_pycryptodome_raw_lib
raise OSError("Cannot load native module '%s': %s" % (name, ", ".join(attempts)))
OSError: Cannot load native module 'Crypto.Cipher.\_raw_ecb': Not found '\_raw_ecb.cpython-39-darwin.so', Cannot load '\_raw_ecb.abi3.so': dlopen(/Users/leonkc/solidityDemo/web3_py_simple_storage/myvenv/lib/python3.9/site-packages/Crypto/Util/../Cipher/\_raw_ecb.abi3.so, 0x0006): tried: '/Users/leonkc/solidityDemo/web3_py_simple_storage/myvenv/lib/python3.9/site-packages/Crypto/Util/../Cipher/\_raw_ecb.abi3.so' (mach-o file, but is an incompatible architecture (have (x86_64), need (arm64e))), '/Users/leonkc/solidityDemo/web3_py_simple_storage/myvenv/lib/python3.9/site-packages/Crypto/Cipher/\_raw_ecb.abi3.so' (mach-o file, but is an incompatible architecture (have (x86_64), need (arm64e))), Not found '\_raw_ecb.so'

I've tried reinstalling pycryptodome as per another Stack Overflow answer but I still get the same error (with pip install pycryptodome --no-cache-dir --verbose --force-reinstall )

I'm using a Ganache Ethereum blockchain with an unimportant test address.


Solution

  • Update: I finally ended up fixing this. It was a combination of jeugregg's suggestion here and luchanos's solution in this thread.

    That is to say, the things that finally made it work for me were:

    • Downloading Python 3.9.5 from https://www.python.org/downloads/macos/ with "Download macOS 64-bit universal2 installer"
    • Use python3-intel64 command to run Python instead of just python
    • Create new venv in project from the base interpreter that you installed
    • Install all requirements to venv
    • Uninstall pycryptodome with pip (I think I had to do pip uninstall pycryptodome twice) and reinstalling with conda install pycryptodome (crucial step)
    • If I recall correctly I had to do similar reinstallations with a few other libraries (e.g. cytoolz, bitarray), which came up as errors when trying to install eth-brownie and which I addressed ad-hoc, but this may not be a problem for you
    • pipx install eth-brownie=1.14.6

    Python 3.9.5 and brownie version 1.14.6 seem to be the best combination.