├── README.md └── install_blender_python_module.py /README.md: -------------------------------------------------------------------------------- 1 | # Install any python module for Blender python 2 | 3 | 4 | If you are a Blender user you might also wonder why the module you just installed for python didn't work in the Blender python console or in the python script you wrote in Blender. 5 | 6 | The reason for this is that Blender has its own python version and python libraries "inside" which it is using. 7 | 8 | So to make it work in Blender too, you have to install the modules you are using for the python version in Blender as well. 9 | 10 | This little script shall simplify this task for you. 11 | 12 | Just copy and paste the code in your text editor in Blender, then just change this one line here: 13 | 14 | installModule("pandas") 15 | 16 | and replace "pandas" with whatever module you want to install. 17 | 18 | Then run the script. 19 | 20 | 21 | 22 | 23 | 24 | Small remark: 25 | 26 | I am working on MacOS. So i am pretty sure it works on MacOS. I tried to implement the functionality for Linux and Windows as well. Unfortunately i couldn't test that. If there are errors, please open an issue and i try to fix that. Thank you. 27 | 28 | 29 | --- 30 | 31 | 32 | If you want to support my work or buy me a coffee: 33 | 34 | [![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/paypalme/christophduyster) 35 | 36 | -------------------------------------------------------------------------------- /install_blender_python_module.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import subprocess 3 | import os 4 | import platform 5 | import bpy 6 | 7 | def isWindows(): 8 | return os.name == 'nt' 9 | 10 | def isMacOS(): 11 | return os.name == 'posix' and platform.system() == "Darwin" 12 | 13 | def isLinux(): 14 | return os.name == 'posix' and platform.system() == "Linux" 15 | 16 | def python_exec(): 17 | 18 | if isWindows(): 19 | import sys 20 | return os.path.join(sys.prefix, 'bin', 'python.exe') 21 | elif isMacOS(): 22 | import sys 23 | try: 24 | # 2.92 and older 25 | path = bpy.app.binary_path_python 26 | except AttributeError: 27 | # 2.93 and later 28 | path = sys.executable 29 | return os.path.abspath(path) 30 | elif isLinux(): 31 | import sys 32 | return os.path.join(sys.prefix, 'bin', 'python3.11') 33 | else: 34 | print("sorry, still not implemented for ", os.name, " - ", platform.system) 35 | 36 | 37 | def installModule(packageName): 38 | python_exe = python_exec() 39 | try: 40 | subprocess.call([python_exe, "import ", packageName]) 41 | except: 42 | # upgrade pip 43 | subprocess.call([python_exe, "-m", "ensurepip"]) 44 | subprocess.call([python_exe, "-m", "pip", "install", "--upgrade", "pip"]) 45 | # install required packages 46 | subprocess.call([python_exe, "-m", "pip", "install", packageName]) 47 | 48 | installModule("pygame") 49 | --------------------------------------------------------------------------------