New MWorks Python

Hi Chris,

I’m testing the new embedded Python 3.7 in the latest MWorks nightly.
Our embedded Python code uses zmq to talk to hardware.

Following your instructions, I’ve installed zmq to a user directory.
https://mworks.tenderapp.com/discussions/suggestions/450-python-3-support
Python does find the zmq module.

But when I try to import zmq, I’m getting this error:
, line 9, in _load_libzmq
import sys, ctypes, platform, os
File “python37.zip/ctypes/init.py”, line 7, in
ModuleNotFoundError: No module named ‘_ctypes’

I found this issue:
https://bugs.python.org/issue26598

Could that be the problem? Or do you have any other ideas why this might not work?

Thanks,
Mark

Hi Mark,

The problem is that MWorks’ bundled Python doesn’t include ctypes. (ctypes doesn’t support ARM/iOS at all, and it was causing some problems on macOS, so I opted to omit it.)

Fortunately, the zmq package doesn’t actually need it (except on Windows, I think). I was able to import it via MWorks like this:

import sys

sys.modules['ctypes'] = sys.modules['__main__']
RTLD_GLOBAL = 0

sys.path.append('/Users/cstawarz/Library/Python/3.7/lib/python/site-packages')
import zmq

Cheers,
Chris

A less hacky and more general solution (which would allow you to actually use ctypes if you needed it) would be to create a symbolic link to the .so file in a non-MWorks Python installation. For example:

$ cd $HOME/Library/Python/3.7/lib/python/site-packages
$ ln -s /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/_ctypes.cpython-37m-darwin.so

Then, in your Python file, you could just import zmq as usual, without messing with sys.modules.

Chris

One more option, which also gives you a usable ctypes but doesn’t require that you create a symbolic link:

import importlib.util
_ctypes_path = '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload/_ctypes.cpython-37m-darwin.so'
_ctypes_spec = importlib.util.spec_from_file_location('_ctypes', _ctypes_path)
importlib.util.module_from_spec(_ctypes_spec)

import sys

sys.path.append('/Users/cstawarz/Library/Python/3.7/lib/python/site-packages')
import zmq

Chris

Great.
How did you install Python 3.7 to
/Library/Frameworks/Python.framework/Versions/3.7/
?
Homebrew?

thanks,
Mark

I used the installer from python.org. However, you can install Python any way you want, as long as it matches the version bundled with MWorks (currently 3.7). Just be sure to use the correct path to the _ctypes module in your Python script.

Chris