I found the step by step description here, seemed a bit cumbersome, so I automated it, at least for a Windows environment. Comments welcome:
#https://github-wiki-see.page/m/DynamoDS/Dynamo/wiki/Customizing-Dynamo%27s-Python-3-installation
import platform, os, subprocess, importlib.util, sys
from System import Environment
from System.Net import WebClient
dirName = f'python-{platform.python_version()}-embed-amd64'
localappdatapath = os.getenv(r'LOCALAPPDATA')
dirPath = os.path.join(localappdatapath, dirName)
assert os.path.isdir(dirPath)
bootstrapPipFilePath = os.path.join(dirPath, 'get-pip.py')
if not os.path.isfile(bootstrapPipFilePath):
WebClient().DownloadFile('https://bootstrap.pypa.io/get-pip.py', bootstrapPipFilePath)
pthFilePath = os.path.join(dirPath, 'python38._pth')
assert os.path.isfile(pthFilePath)
replace = False
with open(pthFilePath, 'r') as f:
lines = f.readlines()
if '#' in lines[-1]:
lines[-1] = lines[-1].replace('#','')
replace = True
if replace:
with open(pthFilePath, 'w') as f:
f.writelines(lines)
pipDirPath = os.path.join(dirPath, 'Scripts')
if not os.path.isdir(pipDirPath):
OUT = f'installing pip in local Python environment: {dirPath}'
batPath = os.path.join(localappdatapath,'Temp', 'pipInstall.bat')
with open(batPath, 'w') as f:
f.write(f'cd {dirPath}\n')
f.write('python get-pip.py')
try: subprocess.run(batPath, check = True) #check argument raises exception when an error occurs
except Exception as ex:
OUT += '\n error while installing pip:'
OUT += '\n' + str(ex)
else:
os.remove(batPath)
OUT += '\n pip succesfully installed'
else: OUT = 'pip was already installed for this local Python environment'
installPackages = IN[0]
if installPackages:
sys.path.append(os.path.join(localappdatapath, r'python-3.8.3-embed-amd64\Lib\site-packages'))
for packageName in installPackages:
spec = importlib.util.find_spec( packageName )
if spec is None:
OUT += f'\n installing {packageName} in local Python environment: {dirPath}'
batPath = os.path.join(localappdatapath,'Temp', packageName+'Install.bat')
with open(batPath, 'w') as f:
f.write(f'cd {dirPath}\n')
f.write('.\Scripts\pip install '+packageName)
try: subprocess.run(batPath, check = True) #check argument raises exception when an error occurs
except Exception as ex:
OUT += f'\n error while installing {packageName}:'
OUT += '\n' + str(ex)
else:
os.remove(batPath)
OUT += f'\n{packageName} succesfully installed'
else: OUT += f'\n{packageName} was already installed for this local Python environment'```