Michael Balmes
Michael Balmes

Reputation: 334

How to install python modules in 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

Upvotes: 16

Views: 51898

Answers (10)

KNTY
KNTY

Reputation: 457

Step-by-Step Guide How to Install Your Packages in Blender Using the Interpreter

  1. Find Blender's Python Executable: Blender comes with its own bundled Python interpreter. You need to find where this interpreter is located.

    • Open Blender.

    • Go to the Scripting tab.

    • In the Python console within Blender, run the following command to find the path to the Python executable:

      import sys
      print(sys.executable)
      

    This will print the path to Blender's Python executable, which will look something like /path/to/blender/2.93/python/bin/python.

  2. Open Command Prompt or Terminal: Open your command prompt (Windows) or terminal (Mac/Linux).

  3. Navigate to Blender's Python Executable Directory: Navigate to the directory containing Blender's Python executable. For example:

    cd /path/to/blender/2.93/python/bin
    
  4. Install the Package Using Blender's Python: Use Blender's Python executable to install the cryptography package (or any other required package). Replace path/to/python with the actual path you obtained in step 1.

    ./python -m ensurepip
    ./python -m pip install cryptography python-dotenv requests
    

Windows

Let's say the path to Blender's Python executable on Windows is C:\Program Files\Blender Foundation\Blender 2.93\2.93\python\bin\python.exe.

  1. Open Command Prompt.

  2. Run the following commands:

    cd "C:\Program Files\Blender Foundation\Blender 2.93\2.93\python\bin"
    python.exe -m ensurepip
    python.exe -m pip install cryptography python-dotenv requests # Example
    

MacOS/Linux

For macOS or Linux, if the path to Blender's Python executable is /Applications/Blender.app/Contents/Resources/2.93/python/bin/python3.7m

  1. Open Terminal.

  2. Run the following commands:

    cd /Applications/Blender.app/Contents/Resources/2.93/python/bin
    ./python3.7m -m ensurepip
    ./python3.7m -m pip install cryptography python-dotenv requests #Example
    

Verify Installation in Blender

After installing the packages, you can verify the installation within Blender:

  1. Open Blender.

  2. Go to the Scripting tab.

  3. In the Python console, try importing the installed packages:

    import cryptography
    import dotenv
    import requests
    

If there are no errors, the packages have been successfully installed and are ready to use in your Blender scripts.

Note

  • Ensure you have write permissions in the directory where Blender is installed, as you'll need them to install packages.
  • If you encounter any permission issues, you might need to run your command prompt or terminal as an administrator/superuser.

Upvotes: 1

SakSath
SakSath

Reputation: 149

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])

Upvotes: 4

Ornstein89
Ornstein89

Reputation: 676

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 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

Upvotes: 14

Swoogan
Swoogan

Reputation: 5558

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.

Upvotes: 0

fflores
fflores

Reputation: 452

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.

Upvotes: 2

phyatt
phyatt

Reputation: 19152

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.

Upvotes: 1

peko
peko

Reputation: 507

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>

Upvotes: 8

igmar
igmar

Reputation: 51

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

Upvotes: 1

god
god

Reputation: 1708

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.

Upvotes: 1

ntg
ntg

Reputation: 14155

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 Foundation\Blender\2.77>mv python python_old
D:\Blender Foundation\Blender\2.77>mklink /j python d:\Anaconda2\envs\py3
Junction created for python <<===>> d:\Anaconda2\envs\py3

Upvotes: 0

Related Questions