Overriding numpy

Hey all,

I’m trying to import then pandas module which requires Numpy 1.12 or greater. I have Numpy 1.16 on laptops python path, but the version TD comes installed with is 1.11.

I was looking in the docs about overriding built in modules, but I’m still confused and couldn’t get it to work - derivative.ca/wiki088/index. … ng_Modules

Does anyone know of a way I can get TD to use the Numpy version on my system, rather than the numpy built into TD?

Thanks in advance!

You could try changing the sys.path so that the path to your numpy version is before the path to our numpy version. Does that work?

Hey Malcom,

Could you be more specific as to how to do that? sys.path just yields a list of various python related folders, along with the site packages TD uses - it doesn’t seem to go to the module level.

see the chapter “Importing Modules” on the “introduction to Python Tutorial” page:
(more specifically the last paragraph titled “Overriding built in modules”)
docs.derivative.ca/Introduction … ng_Modules

Hey nettoyeur,

That article was what I was referring to in my original post - I read it, but I’m still confused as to how to actually implement it.

I ran sys.path.remove('C:\\Program Files\\Derivative\\TouchDesigner099\\bin\\lib\\site-packages')

and got a value error saying ValueError: list.remove(x): x not in list

I printed out sys.path and sure enough the site-packages path wasn’t in there - which confused me even more. If my the default TD sitepackages path isn’t in sys.path, then how is TD importing the default modules like cv2 and numpy?

I also tried


import sys
mypath = "C:\\Users\\myusername\\Desktop\\Python37\\Lib\\site-packages\\numpy"
sys.path.insert(0, mypath)
import numpy
print(sys.path)
print(numpy.__version__)

Which did put numpy version 1.16 first in the sys.path, but when I printed numpy.__version it yielded version 1.11 - which is the TD default version

Try without the numpy portion:

import sys
sys.path.insert(0, "C:/Python35-x64/Lib/site-packages")
import numpy
print(numpy.__version__)

This works for me.

(I’ve updated the wiki!)

Cheers

Hello,

I still got this problem, I’m struggling to override the Numpy 1.16.2 from TD (last Official built 2021.16410) with a more recent version of Numpy? I tried so many things without success.

I consciously follow the solution of this thread

And the precious tutorial from @JetXS : Anaconda - Managing Python Environments and 3rd-Party Libraries in TouchDesigner | Derivative

I’ve got my custom python path at the beginning of sys.path (and at the end as well as I added it in preference / Python 64-bit module path.

'C:/Users/PC/anaconda3/envs/td-demo/Lib/site-packages', 
'C:\\Program Files\\Derivative\\TouchDesigner\\bin', 
'C:\\Program Files\\Derivative\\TouchDesigner\\bin\\python37.zip', 
'C:\\Program Files\\Derivative\\TouchDesigner\\bin\\DLLs', 
'C:\\Program Files\\Derivative\\TouchDesigner\\bin\\lib',
 'C:\\Program Files\\Derivative\\TouchDesigner\\bin', 
'C:\\Program Files\\Derivative\\TouchDesigner\\bin\\lib\\site-packages', 
'C:/Program Files/Derivative/TouchDesigner/Config/Cmd']
'C\\Users\\PC\\anaconda3\\envs\\td-demo\\Lib\\site-packages'

But when I import numpy it’s always the Touchdesigner’s 1.16.2 version and not the 1.21.5 in my custom site-package directory.

The addition of new modules is working, but not the overriding!

(I worked around the problem in a dirty way on mac by copying the directory of the recent version of numpy directly into the original ToucheDesigner “site-packages” directory, but it doesn’t work on PC)

Is someone have the same problem?

Thank you very much

Louk

Hey @Infratonal

Could you please share your import script?

Is that the same, or something similar to what’s in the Anaconda tutorial ? Do you trigger it in an Execute DAT onStart ?

Best,
Michel

Hello Michel

Thank you for your quick answer,
Here’s the script, it’s coming from yours in a Execute DAT onStart :

import os
import sys
import numpy

def onStart():
	for each in sys.path:
		print(each)
	
	print ("- - - - - - - - -")
	
	custom_path = 'C:\\Users\\PC\\anaconda3\\envs\\td-demo'
	package_path = custom_path + '\\Lib\\site-packages'
	dll_path = custom_path + '\\DLLs'
	library_path = custom_path + '\\Library\\bin'
	
	os.environ['PATH'] = dll_path + os.pathsep + os.environ['PATH']
	os.environ['PATH'] = library_path + os.pathsep + os.environ['PATH']
	sys.path = [package_path] + sys.path
	print('loaded')

	print ("- - - - - - - - -")

	for each in sys.path:
		print(each)
	
	print ("- - - - - - - - -")
	
	import numpy
	print (numpy.__version__)

	print ("- - - - - - - - -")

	return

The Textport Result

python >>> 
C:\Program Files\Derivative\TouchDesigner.2021.16410\bin
C:\Program Files\Derivative\TouchDesigner.2021.16410\bin\python37.zip
C:\Program Files\Derivative\TouchDesigner.2021.16410\bin\DLLs
C:\Program Files\Derivative\TouchDesigner.2021.16410\bin\lib
C:\Program Files\Derivative\TouchDesigner.2021.16410\bin
C:\Program Files\Derivative\TouchDesigner.2021.16410\bin\lib\site-packages
C:/Program Files/Derivative/TouchDesigner.2021.16410/Config/Cmd
C:/Users/PC/anaconda3/envs/td-demo/Lib/site-packages
- - - - - - - - -
loaded
- - - - - - - - -
C:\Users\PC\anaconda3\envs\td-demo\Lib\site-packages
C:\Program Files\Derivative\TouchDesigner.2021.16410\bin
C:\Program Files\Derivative\TouchDesigner.2021.16410\bin\python37.zip
C:\Program Files\Derivative\TouchDesigner.2021.16410\bin\DLLs
C:\Program Files\Derivative\TouchDesigner.2021.16410\bin\lib
C:\Program Files\Derivative\TouchDesigner.2021.16410\bin
C:\Program Files\Derivative\TouchDesigner.2021.16410\bin\lib\site-packages
C:/Program Files/Derivative/TouchDesigner.2021.16410/Config/Cmd
C:/Users/PC/anaconda3/envs/td-demo/Lib/site-packages
- - - - - - - - -
1.16.2
- - - - - - - - -
python >>>

I even tried to remove every predefined path in sys.path and add mine, but it still does not override the old numpy (it’s like any change to sys.path is not taken into account for override).

Maybe I’m forgetting something?

Louk

At the top of the execute DAT, remove import numpy.

Remove the import numpy during the onStart call as well.

Save and restart the project.

In the textport or in a text DAT, add import numpy and then call numpy.__version__, do you now get the version you expect ?

Great it works !

Ok, I don’t have to call the library I want to override on execute start !

Thank you very much Michel !

BTW : Your Anaconda tutorial (Anaconda - Managing Python Environments and 3rd-Party Libraries in TouchDesigner | Derivative) is very clear and useful to manage python environments!

1 Like