Import Python libraries from Python3

I am trying to import cubit in my python environment, I first add in the python path:

/Applications/Coreform-Cubit-2021.3.app/Contents/MacOS

then I have in my script the following command:

import cubit

it works with python2 but if I do the same thing with python3 I get an error:

Traceback (most recent call last):
File “myscript.py”, line 16, in
import cubit
File “/Applications/Coreform-Cubit-2021.3.app/Contents/MacOS/cubit.py”, line 29, in
from cubit3 import *
File “/Applications/Coreform-Cubit-2021.3.app/Contents/MacOS/cubit3.py”, line 15, in
import _cubit3
ImportError: dlopen(/Applications/Coreform-Cubit-2021.3.app/Contents/MacOS/_cubit3.so, 2): Symbol not found: _PyCMethod_New
Referenced from: /Applications/Coreform-Cubit-2021.3.app/Contents/MacOS/_cubit3.so
Expected in: flat namespace
in /Applications/Coreform-Cubit-2021.3.app/Contents/MacOS/_cubit3.so

Any Idea why?

My python version is Python 3.8.5 (anaconda3)

Hi @balep, sorry for your trouble. First off, we use Python 3.9.2 on Mac, so loading Cubit into 3.8.5 is unlikely to work. Secondly, in 2021.3 there were still some Mac-specific bugs in how we packaged Python 3.9.2 that have been fixed in 2021.5. I’d recommend you do two things:
Upgrade to 2021.5 from our downloads page.
Either use Python 3.9.2 or use the version of Python that ships with Cubit. On Mac, it’s at this path:

/Applications/Coreform-Cubit-2021.5.app/Contents/MacOS/python3/Python.framework/Versions/3.9/bin/python3

That’s a bit of a mouthful, unfortunately, but I know you won’t have any issues with that version of Python because we test against that.

1 Like

Thanks a lot for the help I will test it and let you know!

Thanks for asking this @balep. :blush: Another workaround I had for this was to install a conda environment with Python 3.9.2. Once the environment was activated, at the top of my Python script I include:

import sys
import os
sys.path.append("/Applications/Coreform-Cubit-2021.5.app/Contents/MacOS/")
print("Python version")
print (sys.version)
import cubit

cubit.init([''])

I also can install modules like NumPy into my conda env and utilize them as well.

1 Like