Using PyPI from Blender #71420
Labels
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
28 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#71420
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Proposal
Improve the workflow for installing Python modules from the Python package index PyPI .
This is a design task to evaluate options.
Note:
Motivation
For Python developers to be able to install 3rd party modules into Blender.
While this is possible, it's quite involved:
https://blender.stackexchange.com/questions/56011/how-to-install-pip-for-blenders-bundled-python
This task is to evaluate possible solutions:
Package Compatibility
Windows Python MSVC compatibility. (EDIT: this is no longer an issue).
In the past, Blender's Python was built with a different MSVC version, making binary modules incompatible.
File System Permissions
Possible Solution: Set
PYTHONUSERBASE
to a directory under Blender's own configuration.Added subscriber: @ideasman42
Added subscriber: @JacquesLucke
I support bundling pip with Blender. It would have helped me in quite a few cases. I did not have any issues with binary compatibility after installing it myself yet. That does not mean that there are none of course.
For my vscode extension for Blender development, I had to use pip to install some packages in Blender. It works in most cases, but I remember that it did not work in every case. I would have to dig deeper to figure out the failure cases more precisely.
The code responsible for installing the packages is here: https://github.com/JacquesLucke/blender_vscode/blob/master/pythonFiles/include/blender_vscode/installation.py
This solution does not require any user interaction. Blender only has to be started from within vscode and everything else should happen automatically.
Added subscriber: @JulienDuroure
Added subscriber: @mudu
I would also appreciate to have pip in blender. I'm struggling with using numpy or scipy inside of blender. It would make it so much easier to use scientific data for visualising in blender.
At Airbus Defence & Space, we are using ensurepip (and then pip install) to install 3rd party packages inside Blender, for addon and pipeline development, based on buildin python from blender. Hope we can continue to perform it even on PC without system install of python
@mudu: numpy is already bundled with blender (but scipy not, but can be install with pip as described in stackexchange link.
Added subscriber: @rjg
It would be great if the improved workflow would make use of venv. This would allow a nice replacement for how modules were handled through the Scripts path in the preferences with the Blender specific directory structure.
Added subscriber: @fsiddi
If we consider improving the workflow of how Python modules are installed in Blender, I'd like to make the following proposal:
Instead of installing modules directly into Blender's bundled Python, let the user create, reference and use venvs in Blender's preferences. Blender would create the venv based on the bundled Python and therefore modules installed through pip in that venv would be compatible to Blender's bundled Python. The user could select one of the venvs to use, however Blender doesn't actually load the venv since it already has it's own interpreter running. Instead it checks if the referenced venv interpreter matches the bundled Python version and if it's ok, loads the modules. There could also be a section in the preferences that allow to install modules through pip into the venv within Blender.
The benefits of this approach:
This could possibly replace the Scripts path in the preferences altogether, since custom startup files can already be created through application templates and add-ons would be installed in the add-ons section of the preferences. I know it's more work that simply adding pip, however it may be a cleaner solution in the long run.
BTW a better link for this discussion would be: https://blender.stackexchange.com/questions/5287/using-3rd-party-python-modules.
With
ensurepip
it's actually not really complicated.Added subscriber: @LazyDodo
Good news: No longer an issue, pip works and you can use it to install things !
Bad news: I think by default pip wants to install into the scripts folder, however if blender is installed into
c:\program files\
writing there maybe problematic and require administrator rights, which is guaranteed to be a troublesome in most corporate and educational environments.@LazyDodo You're right pip is already shipped with 2.81. So this task is basically solved?
It looks like we can work-around this problem using
PYTHONUSERBASE
and pip's--user
argument to install packages in a path we define (noted in task).@rjg, I'm not sure if we need a venv if we can define the user-site directory.
While I haven't used venv much, and never dug into it's internals - a user-site directory looks to have most of the advantages noted in your post about venv's.
This comment was removed by @rjg
@ideasman42 If setting the
PYTHONUSERBASE
allows to switch between multiple directories in order to separate the modules per directory from each other and from the system-wide installed modules, then it would be a good solution. It would also have less overhead than a venv.Added subscriber: @MiroHorvath
Added subscriber: @tintwotin
Imagine how useful it would be if pip could be accessed through the API?
Here is a project which attempts to do that: https://github.com/di/pip-api
Added subscriber: @dee909
The ability to use venvs would actualy be really great.
TL;DR
PYTHONUSERBASE
Setting PYTHONUSERBASE and using --user does not quite match:
1) You would need to track which python/blender it has been created by (wheras it's included in venvs)
What works though:
- The "console_scripts" entry points correctly end up iside the USER_BASE.
- Entry points of package from the USER_BASE are correctly seen by pkg_resources.iter_entry_points()
What we can do:
1) Could be fixed by adding a .bat or .sh file pointing to the blender that created the USER_BASE.
2) Feels like whining but it actually scares me. More tests should be done.
3) After a couple hours I couldn't figure this one out :/
4) There is no fix for that.
In summary: while similar in certain features, there are solid reasons for the raise of virtualenv, then venv, in addition to the USER_BASE feature.
It wouldn't feel right to try to user USER_BASE instead of venvs.
VENV
If we could create VENV with blender executable (not the bundled python), the venv would contain blender itself, which fixes 1). We could also use all the PyPA tools, which fixes 2), 3), and 4) wouldn't be a thing anymore.
Let's see what prevents us from doing so:
blender -Im ensurepip --upgrade --default-pip
and it fails because blender command line does not behave like this.My Conclusion
Using blender inside a VENV is really appealing from a pipeline/workflow/dependency-management point of view.
On project spanning 6 to 18 months, using venvs would help keeping up to date on blender versions.
Out of Topic
That being said, if there was an official way to use blender in a venv (not just a community thing), we would be able to elaborate an official way to publish/install addons to/from PyPI, which would give us free addon dependencies \o/.
Full disclosure: this is something I'm working on on my (non-existent) spare time. You can expect to see some tools for this soon.
Also...
This is my first post here, and probably my last on this year, so Yeah !
Happy New Year Everyone ! \o/
Added subscriber: @c2ba
Added subscriber: @mavek
Added subscriber: @BrianSavery
Added subscriber: @jesterking
One issue with pip is updating of add-ons and their dependencies when the add-on is already loaded. Especially if the add-on dependency is a cpython-based module. My experience is that it is not possible to pip update a cpython dependency while it is loaded.
If an update mechanism could be provided for this that would be nice. One that would somehow uncheck add-on, somewhere write that the update needs to be made, then on next blender start do the update before re-enabling the add-on.
I think it would be great if the addon system of Blender could handle pip dependencies of addons in a pythonic way, from a requirements.txt file or setup.py file (would be great to support both). When an addon is loaded by the system, Blender could detect if one of these file exists and:
From what I saw when digging into the source code, most of the addon system is implemented in python so I think such a system could be prototyped quickly.
The advantages of this system are:
One point that seems hard to solve to me: if an addon require a specific version of a pip package but another is already installed, what to do ? report an error ? ask the user if he want to upgrade/downgrade ?
Added subscriber: @jta
Added subscriber: @MACHIN3
Added subscriber: @RayMairlot
Added subscriber: @piiichan
Added subscriber: @JuanCa
Added subscriber: @unwave
"""
packages
: list of tuples (, ) """if not packages:
return
import site
import importlib
import importlib.util
user_site_packages = site.getusersitepackages()
os.makedirs(user_site_packages, exist_ok = True)
sys.path.append(user_site_packages)
modules_to_install = [module[1] for module in packages if not importlib.util.find_spec(module[0])]
if modules_to_install:
import subprocess
("PIL", "Pillow"),
("xxhash","xxhash"),
("bs4","beautifulsoup4"),
Or may be to use
modules
folder: https://docs.blender.org/manual/en/latest/advanced/blender_directory_layout.html?highlight=./scripts/addons/modules/*.py#path-layoutAdded subscriber: @hlorus
Added subscriber: @mysticfall
Added subscriber: @thanhph111
Added subscriber: @Debuk
Added subscriber: @IyadAhmed
Added subscriber: @rotoglup
Added subscriber: @wassimr
Added subscriber: @ezragoss