No module named '_bpy' when importing bpy in Python script

I compiled Blender as a Python module successfully under Windows.
Then I tried to use bpy to make a simple plot inside my Python program.
This program is run under Liclipse with a Python 3.7 installation to deal with Blender Python 3.7 version.
The path to bpy module is added in PyDev - PYTHONPATH / External Libraries.

But in my Python script the line ‘import bpy’ leads to the following error:

C:\blender-git\build_windows_x64_vc16_Release_bpy\bin\Release\2.91\scripts\modules\bpy\__init__.py", line 38, in <module>

    from _bpy import (
ModuleNotFoundError: No module named '_bpy'

Why does bpy module code contain this kind of syntax ‘from _bpy import …’ ?
How can we deal with that to make bpy work ?

1 Like

from bpy, not from _bpy

Right? I can’t imagine it’s that simple, but…

2 Likes

Maybe the installation of Blender as a Python module is incomplete. Did you follow all of the steps from this guide to create a Python installation with the bpy module in it?

You can verify if this worked by simply running a python shell from the python folder you have installed the bpy module to, and enter import bpy there. It should not throw any errors.

Thanks for your reply.
Firstly I just added bpy path to PyDev - PYTHONPATH / External Libraries in Liclipse.
Then I followed all requirements as you adviced : copy of bpy.pyd + *.dll in Python37\lib\site-packages, copy of 2.91 in Python37.

Now I get the following error:
import bpy
ImportError: DLL load failed: L’application n’a pas pu démarrer car sa configuration côte-à-côte est incorrecte. Pour plus d’informations, consultez le journal des événements des applications ou utilisez l’outil de ligne de commande sxstrace.exe.

This post said it’s fixed but is it really ?

After recompiling with WITH_WINDOWS_BUNDLE_CRT to OFF it went a step forward:
Color management: using fallback mode for management
Color management: Error could not find role data role.
BLT_lang_init: ‘locale’ data path for translations not found, continuing
Color management: scene view “Filmic” not found, setting default “Standard”.
blf_load_font_default: ‘fonts’ data path not found for ‘droidsans.ttf’, will not be able to display text
blf_load_font_default: ‘fonts’ data path not found for ‘bmonofont-i18n.ttf’, will not be able to display text
blf_load_font_default: ‘fonts’ data path not found for ‘bmonofont-i18n.ttf’, will not be able to display text
bpy: couldn’t find ‘scripts/modules’, blender probably wont start.
Freestyle: couldn’t find ‘scripts/freestyle/modules’, Freestyle won’t work properly.

Same error if I run “import bpy” in a python shell.

In fact I am not sure if it was a step forward or backward…
Thus I added datafiles and scripts paths in PyDev - PYTHONPATH / External Libraries in Liclipse and I got again this error:
ModuleNotFoundError: No module named ‘_bpy’

bpy code is really strange.

For example in scripts/modules/bpy/utils/init_.py, we can read:

from _bpy import (

)

And after:

import bpy as _bpy

I would be very interesed in knowing if somebody made it work under Windows

I recall having had the windows SideBySide error you got as well some time ago. IIRC the reason back then had been that the make bpy command did not finish without errors. Can you verify that make bpy works on your configuration? It’s a good idea to do a fresh recompile, meaning renaming or deleting the whole build folder first.

Thanks for your reply.
I don’t see any issue in the compilation, only one warning.

Here are the build options and the compilation log.log.txt (503.0 KB)

CMake Warning at CMakeLists.txt:609 (message):
WITH_OPENAL requires WITH_AUDASPACE which is disabled

Blender Configuration

Build Options:

  • WITH_BULLET ON
  • WITH_IK_SOLVER ON
  • WITH_IK_ITASC ON
  • WITH_OPENCOLLADA ON
  • WITH_FFTW3 ON
  • WITH_INTERNATIONAL ON
  • WITH_INPUT_NDOF ON
  • WITH_CYCLES ON
  • WITH_FREESTYLE ON
  • WITH_OPENCOLORIO ON
  • WITH_XR_OPENXR ON
  • WITH_OPENIMAGEDENOISE ON
  • WITH_OPENVDB ON
  • WITH_ALEMBIC ON
  • WITH_QUADRIFLOW ON
  • WITH_USD ON
  • WITH_TBB ON

Compiler Options:

  • WITH_BUILDINFO ON
  • WITH_OPENMP ON

System Options:

  • WITH_INSTALL_PORTABLE
  • WITH_X11_ALPHA
  • WITH_X11_XF86VMODE
  • WITH_X11_XFIXES
  • WITH_X11_XINPUT
  • WITH_MEM_JEMALLOC ON
  • WITH_MEM_VALGRIND OFF
  • WITH_SYSTEM_GLEW OFF

Image Formats:

  • WITH_OPENIMAGEIO ON
  • WITH_IMAGE_CINEON ON
  • WITH_IMAGE_DDS ON
  • WITH_IMAGE_HDR ON
  • WITH_IMAGE_OPENEXR ON
  • WITH_IMAGE_OPENJPEG ON
  • WITH_IMAGE_TIFF ON

Audio:

  • WITH_OPENAL OFF
  • WITH_SDL ON
  • WITH_SDL_DYNLOAD
  • WITH_JACK
  • WITH_JACK_DYNLOAD
  • WITH_CODEC_AVI ON
  • WITH_CODEC_FFMPEG ON
  • WITH_CODEC_SNDFILE ON

Compression:

  • WITH_LZMA ON
  • WITH_LZO ON

Python:

  • WITH_PYTHON_INSTALL OFF
  • WITH_PYTHON_INSTALL_NUMPY
  • WITH_PYTHON_MODULE ON
  • WITH_PYTHON_SAFETY OFF

Modifiers:

  • WITH_MOD_REMESH ON
  • WITH_MOD_FLUID ON
  • WITH_MOD_OCEANSIM ON

OpenGL:

  • WITH_GLEW_ES OFF
  • WITH_GL_EGL OFF
  • WITH_GL_PROFILE_ES20 OFF
  • WITH_GL_ANGLE OFF