BUG?: venv.EnvBuilder.create() works on console but fails in TD

This works fine in the console

import venv
builder = venv.EnvBuilder(with_pip=True)
builder.create(".venv")   

but fails when run in TD

python >>> 
Traceback (most recent call last):
  File "</engine/admin/Lib/tools/rtfm/test:op('/engine/admin/Lib/tools/rtfm/test').run()>", line 1
td.Error: File "/engine/admin/Lib/tools/rtfm/test", line 7
  File "C:\Program Files\Derivative\TouchDesigner.2020.23680\bin\lib\venv\__init__.py", line 70, in create
    self._setup_pip(context)
  File "C:\Program Files\Derivative\TouchDesigner.2020.23680\bin\lib\venv\__init__.py", line 240, in _setup_pip
    subprocess.check_output(cmd, stderr=subprocess.STDOUT)
  File "C:\Program Files\Derivative\TouchDesigner.2020.23680\bin\lib\subprocess.py", line 395, in check_output
    **kwargs).stdout
  File "C:\Program Files\Derivative\TouchDesigner.2020.23680\bin\lib\subprocess.py", line 472, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Program Files\Derivative\TouchDesigner.2020.23680\bin\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Program Files\Derivative\TouchDesigner.2020.23680\bin\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Results of run operation resulted in exception.

I don’t think the virtual environments are currently supported - though it sure would be cool if they were.

In this case it due to a python (bug) in 3.7.2 where python.exe is not copied to the venv/scripts dir when using EnvBuilder.setup_python(). If you copy lib/venv from 3.7.3 to TD/bin/lib and run

# create a venv from within TD 
# !!! need to copy venv from 3.7.3 to TD/bin/lib !!!!
import venv
import os

builder = venv.EnvBuilder(with_pip=True)
env_dir = os.path.abspath('.venv')
context = builder.ensure_directories(env_dir)

# fix touchdesigner.exe is python.exe 
context.env_exe = context.env_exe.replace("TouchDesigner.exe","Python.exe") 

builder.create_configuration(context)
builder.setup_python(context)
builder.setup_scripts(context)
builder.post_setup(context)
builder._setup_pip(context)


# test pip import sphinx
import subprocess
import shlex
pip = project.folder + "/.venv/Scripts/pip"
output = subprocess.run(shlex.split(f"{pip} install pandas"), capture_output=True, text=True)
print(output.stderr)
print(output.stdout)

you can create a venv from within TD (if you dont wanna use subprocess calls ). That doesn’t make the venv usable in the traditional way . To use any libraries in the venv you still have to do: sys.path.insert(0,".venv/Lib/site-packages")

@ derivative: is TD/lib/python.exe is a fully valid python.exe? using it to python -m pip install seems to work just fine

Yes, it should be fully valid python. That’s just what comes out of compiling Python.

How about now? I am curious about because I want to set-up install python package automatically in touchdesigner via venv.