OpenGL & Python under Linux + pyopengl : Attempt to retrieve context when no valid context
Hello. Thanks for making this project.
I have been trying to use immapp with pyopengl and I get an error. I found this error also happens when running the demo_custom_background.py example. I've tried setting the open_gl_options through code or with a hello_imgui.ini file but it doesn't help.
$ python bindings/imgui_bundle/demos_python/demos_immapp/demo_custom_background.py
Traceback (most recent call last):
File "/home/f/Projects/imgui_bundle/bindings/imgui_bundle/demos_python/demos_immapp/demo_custom_background.py", line 610, in <module>
sys.exit(main())
~~~~^^
File "/home/f/Projects/imgui_bundle/bindings/imgui_bundle/demos_python/demos_immapp/demo_custom_background.py", line 604, in main
immapp.run(runner_params, add_ons_params)
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/f/.local/lib/python3.13/site-packages/imgui_bundle/_patch_runners_add_save_screenshot_param.py", line 38, in patched_run
run_backup(*args, **kwargs)
~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "/home/f/Projects/imgui_bundle/bindings/imgui_bundle/demos_python/demos_immapp/demo_custom_background.py", line 593, in <lambda>
runner_params.callbacks.post_init = lambda: app_state.init_app_resources_3d(VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/f/Projects/imgui_bundle/bindings/imgui_bundle/demos_python/demos_immapp/demo_custom_background.py", line 490, in init_app_resources_3d
self.full_screen_quad_vao = create_full_screen_quad_vao()
~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
File "/home/f/Projects/imgui_bundle/bindings/imgui_bundle/demos_python/demos_immapp/demo_custom_background.py", line 185, in create_full_screen_quad_vao
GL.glVertexAttribPointer(0, 2, GL.GL_FLOAT, GL.GL_FALSE, 4 * 4, ctypes.c_void_p(0))
~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "src/latebind.pyx", line 52, in OpenGL_accelerate.latebind.Curry.__call__
File "/home/f/.local/lib/python3.13/site-packages/OpenGL/GL/VERSION/GL_2_0.py", line 465, in glVertexAttribPointer
contextdata.setValue( key, array )
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
File "/home/f/.local/lib/python3.13/site-packages/OpenGL/contextdata.py", line 58, in setValue
context = getContext( context )
File "/home/f/.local/lib/python3.13/site-packages/OpenGL/contextdata.py", line 40, in getContext
raise error.Error(
"""Attempt to retrieve context when no valid context"""
)
OpenGL.error.Error: Attempt to retrieve context when no valid context
it looks like pyopengl tries to use opengl 2.0?
$ glxinfo | grep "OpenGL version"
OpenGL version string: 4.6 (Compatibility Profile) Mesa 24.3.4
i am on linux and have also tried building the bundle from the latest source.
Hi,
I cannot reproduce this on my side, under linux, with:
glxinfo | grep "OpenGL version"
OpenGL version string: 4.6.0 NVIDIA 565.57.01
(it does work for me)
In this demos, OpenGL setup is handled by the C++ code, namely opengl_setup_glfw.cpp:
Could you log what you see in src/hello_imgui/internal/backend_impls/opengl_setup_helper/opengl_setup_glfw.cpp:
static void ApplyOpenGlOptions(const OpenGlOptionsFilled_& openGlOptions)
{
// Add these logs (and you may try to force versions below too)
printf("GLFW_CONTEXT_VERSION_MAJOR %d\n", openGlOptions.MajorVersion);
printf("GLFW_CONTEXT_VERSION_MINOR %d\n", openGlOptions.MinorVersion);
printf("GLFW_OPENGL_PROFILE %d\n", openGlOptions.UseCoreProfile);
printf("GLFW_OPENGL_FORWARD_COMPAT %d\n", openGlOptions.UseForwardCompat);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, openGlOptions.MajorVersion);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, openGlOptions.MinorVersion);
if (openGlOptions.UseCoreProfile)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
if (openGlOptions.UseForwardCompat)
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#ifdef HELLOIMGUI_HAS_OPENGL3
if (HelloImGui::GetRunnerParams()->rendererBackendType == RendererBackendType::OpenGL3)
ApplyAntiAliasingSamples(openGlOptions);
#endif
}
I have been trying to use immapp with pyopengl ... it looks like pyopengl tries to use opengl 2.0?
What do you mean? Did you perform a custom install of pyopengl? Do you think the line below from demo_custom_background.py should be the cause of the issue?
import OpenGL.GL as GL # type: ignore
These kinds of errors are hard to track. They are likely linked to dynamic library dependencies, and might also be due to a driver install, or to library path being incorrect (i.e. a bad version of libGL.so being used, etc).
If you have time, you might also test imgui_bundle with its recently available package on conda (channel conda-forge). I suspect it will handle dynamic libraries dependencies differently.
I can confirm it works ok installed with conda. I also tried it on a laptop with intel graphics with pip and I had the same problem. (both the laptop and my pc use fedora)
What I was talking about when I said it looked like it tried to use opengl 2.0 was a line on the original error message:
File "/home/f/.local/lib/python3.13/site-packages/OpenGL/GL/VERSION/GL_2_0.py", line 465, in glVertexAttribPointer
I can confirm it works ok installed with conda.
Thanks for the info.
What I was talking about when I said it looked like it tried to use opengl 2.0 was a line on the original error message: File "/home/f/.local/lib/python3.13/site-packages/OpenGL/GL/VERSION/GL_2_0.py", line 465, in glVertexAttribPointer
hum, this looks you were installing in the default python location.
Could you try with a virtual environment? I now have doubts that your install .local/lib/python3.13/site-packages/OpenGL/ was indeed kind of outdated, and that an isolated virtual environment might have solved the issue also
I tried a virtual environment and got the same error
Package Version
----------------- -------
annotated-types 0.7.0
freetype-py 2.5.1
glfw 2.8.0
glx 0.6
imgui-bundle 1.6.2
Mako 1.3.9
MarkupSafe 3.0.2
munch 4.0.0
numpy 2.2.3
pillow 11.1.0
pip 25.0.1
pydantic 2.10.6
pydantic_core 2.27.2
pyglm 2.8.0
PyOpenGL 3.1.9
rectangle 0.4
typing_extensions 4.12.2
i also tried an arch toolbox with the same result.
the glfw is older than in the conda environmet.
This strongly reminds me of an issue I helped to solve when the conda package was developed:
https://github.com/conda-forge/imgui-bundle-feedstock/issues/14#event-16111960178
It was happening under OSX, but was linked to the installation of PyOpenGL. It had been solved by this PR on PyOpenGL: https://github.com/conda-forge/pyopengl-feedstock/pull/27 => this PR is actually still based on PyOpenGL 3.7 (not 3.9) with backported patches from 3.8.
So yes, I suspect PyOpenGL is a possible culprit since conda ignores its latest version
You are probably right. I tested in the environment with pyopengl 3.1.0 and it works. Also tested with 3.1.7 and got the same error.
3.1.5 is the last version that also works for me. Would it help to find the commit?
https://github.com/mcfletch/pyopengl/commit/21927712ccb4f72cec6c788ca2e736481e4b29b6 this is the commit. it has to do with wayland detection and I am running on wayland. if I run the demo with PYOPENGL_PLATFORM=x11 it works with the latest pyopengl
Wow, thanks for the investigation!
May be @hmaarrfk (who originally did the fix the issue in the conda package) will know more (namely on why conda ignores pyopengl 3.9 and sticks to 3.7).
Anyhow, if you have time, could you post your investigation results as an issue to pyopengl. I suspect you were on track to do it anyway :-)
Do you have any idea of why it could be happening? I assumed it must be a mismatch of how the window is created and pyopengl thinking it is running in a wayland window while the window created is an x11 window... If that is the case perhaps setting XDG_SESSION_TYPE in hello_imgui or whatever part is creating the window could be a solution. Or I don't know if pyopengl could better detect what is the window it is running into?
wayland vs X11:
We are entering a territory which I do not know since Linux is not my primary OS.
Windows are created in src/hello_imgui/internal/backend_impls/backend_window_helper/glfw_window_helper.cpp. You might want to try to add flags there
, and that an isolated virtual environment might have solved the issue also
I 100% agree.
Coming in and chiming in. I'm sorry to say but the fact that we see:
/home/f/.local/lib/python3.13/site-packages/OpenGL/contextdata.py
indicates that you have installed packages with
pip install --user PyOpenGL
or something similar.
There is unfortunately alot of mis-information on the internet recommending this.
This might be OK to do in a docker container (where you are using docker isolation), but you simply cannot get reliable results without a python virtual environment, through venv or conda.
I recommend you do:
- Remove everything that looks like python from
${HOME}/.local/lib/
rm -rf ${HOME}/.local/lib/pytho*
- Rebuild your python environments with venv or conda.
if anything is in ${HOME}/.local/lib/python.... it might get accidentally picked up by conda, and cause you more headaches.
Note: a possible workaround would be to add this somewhere
# Note for linux Wayland users. you may need to set the following environment variable
# export XDG_SESSION_TYPE=x11
# due to a bug in PyOpenGL (see https://github.com/pthom/imgui_bundle/issues/321)
import os
if os.getenv("XDG_SESSION_TYPE") == "wayland" and not os.getenv("PYOPENGL_PLATFORM"):
print("""WARNING: You are running on Wayland, and PyOpenGL may fail to retrieve the OpenGL context. If you encounter issues, try running with:
export PYOPENGL_PLATFORM=x11 # (see https://github.com/pthom/imgui_bundle/issues/321""")
I will not do it now. Waiting to see if more users stumble on this issue.
Hi,
I stumbled across the same issue. I was using PyOpenGL 3.1.9, which causes the error that @giorgosg had from the original post.
OpenGL.error.Error: Attempt to retrieve context when no valid context
This is the current version of OpenGL I have:
$ glxinfo | grep "OpenGL version"
OpenGL version string: 4.6.0 NVIDIA 560.35.03
The earliest version that work for me is PyOpenGL 3.1.4 (also 3.1.5 works for me). 3.1.0 doesn't seem to work on my machine.
Same here. Fedora 41, x86, Gnome, Wayland PyOpenGL 3.1.9
glxinfo | grep "OpenGL version"
OpenGL version string: 4.6 (Compatibility Profile) Mesa 25.0.2
Attempted to run .../python_backends/examples/example_python_backend_glfw3.py