New MWorks Python

mhisted's Avatar

mhisted

20 Mar, 2019 01:31 PM

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 <module>
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

  1. Support Staff 1 Posted by Christopher Sta... on 20 Mar, 2019 02:00 PM

    Christopher Stawarz's Avatar

    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

  2. Support Staff 2 Posted by Christopher Sta... on 20 Mar, 2019 02:22 PM

    Christopher Stawarz's Avatar

    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

  3. Support Staff 3 Posted by Christopher Sta... on 20 Mar, 2019 02:35 PM

    Christopher Stawarz's Avatar

    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

  4. 4 Posted by mhisted on 20 Mar, 2019 04:34 PM

    mhisted's Avatar

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

    thanks,
    Mark

  5. Support Staff 5 Posted by Christopher Sta... on 20 Mar, 2019 05:18 PM

    Christopher Stawarz's Avatar

    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

  6. Christopher Stawarz closed this discussion on 25 Mar, 2019 04:15 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac