How should I import torch?
I’m on 2021.39010 on Windows.
I installed Python 3.9 to C:/Python39.
I installed torch version 1.10.2+cu113 and can import it outside of TouchDesigner.
Inside TouchDesigner, I have a DAT:
import sys
custompath = "C:/Python39/Lib/site-packages"
if sys.path[0] != custompath:
sys.path.insert(0, custompath)
custompath = "C:/Python39/Lib"
if sys.path[1] != custompath:
sys.path.insert(1, custompath)
#sys.path.remove('C:\\Program Files\\Derivative\\TouchDesigner.2021.39010\\bin\\lib\\site-packages')
for a in sys.path:
print(a)
import torch
output:
C:/Python39/Lib/site-packages
C:/Python39/Lib
C:\Program Files\Derivative\TouchDesigner.2021.39010\bin
C:\Program Files\Derivative\TouchDesigner.2021.39010\bin\python39.zip
C:\Program Files\Derivative\TouchDesigner.2021.39010\bin\DLLs
C:\Program Files\Derivative\TouchDesigner.2021.39010\bin\lib
C:\Program Files\Derivative\TouchDesigner.2021.39010\bin
C:\Program Files\Derivative\TouchDesigner.2021.39010\bin\lib\site-packages
C:/Program Files/Derivative/TouchDesigner.2021.39010/Config/Cmd
C:/Python39/Lib/site-packages
C:/Python39/Lib/site-packages
Traceback (most recent call last):
File "</project1/text1:op('/project1/text1').run()>", line 1
td.tdError: File "/project1/text1", line 16
r = previousimport(*args, **kw)
File "C:\Python39/Lib/site-packages\torch\__init__.py", line 16
import ctypes
r = previousimport(*args, **kw)
File "C:\Python39/Lib\ctypes\__init__.py", line 8
from _ctypes import Union, Structure, Array
r = previousimport(*args, **kw)
ImportError: DLL load failed while importing _ctypes: The specified module could not be found.
Results of run operation resulted in exception.
python >>>
hey @DavidBraun , at first sight this looks like the same bug from this thread where importing ctypes does not work in experimental.
This should be fixed in 2021.39070 and later.
Hi
I tried to import torch with the official version.
I had same errors but I modified paths in a file in torch site package (I don’t remember which cause I don’t have my computer) I think it was init.py cause when I print the paths to dlls it tries to go in touchdesigner dlls folder. After modifications ( I hard coded paths in that file to try) torch imported more dlls but failed to import all but I think that should work. It failed for me cause I think I installed cuda 11.4 to use onnx runtime, but torch waits cuda 11.3…
You can try change the paths, maybe that should work…
Thanks. On 2022.20150 Windows I’m able to import ctypes but import torch results in a hang without anything printed to textport or console, or saved to Desktop or .dmp.
I’ve tried the script in my first post and also separately just import torch. Both freeze TouchDesigner in the same way.
My Windows system PATH includes both C:/Python39 and C:/Python39/scripts. My TouchDesigner preferences for Python 64-bit Module Path is “C:/Python39/Lib/site-packages”
@DavidBraun Can you add those to an onStart() Exec DAT and see if that helps?
import os
import sys
os.add_dll_directory('C:\\Python39\\DLLs')
os.add_dll_directory('C:\\Python39\\Library\\bin') # If you have this folder
sys.path = ['C:\\Python39\\Lib\\site-packages'] + sys.path # Optional if the first 2 lines here are not enough, disable your TD Module Path preferences though
If that works or help at all, or bring another issue please let me know.
This search path is used when resolving dependencies for imported extension modules (the module itself is resolved through sys.path), and also by ctypes.
Remove the directory by calling close() on the returned object or using it in a with statement.
See the Microsoft documentation for more information about how DLLs are loaded.
Raises an auditing event os.add_dll_directory with argument path.
Availability: Windows.
New in version 3.8: Previous versions of CPython would resolve DLLs using the default behavior for the current process. This led to inconsistencies, such as only sometimes searching PATH or the current working directory, and OS functions such as AddDllDirectory having no effect.
In 3.8, the two primary ways DLLs are loaded now explicitly override the process-wide behavior to ensure consistency. See the porting notes for information on updating libraries.
If you give it a try, make sure to use the following when installing Pytorch in a new conda env (the version of that conda env should match TD’s python version, just to be safe)
yes python import torch works outside TouchDesigner.
I tried miniconda with 3.9.5 and 2022.20150 but it froze when importing torch.
Then I tried miniconda with 3.7.2 and 2021.16410 and everything is working.
Also, in the tutorial you shared, there’s a typo with > instead of “>”.
Yes I’m only trying import torch. My GPU is NVIDIA RTX 2080 with driver 511.79. Maybe I’ll try a clean re-install of Python and TouchDesigner at some point.
I’m having a similar issue with pytorch + cuda on python 3.11 and td 2023.12.
seems to work fine without gpu but if I try to use pytorch + cuda I get error that seems to be common.
OSError: [WinError 127] The specified procedure could not be found. Error loading "C:\Users/davis/anaconda3/envs/td-demo-ptorch/Lib/site-packages\torch\lib\shm.dll" or one of its dependencies.
Have tried several of the work arounds but they don’t work.