beeware / Python-Apple-support

A meta-package for building a version of Python that can be embedded into a macOS, iOS, tvOS or watchOS project.
MIT License
1.1k stars 159 forks source link

Python exception: No module named 'math' #170

Closed eldare closed 1 year ago

eldare commented 1 year ago

Describe the bug import math throws the following error: Python exception: No module named 'math'

To Reproduce Steps to reproduce the behavior:

  1. setenv("PYTHONHOME", "<python-stdlib location>", 1)
  2. setenv("PYTHONPATH", "<python-stdlib location>", 1)
  3. Py_Initialize()
  4. Using PythonKit: Python.import("sys") -> Works
  5. Python.import("os") -> Works
  6. Python.import("pathlib") -> Works
  7. Python.import("math") -> Exception

Expected behavior It should be possible to import math.

Screenshots

Screen Shot 2022-12-01 at 22 25 22

Environment:

Additional context I've tried to use Python-3.10-macOS-support.b6, only to the get the same exception.

I tried to make sure the source of the issue is not coming from PythonKit. I used the local Python 3.11 on the mac (by removing the MacOS App's Sandbox + "Disable Library Validation"). It was able to import math with issues.

Thank you

freakboy3742 commented 1 year ago

I believe the issue is your PythonKit configuration. The math module definitely exists, and can be used successfully (verified by both the Python-support-testbed app), and manual testing by adding import math to a Briefcase test project.

From what you've described, the interpreter can't find your lib-dynload path. os and pathlib are backed by a os.py and pathlib.py respectively; math is a binary module in the lib-dynload directory. PYTHONHOME needs to point to a base folder that contains lib/python3.X; and/or PYTHONPATH needs to include the lib-dynload folder. If nether of those things are true, then the interpreter won't be able to find the binary module to load at runtime.

eldare commented 1 year ago

@freakboy3742 Thank you for the reply. You're right lib-dynload was missing from PYTHONPATH. I've added it, and I'm getting a more specific error Contents/Resources/python-stdlib/lib-dynload/math.cpython-311-darwin.so' not valid for use in process: mapped file has no cdhash, completely unsigned? Code has to be at least ad-hoc signed.)

I need to sign it with my TeamID.

I've looked though briefcase project, and found this. It worked :)

set -e
echo "Signed as $EXPANDED_CODE_SIGN_IDENTITY_NAME ($EXPANDED_CODE_SIGN_IDENTITY)"
find "$CODESIGNING_FOLDER_PATH/Contents/Resources/python-stdlib/lib-dynload" -name "*.so" -exec /usr/bin/codesign --force --sign "$EXPANDED_CODE_SIGN_IDENTITY" -o runtime --timestamp=none --preserve-metadata=identifier,entitlements,flags --generate-entitlement-der {} \;

Thank you for pointing me to the right direction 🙏

freakboy3742 commented 1 year ago

@eldare FWIW: We're not actively supporting other use cases external to Briefcase at present, but if someone were to write up a usage guide as a PR, I wouldn't reject the PR.

eldare commented 1 year ago

I understand. Thank you again :)

eldare commented 1 year ago

@freakboy3742 as suggested, I've submitted a PR with a detailed usage guide. https://github.com/beeware/Python-Apple-support/pull/171