How to install python modules in blender

Question:

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

Asked By: Michael Balmes

||

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.632.63scriptsmodules

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>

Answered By: peko

make a permanent link of your python (3.5 and above needed) and replace your python directory in blender to directly use your systems python in blender…

U need to run cmd as administrator (use right click on the item)

D:Blender FoundationBlender2.77>mv python python_old
D:Blender FoundationBlender2.77>mklink /j python d:Anaconda2envspy3
Junction created for python <<===>> d:Anaconda2envspy3
Answered By: ntg

If you are on Windows you can just do python setup.py install as usual using the python interpreter given by blender. So for example, 'c:/Program Files/Blender Foundation/Blender/2.78/python/bin/python.exe' setup.py install.

On Linux, I think the native python3 interpreter is used so there is no problem of this kind.

Answered By: god

After a lot of search and experiments, I have found this solution:

  1. give all permissions to python folder in Blender installation
  2. download get-pip.py and install it with the Blender’s internal python executable
  3. now you can install any modules by using the internal pip: …bin>python.exe -m pip install module_name

More details are described here: https://blender.stackexchange.com/questions/218486/installing-pythonnet-in-blender?noredirect=1#comment368756_218486

Answered By: igmar

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>AppDataRoamingPythonPython39Scripts' which is not on PATH):

    Blender console, actial 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 next 2 lines of code in blender python interpreter

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

import sys

# 2. watch blender's python path in console output at this moment
# 3. insert the path to packages_path below and uncomment

# packages_path = "C:\Users\<Username>\AppData\Roaming\Python\Python39\Scripts" + "\..\site-packages" # the path you see in console

# 4. uncomment the next code and launch script in blender interpreter again

# sys.path.insert(0, packages_path )
# import tqdm

# use installed packages here
Answered By: Ornstein89

Move the python file into a zip. Find the zip file from

Blender > Preferences > Add-on > Install

It should then show up in the list in the Add-on page.

Done.

Answered By: phyatt

The following commands work fine for installing numpy package when done on windows.

import sys
import pip
pip.main(['install', 'numpy', '--target', (sys.exec_prefix) + '\lib\site-packages'])

To avoid permission issues, ensure that Blender application is Run as administrator.

The following works as well

import site
import pip
pip.main(['install', 'numpy', '--target', site.USER_SITE])
Answered By: SakSath

I faced a similar problem but on Linux. Blender in Linux has its own Python interpreter and its pip installer, it does not use the default installed versions always. This can be controlled by changing the PATH environment variable.
Here is a script that we will be launched first from a regular Python interpreter and then from Blenders Python interpreter:

import sys
print(sys.path)

When we launch it from the default Python interpreter we will get the following output:

python3 test.py
['/app/srcs/blender/srcs', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages']

When I run the same file using Blender it looks different:

blender --background --python test.py
['/blender/3.5/scripts/startup', '/blender/3.5/scripts/modules', '/blender/3.5/python/lib/python310.zip', '/blender/3.5/python/lib/python3.10', '/blender/3.5/python/lib/python3.10/lib-dynload', '/blender/3.5/python/lib/python3.10/site-packages', '/blender/3.5/scripts/freestyle/modules', '/blender/3.5/scripts/addons/modules', '/config/.config/blender/3.5/scripts/addons/modules', '/blender/3.5/scripts/addons']

As you noticed, the path to the default Python (usr/bin/python3) is not even included, instead, it includes the local Python interpreter and local packages.
You could add your own path to your Python environment or virtual environment using sys.path.append("<path>") or even import the pip module as someone suggested, however, these methods are not ideal, and you will face problems when you import some modules.
The last method is even not recommended by Blender itself, if you check the file /blender/3.5/python/lib/python3.10/site-packages/pip/_internal/cli/main.py, it says:

Do not import and use main() directly! Using it directly is actively
discouraged by pip’s maintainers. The name, location and behavior of
this function is subject to change, so calling it directly is not
portable across different pip versions.

In addition, running pip in-process is unsupported and unsafe. This is
elaborated in detail at
https://pip.pypa.io/en/stable/user_guide/#using-pip-from-your-program.
That document also provides suggestions that should work for nearly
all users that are considering importing and using main() directly.

However, we know that certain users will still want to invoke pip
in-process. If you understand and accept the implications of using pip
in an unsupported manner, the best approach is to use runpy to avoid
depending on the exact location of this entry point.

The following example shows how to use runpy to invoke pip in that
case:

sys.argv = ["pip", your, args, here]
runpy.run_module("pip", run_name="main")

Note that this will exit the process after running, unlike a direct
call to main. As it is not safe to do any processing after calling
main, this should not be an issue in practice.

So it recommends using the "runpy" module instead, which implements the "-m" command. So pip itself is a module that can be executed by Python without being imported:

# add your own path and Python version.
/blender/3.5/python/bin/python3.10 -m pip install <module>
# or (check freeze for more details on this method)
/blender/3.5/python/bin/python3.10 -m pip install -r requirements.txt

That is how you can add a 3rd party libraries to the Blenders Python environment.
One more thing about modules, it seems that Python interpreter by default adds the path where the script is launched, which is why we can import our own project packages from our script using just import <my_package>. But when the script is launched by Blender, Python doesn’t add that path, as a result, you get an error that indicates that the module is not found, in this case, you should add the path where the script was launched manually using sys.path.append("<my_path>").

I hope this will save time for those who use Linux, it takes some time to investigate, please fill free to correct me, if I missed something.

Answered By: fflores

One way to have a python module available to Blender is to create a virtual environment and run Blender from that environment. On PowerShell, for example:

python -m venv .venv
.venv/Scripts/activate.ps1
pip install <package>
blender

This creates a new virtual environment and runs Blender. Subsequent launches of Blender would only require the second and fourth lines.

Note: you may need to adjust the virtual environment activation depending on your OS/shell. For example, from bash you would use source .venv/bin/activate.

Inside Blender if you check, you will see the virtual environments site packages directory is available.

import site
site.getsitepackages()

[‘//.venv/lib/python3.10/site-packages’,…]

Of course, you will have to ensure that the python that you use to make the virtual environment is compatible with the version of Python in Blender. Also, you will need to activate the virtual environment and launch Blender in this manner every time you want the package available.

Answered By: Swoogan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.