Anaconda - Managing Python Environments and 3rd-Party Libraries in TouchDesigner - 2022-05-02 09:15

Anaconda - Managing Python Environments and 3rd-Party Libraries in TouchDesigner

Link to main site

1 Like

Hello! What do you mean by “MacOS users should include path to .dlybs / MacOS binaries” here? Forgive my ignorance…

Hey @saimgulay

Apologies - this was not tested on Mac at the time of writing.

A couple things you could follow…

After using a command like conda create -n td-demo python=3.9.5

As per Anaconda’s official doc:

  1. Open a terminal window.
  2. If you want the location of a Python interpreter for a conda environment other than the root conda environment, run conda activate environment-name.
  3. Run which python.

From there, the result should tell you where is the python interpreter for that environment, on your mac.

image

Looking at this screenshot, a default installation / new environment should give you an environment path similar to /Users/YOUR_USERNAME/opt/anaconda3/envs/YOUR_ENVIRONMENT_NAME

Looking at that conda environment folder, the two folders that are likely to be of interest are bin and lib.

In an Anaconda terminal, just starting the python interpreter of the environment by typing python, we import a package of that environment… in my case I tried with NumPy.

(td-demo) micheldidier@Michels-Mac-mini ~ % python
Python 3.9.5 (default, May 18 2021, 12:31:01) 
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy
<module 'numpy' from '/Users/micheldidier/opt/anaconda3/envs/td-demo/lib/python3.9/site-packages/numpy/__init__.py'>
>>> numpy.__version__
'1.22.3'

We see with the commands above where is my site-packages folder.

With all that extra info, you could update the script to be:

import sys
import os
import platform

def onStart():
	user = 'YOUR_USERNAME ' # Update accordingly
 
	condaEnv = 'YOUR_ENVIRONMENT_NAME' # Update accordingly
 
	if platform.system() == 'Windows':
		if sys.version_info.major >= 3 and sys.version_info.minor >= 8:
			"""
			Double check all the following paths, it could be that your anaconda 'envs' folder is not in your User folder depending on your conda install settings and conda version.
			"""
			os.add_dll_directory('C:/Users/'+user+'/anaconda3/envs/'+condaEnv+'/DLLs')
			os.add_dll_directory('C:/Users/'+user+'/anaconda3/envs/'+condaEnv+'/Library/bin')
		else:
			"""
			Double check all the following paths, it could be that your anaconda 'envs' folder is not in your User folder depending on your conda install settings and conda version.
			"""
			# Not the most elegant solution, but we need to control load order
			os.environ['PATH'] = 'C:/Users/'+user+'/anaconda3/envs/'+condaEnv+'/DLLs' + os.pathsep + os.environ['PATH']
			os.environ['PATH'] = 'C:/Users/'+user+'/anaconda3/envs/'+condaEnv+'/Library/bin' + os.pathsep + os.environ['PATH']
 
		sys.path = ['C:/Users/'+user+'/anaconda3/envs/'+condaEnv+'/Lib/site-packages'] + sys.path
	
	else:
		"""
		MacOS users should include path to .dlybs / MacOS binaries, site-packages
		"""
		os.environ['PATH'] = '/Users/'+user+'/opt/anaconda3/envs/'+condaEnv+'/lib' + os.pathsep + os.environ['PATH']
		os.environ['PATH'] = '/Users/'+user+'/opt/anaconda3/envs/'+condaEnv+'/bin' + os.pathsep + os.environ['PATH']
		sys.path = ['/Users/'+user+'/opt/anaconda3/envs/'+condaEnv+'/lib/python3.9/site-packages'] + sys.path
 
	return

Saving our file with the above code in the Execute DAT (and its Start par toggled ON), we restart the project.

We test those few commands in the textport:

We can confirm that our PATH was updated properly. Importing numpy, typing numpy and numpy.__version__ we can confirm we now run a newer version of NumPy, from the Conda environment.

I hope this helps.

I will update the Anaconda article.

NOTE: You might encounter issues if you are on an M1 Mac, and run different architectures. If you are running the native ARM build of TouchDesigner, your Anaconda install, Anaconda environment and libraries should all be the Native ARM versions as well. More details are available with documentation for Homebrew here: Category:Python - Derivative

1 Like

Dear @JetXS
Thanks for detailed reply. I will try those solutions as soon as possible. Also thanks for the work. :slight_smile:

Edit: I tried and everything worked very well so far! Thx again!!!

1 Like

Hello,

May I also ask if it’s possible to update the Python version TD is currently using? Mine is using 3.9.5, but the program I want to use in TD requires 3.10.

@saimgulay We only consider changes to the built-in Python version for TouchDesigner in new branches, so we won’t be changing it for 2022 Official.

1 Like

Dear @JetXS
Are you aware of any option for mac m1/m2?
I’ve tried all these options but none works.

Hey @Tommella

This comment here is listing the solution on MacOS, look at the note at the very bottom. Process is similar but you use ARM libs for everything: Anaconda - Managing Python Environments and 3rd-Party Libraries in TouchDesigner - 2022-05-02 09:15 - #4 by JetXS

The Homebrew solution here was also tested on ARM: Category:Python - Derivative

Best,
Michel

Hello guys!

Is anyone awre of a way i can change the python version in TD? I wanna run Stylegan-t but the required Python version is 3.9…

Best regards
Simon

Hi @simoher,

the version of the python interpreter in TouchDesigner cannot be changed.

cheers
Markus