Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install python modules in blender

Tags:

python

blender

I've been trying to install pyserial for blender, but I can only install it to python32 on my C drive, is there anything i can do to have it install to blender or have blender import from python32

like image 704
Michael Balmes Avatar asked Jun 22 '12 18:06

Michael Balmes


People also ask

How do I install a python module?

The best and recommended way to install Python modules is to use pip, the Python package manager. Otherwise: Download get-pip.py from https://bootstrap.pypa.io/get-pip.py. Run python get-pip.py.

How do I enable python in Blender?

In the Preferences, there is the toggle to Auto Run Python Scripts. This means the Trusted Source option in the File Browser will be enabled by default, and scripts can run when blend-files are loaded without using the File Browser.


2 Answers

Blender has its own python installation and libraries. You can try to install your packages to blender directly. My dir for example: ...\Blender 2.63\2.63\scripts\modules

Otherwise, you can always hardcode the paths directly in your code with sys.path.append("...")

More info about installing modules available here, read about python setup.py install --home=<dir>

like image 72
peko Avatar answered Sep 18 '22 17:09

peko


For windows, with no special permissions, and from blender python script only:

  1. Install package you want from blender script (tqdm for example given below):

    import pip
    pip.main(['install', 'tqdm', '--user'])
    
  2. From blender console watch the path where pip actually installs packages in your configuration (WARNING: The script tqdm.exe is installed in 'C:\Users\<Username>\AppData\Roaming\Python\Python39\Scripts' which is not on PATH):

    Blender console, actial installing package location

  3. In blender script add the path where your blender's pip installs packages to PATH:

    import sys
    packages_path = "C:\\Users\\<Username>\\AppData\\Roaming\\Python\\Python39\\Scripts" + "\\..\\site-packages"
    sys.path.insert(0, packages_path )
    
  4. Successfully import your package in the script:

    import tqdm
    

Update 1

To show Blender terminal in v2.93 click Window -> Toggle System Console

enter image description here

Update 2

The whole script

# 1. launch in blender python interpreter

import pip
pip.main(['install', 'tqdm', '--user'])

# 2. watch blender's python path in console output at this moment
# 3. insert it to packages_path below
# 4. uncomment the next code and launch script in blender interpreter again

# import sys
# packages_path = "C:\\Users\\<Username>\\AppData\\Roaming\\Python\\Python39\\Scripts" + "\\..\\site-packages" # the path you see in console
# sys.path.insert(0, packages_path )
# import tqdm
like image 26
Ornstein89 Avatar answered Sep 18 '22 17:09

Ornstein89