Skip to content

Instantly share code, notes, and snippets.

@PranavSK
Created June 27, 2021 20:21
Show Gist options
  • Save PranavSK/32540abc57b56c11b55f97b6a8e83749 to your computer and use it in GitHub Desktop.
Save PranavSK/32540abc57b56c11b55f97b6a8e83749 to your computer and use it in GitHub Desktop.
import os
import sys
def ensure_site_packages(packages):
""" `packages`: list of tuples (<import name>, <pip name>) """
if not packages:
return
import site
import importlib
import importlib.util
user_site_packages = site.getusersitepackages()
os.makedirs(user_site_packages, exist_ok = True)
sys.path.append(user_site_packages)
modules_to_install = [module[1] for module in packages if not importlib.util.find_spec(module[0])]
if modules_to_install:
import subprocess
if bpy.app.version < (2,91,0):
python_binary = bpy.app.binary_path_python
else:
python_binary = sys.executable
subprocess.run([python_binary, '-m', 'ensurepip'], check=True)
subprocess.run([python_binary, '-m', 'pip', 'install', *modules_to_install, "--user"], check=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment