The new M1 chip MacBooks come with a nasty surprise for those of us who like to tinker with code, especially Python science packages: Attempting to install Pandas may fail.
Failed to build Numpy
Failed to build Pandas
These 2 are pretty necessary in order to work with the Trading Tools I’m developing on this serie (Interactive Brokers API + Python), so it became necessary as well to test and explain how to install everything to make it work properly.
I won’t fake that I understand all the details of what’s going on with the whole GitHub thread because I’m far from it, however in a nutshell these modules are not working by default because of the different M1 chip architecture, and some extra tools and workarounds are necessary to install Numpy and Pandas ( as well other modules, if you happen to see the same error when working on something else).
While an ARM build is the only real solution going forward, particularly given that Apple seems committed to this transition in the next years, for the moment there are a couple of workarounds that we can use. After many hours wasted and much trial and error, the following 2 workflows worked for me:
How to install Numpy in a M1 MacBook:
pip3 install Cython
pip3 install --no-binary :all: --no-use-pep517 numpy
pip3 install pandas
pip3 install ib_insync
Source: Original StackOverflow thread.
Wait a moment, what is Cython? Cython is a superset Python that allows us to write Python code that is then translated to C or C++. In the same line, Cython can also be used to create C or C++ extensions for Python, which can be imported and used just like any other Python module.
How to install Pandas in a M1 MacBook:
python3 -m pip install virtualenv
virtualenv -p python3.8 venv
source venv/bin/activate
pip install --upgrade pip
pip install numpy cython
git clone https://github.com/pandas-dev/pandas.git
cd pandas
python3 setup.py install
Source: Original StackOverflow thread.
We know what Cython is, now, what is Virtualenv? It is a tool used to create isolated Python environments. This allows us to install packages and dependencies for a specific project without interfering with the global Python installation or with other projects on the same system, so we can have different versions of packages for different projects without them conflicting with each other.
Each virtual environment created by Virtualenv is essentially a directory containing a copy of the Python executable, as well as a copy of the Python standard library, and any other packages that you install in it
With that, you should be able to use both modules in your projects!
Updated: 2023-01-15
Leave a Reply