Importing Cubit into PyCharm or Spyder

Hello all,

I am trying to use Cubit with Python, however, I cannot import cubit. I am currently using PyCharm but this hasn’t worked with Spyder either. I can, however, open the python terminal for cubit and import cubit from there.

How can I solve this importing problem?

Welcome @azk4 !

Make sure that you add the path to Cubit to your path in Cubit (so it knows where to look for the import)

import sys
sys.path.append( r"C:\Program Files\Coreform Cubit 2022.4\bin" )
import cubit
cubit.init( [] )

Does that work for you?

BTW, we are working on a much more general solution for using Python (and other languages) to control Cubit – stay tuned!

This file path does not exist in my PC “C:\Program Files\Coreform Cubit 2022.4\bin”. I attempted already to use sys.path.append with where cubit is located but nothing is working.

What OS do you have, what version of Cubit? You’ll need to make sure that path is valid for both.

Since you mentioned that you’ve already tried using sys.path.append(), do you mind sharing the relevant portions (i.e. the equivalent code to what I provided above)?

import os
import numpy
import sys
sys.path.append(r"C:\Program Files\Cubit-16.04/bin")
print("Python version")
print(sys.version)
try:
    import cubit
except ImportError:
    print("Error: Importing cubit as python module failed")
    print("could not import cubit, please check your PYTHONPATH settings...")
    print("")
    print("current path: ")
    print(sys.path)
    print("")
    print("try to include path to directory which includes file cubit.py, e.g. /opt/Trelis-15.0/bin/")
    print("")
    sys.exit("Import cubit failed")

print(sys.path)

In the terminal I get:

C:\Users\user\PycharmProjects\cubit\venv\Scripts\python.exe C:/Users/user/PycharmProjects/cubit/main.py 
Python version
3.10.6 (tags/v3.10.6:9c7b4bd, Aug  1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)]
Error: Importing cubit as python module failed
could not import cubit, please check your PYTHONPATH settings...

current path: 
['C:\\Users\\user\\PycharmProjects\\cubit', 'C:\\Users\\user\\PycharmProjects\\cubit', 'C:\\Program Files\\Cubit-16.04\\bin', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310\\DLLs', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310\\lib', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310', 'C:\\Users\\user\\PycharmProjects\\cubit\\venv', 'C:\\Users\\user\\PycharmProjects\\cubit\\venv\\lib\\site-packages', 'C:\\Program Files\\Cubit-16.04/bin']

try to include path to directory which includes file cubit.py, e.g. /opt/Trelis-15.0/bin/

Import cubit failed

Process finished with exit code 1

I also forgot to mention that i am using Windows 10 with Cubit 16.04 and Python 3 which you should see reported in the terminal response in case it is not clear.

I am using PyCharm at the moment but it does not work with Spyder either

OK - looks like you’re using Sandia’s Cubit (based on Cubit-16.04 in your snippets). As I don’t have a build of Sandia’s Cubit, I’m not going to be able to help as much as if it were Coreform Cubit. It may be that you are importing the Cubit module from a different version of Python (v3.10) than what Sandia’s Cubit was built with (v3.7?) To verify, open up the Cubit GUI and run sys.version in the Python command line.

If that doesn’t work (or if you have additional questions) I’d also recommend sending a message to Sandia’s help, cubit-help@sandia.gov, as they’ll be more equipped to answer questions regarding their distribution of Cubit.