How to install PySide2 in Blender Python

GOAL

To use PySide/PySide2 UI in Blender Addon.

In this article PySide2 UI is used independently. PySide is not embedded in Blender custom UI or include Blender custom UI inside.

*This method is informal and I’m not responsible for any problems that may arise.

Environment

Blender 2.83 (Python 3.7.4)
Windows 10
PySide2 5.15.2

Method

If you’d like to see what is PySide, please check the article “First PySide Application” firsy.

Method 1. Add path to the local package

Warning: This method may cause confliction of modules.

1. Install PySide2 in the local directory

Install PySide or PySide2 in your local directory. In my case, the module “PySide2” is in the directory “Python38/Lib/site-packages”.

2. Add path to the PySide2

Add local python path with sys.path.insert().

import pathlib
import sys

my_python_path = "C:/Users/<USER_NAME>/AppData/Local/Programs/Python/Python38/Lib/site-packages"
sys.path.insert(0, my_python_path)

import PySide2
print(PySide2.__version__)
# output -> 5.15.2

To add path automatically, create start up file as below and save it in the startup directory (C:\Users\<USER_NAME>\AppData\Roaming\Blender Foundation\Blender\2.83\scripts\startup).

import sys

def add_package_path():
    my_python_path = "C:/Users/<USER_NAME>/AppData/Local/Programs/Python/Python38/Lib/site-packages"
    sys.path.insert(0, my_python_path)
    print("You can use local package")

def register():
    add_package_path()

def unregister():
    pass

Method 2. Install PySide2 into Blender Python directly.

Warning: This method may destroy blender by command input mistake.

Run command prompt as administrator. Then install PySide2 by using pip install command with -t or –target option.
Reference: pip documentation v21.0

>pip install PySide2 -t "C:\Program Files\Blender Foundation\Blender 2.83\2.83\python\lib\site-packages"

You can import PySide2 in the text editor of Blender.

import PySide2
print(PySide2.__version__)
#output -> 5.15.2

postscript

If you don’t have pip in site-packages directory of Blender Python. Download get-pip.py on pypa.io. Then run get-pip.py to install pip as below.

>python get-pip.py