I have recently upgraded to Trelis 17.0 from Trelis 16.5.4.
Earlier with Trelis 16.5.4, it was possible to use Trelis and the Cubit library from a Python environment with the following code (a .py script file with the Trelis code was executed in the Python Environment)-
import sys
sys.path.append('C:/Program Files/Trelis 16.5.4/bin/')
sys.path.append('C:/Program Files/Trelis 16.5.4/bin/Lib/')
import cubit
cubit.init([''])
cubit.cmd...........
However, since updating to Trelis 17.0, this has stopped working. of course, the path in the code above has been edited to -
sys.path.append('C:/Program Files/Trelis 17.0/bin/')
sys.path.append('C:/Program Files/Trelis 17.0/bin/Lib/')
But it still doesn’t seem to work. Could you please help? Below is the error -
import cubit
File "C:/Program Files/Trelis 17.0/bin/cubit.py", line 20, in <module>
from cubit2 import *
File "C:/Program Files/Trelis 17.0/bin/cubit2.py", line 21, in <module>
_cubit2 = swig_import_helper()
File "C:/Program Files/Trelis 17.0/bin/cubit2.py", line 20, in swig_import_helper
return importlib.import_module('_cubit2')
File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
__import__(name)
ImportError: DLL load failed: The specified module could not be found.
Also, please note that the above code works when it is opened and executed inside the Trelis 17.0 GUI environment.
Also, I saw in a post that Trelis 17.0 now has support for both Python 2 and Python 3. Is this correct? I ask because the command -
Somehow I didn’t see your post earlier - thanks for replying to “bump” the thread. I’m not a Coreform-er, but I’ve had the same issue with importing the Cubit python library. This is seemingly not limited to Trelis, but Cubit as well.
I’ve previously brought up this issue with @randy and another core developer - I can’t remember exactly what the finding was, but I seem to recall it was maybe due to changes in Windows not working with the Python library import. I can’t recall if a fix was found, it sounded like a true fix was going to be fairly involved and may take some time. Personally, I switched to the Linux build of Trelis and use my Windows Subsystem Linux (WSL) as a work-around. Definitely would like to see this issue resolved.
Regarding your other question:
Also, I saw in a post that Trelis 17.0 now has support for both Python 2 and Python 3. Is this correct? I ask because the command - print(sys.version) - still shows 2.7.10.
In the GUI you need to change from Python2 to Python3 like so:
Thank you so much for your response. At least one problem is solved for now
Thank you for the idea about the Linux workaround. It will be difficult to implement it in my situation, but it is a solution.
The annoying this is it used to work perfectly in Trelis 16.5.4. Ever since Trelis 17.0 was installed, the cubit library couldn’t be called from a Python environment anymore.
I hope people notice your attention and there’s some solution. Maybe the fact that it worked for Trelis 16.5.4 means that the workaround is fairly simple?
Hi, it looks like I am facing the same issue when trying to import Cubit (2020.2) into Python 3 in Windows. I was looking for similar posts and found this one. @gvernon may I know if this issue has been resolved in the 2020.2 version or any future version? Also, were you using WSL during the Python webinar?
These are a few lines from my .py code:
import sys
sys.path.append(‘C:/Program Files/Coreform Cubit 2020.2/bin’)
import cubit
cubit.init()
This is the error message:
File “C:/Program Files/Coreform Cubit 2020.2/bin\cubit.py”, line 22, in
from cubit3 import *
File “C:/Program Files/Coreform Cubit 2020.2/bin\cubit3.py”, line 15, in
import _cubit3
ImportError: DLL load failed while importing _cubit3: The specified module could not be found
Thanks for your response @scot but I am facing the same _cubit3 import error in v2021.5 as well. I would appreciate it if you could help me troubleshoot this issue. Thanks.
Use the instance of Python that ships with Cubit. It’s located at C:\Program Files\Coreform Cubit 2021.5\bin\python3\python.exe. We ship (and Cubit is linked against) Python 3.8.2. If the version of Python you tried previously is earlier than 3.8, that would explain the error.
Check that you don’t have the PYTHONHOME environment variable set. This can interfere with library loading.
You can see that _cubit3.pyd is linked against python38.dll. If the version of Python you’re trying to import Cubit into is less than Python 3.8, it will not work.
Other info:
I can run python scripts from the Cubit GUI, but because of performance I want to run it from the command line or using VSCode or the terminal. I have set the environment in VSCode to run the Python from Cubit and am getting the right version, but still cannot import cubit.
Can you load into the python interpreter in the cmd line using the Cubit python? Are you still unable to load cubit that way? I’m not sure what else VS Code may be doing that might interfere with Cubit.
I was able to get into the cmd line interpreter using the Cubit python, and it still didn’t work that way, looking at the path we noticed that we weren’t actually loading all the paths we were expecting to. We were actually debugging it here so we got two solutions that worked for us, here for posterity:
Option 1
In Windows, add a PYTHONPATH variable with the path to the Cubit bin directory: “C:\Program Files\Coreform Cubit 2021.5\bin”
Option 2
This will only work for the session or for the script and is only necessary if Option 1 is not done above: