Page MenuHome

"ModuleNotFoundError: No module named 'bpy_types'" when building as Python module
Closed, InvalidPublic

Description

I've been trying to build Blender as a Python module, and keep running into the same error on both Mac and windows on both master and Blender2.8. I follow the build instructions on the wiki and updating CMakeLists.txt according to this BSO answer. Here is the most recent output after running make update && make just now on macOS Mojave:

admin@Benjamin-H-Macbook-Pro > ~/Projects/blender-build/build_darwin/bin > python3
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import bpy
Color management: using fallback mode for management
BLT_lang_init: 'locale' data path for translations not found, continuing
bpy: couldnt find 'scripts/modules', blender probably wont start.
Freestyle: couldn't find 'scripts/freestyle/modules', Freestyle won't work properly.
ModuleNotFoundError: No module named 'bpy_types'
ModuleNotFoundError: No module named 'bpy_types'
ERROR (bpy.rna): /Users/admin/Projects/blender-build/blender/source/blender/python/intern/bpy_rna.c:6662 pyrna_srna_ExternalType: failed to find 'bpy_types' module
ModuleNotFoundError: No module named 'bpy_types'
ModuleNotFoundError: No module named 'bpy_types'
ERROR (bpy.rna): /Users/admin/Projects/blender-build/blender/source/blender/python/intern/bpy_rna.c:6662 pyrna_srna_ExternalType: failed to find 'bpy_types' module
ModuleNotFoundError: No module named 'bpy_types'
ERROR (bpy.rna): /Users/admin/Projects/blender-build/blender/source/blender/python/intern/bpy_rna.c:6662 pyrna_srna_ExternalType: failed to find 'bpy_types' module

Surrounding the import statement with try:catch: doesn't do anything, crashing instead of throwing a python error. I've tried building blender normally and that does succeed, and I am able to import bpy and everything else without a problem within the scripting console. This is true on both Mac and Windows. I've tried adding the scripts/modules folder to sys.path but this does not do anything.

Details

Type
Bug

Event Timeline

@Bastien Montagne (mont29), yes.

@Benjamin Humpherys (brhumphe) you need to 'make install'. manually copying bpy.so will give this error.

closing.

@Campbell Barton (campbellbarton) Using make install does not fix the issue. I just did a fresh build and the results were the same:

git pull
make update
make
cd ../build_darwin
make install

I created a virtual environment using these instructions from @sybren, sim linking both blender/release and boy.so inside of the site-packages directory of the virtual environment:

cd venv/lib/python3.7/site-packages/
ln -s ~/Projects/blender-build/build_darwin/bin/bpy.so
ln -s ~/Projects/blender-build/blender/release

Attempting to import bpy results in the following:

../../../bin/python3 -c "import bpy"
Color management: using fallback mode for management
BLT_lang_init: 'locale' data path for translations not found, continuing
bpy: couldnt find 'scripts/modules', blender probably wont start.
Freestyle: couldn't find 'scripts/freestyle/modules', Freestyle won't work properly.
ModuleNotFoundError: No module named 'bpy_types'
ModuleNotFoundError: No module named 'bpy_types'
ERROR (bpy.rna): /Users/admin/Projects/blender-build/blender/source/blender/python/intern/bpy_rna.c:6662 pyrna_srna_ExternalType: failed to find 'bpy_types' module
ModuleNotFoundError: No module named 'bpy_types'
ModuleNotFoundError: No module named 'bpy_types'
ERROR (bpy.rna): /Users/admin/Projects/blender-build/blender/source/blender/python/intern/bpy_rna.c:6662 pyrna_srna_ExternalType: failed to find 'bpy_types' module
ModuleNotFoundError: No module named 'bpy_types'
ERROR (bpy.rna): /Users/admin/Projects/blender-build/blender/source/blender/python/intern/bpy_rna.c:6662 pyrna_srna_ExternalType: failed to find 'bpy_types' module
F1129 15:51:07.112875 255042944 utilities.cc:322] Check failed: !IsGoogleLoggingInitialized() You called InitGoogleLogging() twice!
*** Check failure stack trace: ***
    @        0x105c2202f
    @        0x105c1e609
    @        0x105c25b2f
    @        0x105b44af1
    @        0x103cc49cd
    @        0x1040e7437
    @        0x103685f9c
    @        0x103689b56
    @        0x103742590
    @        0x10373dea3
    @        0x10373d6a8
    @        0x10365e398
    @        0x10365e081
    @        0x10371a9f2
    @        0x10371e096
    @        0x10365dd81
    @        0x10371d5bb
    @        0x10371a560
    @        0x10365e1f0
    @        0x10371d5bb
    @        0x10371a546
    @        0x10365e1f0
    @        0x10371d5bb
    @        0x10371a601
    @        0x10365e1f0
    @        0x10371d5bb
    @        0x10371a601
    @        0x10365e1f0
    @        0x10371d5bb
    @        0x10371a601
    @        0x10365e1f0
    @        0x10365d7e4
[1]    39508 abort      ../../../bin/python3 -c "import bpy"

This may be OSX spesific then.

Try using a system call tracing tool (strace on Linux), to see which directories are being checked for immediately before the message:

bpy: couldnt find 'scripts/modules', blender probably wont start.

This is not OSX-specific, I have the same issue on Windows too, most recently with build 84285c1e3440. Here is the system trace from Procmon on windows:

@Benjamin Humpherys (brhumphe) On windows it's looking for: C:\ProgramData\Blender Foundation\Blender\2.79\scripts\modules

This is from Blender's release/scripts path.

Why is it looking there instead of searching the sys.path of the python interpreter?

@Benjamin Humpherys (brhumphe)

I rather not get into details of why Blender is setup the way it is.
In brief, this works well for an application that embeds Python, but not so well for a Python module.

This can be made to work as long as the install target copies files to the correct locations. If you get this working, it'd be good to apply changes to CMake.

I keep this working on my system. The reason this bug remains closed is this feature is experimental and difficult to properly support - when Python devs run into issues.

@Campbell Barton (campbellbarton) Fair enough. I can see how changing it to work with sys.path could be more effort than it’s worth. It’s good to know what the issue is, this was bugging me for weeks! If I find a solution I’ll pass it along.