philkr / pystk

Python SuperTuxKart interface
Other
48 stars 29 forks source link

Issues Importing pystk in PySuperTuxKart on Apple M2 Chip with macOS #39

Open victoriaagc opened 12 months ago

victoriaagc commented 12 months ago

Describe the bug I am unable to import the pystk module after installing PySuperTuxKart on my MacBook Pro with an Apple M2 chip. The installation of PySuperTuxKart reports success, but attempting to import pystk in Python results in an ImportError related to missing symbols in the libpng library.

To Reproduce

pip install -U PySuperTuxKart
python -c "import pystk"

Expected behavior I expected the pystk module to be imported without errors, allowing me to run the controller.py script for my SuperTuxKart game project as part of my coursework.

Screenshots

image

System information:

Additional context

saucesaft commented 10 months ago

I have the same error on a M1 Macbook Air.

kweinmeister commented 10 months ago

A workaround is to update libpng and build from source:

sachin-utexas commented 9 months ago

Hey @kweinmeister , thanks for your suggestions. I used v1.6.42 of libpng and also needed to make the if (NOT APPLE) change. But I couldn't get past the python setup.py build step. It fails with

make[2]: *** No rule to make target `zlibstatic', needed by `/Users/dev/Documents/pystk/build/lib.macosx-11.1-arm64-cpython-310/pystk.cpython-310-darwin.so'.  Stop.
make[1]: *** [CMakeFiles/pystk.dir/all] Error 2

Any idea what might be wrong?

SPJ0 commented 9 months ago

I got it working on my Mac. Here is how I did it. Hopefully it works for others too. Let me know.

First, I followed the work-around above. Before building, in pystk/CMakeLists.txt, I changed the line

set(ZLIB_LIBRARY zlibstatic) # old line

to

set(ZLIB_LIBRARY "${PROJECT_BINARY_DIR}/lib/zlib/libzlibstatic.a") # new line

sachin-utexas commented 9 months ago

Thanks @SPJ0 that did the trick 👍

Also for anyone else reading, if you had previously installed pystk with pip, please pip uninstall it and then build it from source.

FrancisMasangcay commented 8 months ago

I'm leaving this here as the above work arounds did not resolve my issue. For context I was running this project on an M1 Macbook Air.

What ultimately resolved the issue was pip installing Pystk through an emulator. This can be done with a Windows emulator via a VM such as VMware Fusion or Parallels. Even simpler would be to install an IDE that is designated to run on an Intel chip Mac. In my case my preferred IDE is VSCode and I installed the Intel chip VSCode found here:

https://code.visualstudio.com/download

This triggers Rosetta to run the emulated application and I found that I was able to successfully pip install Pystk from a virtual env in the emulated IDE. Building from source was not required. Note Python versions > 3.9.6 have trouble installing Pystk through pip at the time of this post (Spring 2024).

ZanwarFaraj commented 8 months ago

Thank you for sharing this Francis. I was having difficulties installing and successfully importing pystk on an M2 Mac and with some modifications, this approach worked for me.

The general approach is to use a Rosetta based terminal (where the terminal is emulating an Intel architecture) to install an Intel version of Python <= 3.9.6 and then build your virtual environment on top of that Intel version of Python. You then install the necessary packages in that virtual environment from within the Rosetta terminal. The Intel version of VS Code essentially serves as a Rosetta terminal but you can also just open the regular Terminal application in Rosetta mode, as described here: https://www.courier.com/blog/tips-and-tricks-to-setup-your-apple-m1-for-development/.

The key point is that you need an Intel version of Python, as the ARM version of Python seems to result in errors when importing pystk. I installed an Intel Python 3.8.18 using a terminal within the Intel version of VS Code by following the instructions in this guide: https://acrogenesis.com/running-intel-python-on-m1. I then created a venv virtual environment using the built-in command within VS code and installed the packages in requirements.txt into that environment. I think other approaches would also work as long as you're installing both Python and the packages from within a Rosetta terminal but I can confirm the approach I just described worked on an M2 Mac.

This installation was really frustrating and I hope this post makes the installation a little smoother for others.