moderngl / moderngl-window

A cross platform utility library for ModernGL making window creation and resource loading simple
MIT License
244 stars 57 forks source link

pyopengl example #78

Closed taiya closed 4 years ago

taiya commented 4 years ago

TL;DR: this example generates AttributeError: module 'moderngl_window' has no attribute 'WindowConfig'

(python3) ~: pip install moderngl-window
Collecting moderngl-window
  Downloading moderngl_window-2.1.1-py3-none-any.whl (117 kB)
     |████████████████████████████████| 117 kB 894 kB/s
Collecting moderngl<6
  Downloading moderngl-5.6.0-cp37-cp37m-macosx_10_9_intel.whl (225 kB)
     |████████████████████████████████| 225 kB 2.3 MB/s
Collecting pyrr<1,>=0.10.3
  Downloading pyrr-0.10.3-py3-none-any.whl (46 kB)
     |████████████████████████████████| 46 kB 3.1 MB/s
Requirement already satisfied: numpy<2,>=1.16 in ./Envs/python3/lib/python3.7/site-packages (from moderngl-window) (1.18.2)
Collecting pyglet<2,>=1.4.2
  Downloading pyglet-1.5.4-py2.py3-none-any.whl (1.0 MB)
     |████████████████████████████████| 1.0 MB 2.4 MB/s
Collecting Pillow>=5
  Downloading Pillow-7.1.2-cp37-cp37m-macosx_10_10_x86_64.whl (2.2 MB)
     |████████████████████████████████| 2.2 MB 4.2 MB/s
Collecting glcontext<3,>1
  Downloading glcontext-2.1.0-cp37-cp37m-macosx_10_9_intel.whl (10 kB)
Collecting multipledispatch
  Downloading multipledispatch-0.6.0-py3-none-any.whl (11 kB)
Requirement already satisfied: six in ./Envs/python3/lib/python3.7/site-packages (from multipledispatch->pyrr<1,>=0.10.3->moderngl-window) (1.14.0)
Installing collected packages: glcontext, moderngl, multipledispatch, pyrr, pyglet, Pillow, moderngl-window
Successfully installed Pillow-7.1.2 glcontext-2.1.0 moderngl-5.6.0 moderngl-window-2.1.1 multipledispatch-0.6.0 pyglet-1.5.4 pyrr-0.10.3
(python3) ~: mate moderngl.py
(python3) ~: python moderngl.py
Traceback (most recent call last):
  File "moderngl.py", line 2, in <module>
    import moderngl_window
  File "/Users/atagliasacchi/Envs/python3/lib/python3.7/site-packages/moderngl_window/__init__.py", line 13, in <module>
    import moderngl
  File "/Users/atagliasacchi/moderngl.py", line 3, in <module>
    from OpenGL import GL
ModuleNotFoundError: No module named 'OpenGL'
(python3) ~: pip install pyopengl
Collecting pyopengl
  Downloading PyOpenGL-3.1.5-py3-none-any.whl (2.4 MB)
     |████████████████████████████████| 2.4 MB 792 kB/s
Installing collected packages: pyopengl
Successfully installed pyopengl-3.1.5
(python3) ~: python moderngl.py
Traceback (most recent call last):
  File "moderngl.py", line 2, in <module>
    import moderngl_window
  File "/Users/atagliasacchi/Envs/python3/lib/python3.7/site-packages/moderngl_window/__init__.py", line 13, in <module>
    import moderngl
  File "/Users/atagliasacchi/moderngl.py", line 6, in <module>
    class PyOpenGL(moderngl_window.WindowConfig):
AttributeError: module 'moderngl_window' has no attribute 'WindowConfig'
einarf commented 4 years ago

It's because python believes your python module /Users/atagliasacchi/moderngl.py is in fact the moderngl library. You need to rename it to something else.

You can see this in the traceback

Traceback (most recent call last):
  File "moderngl.py", line 2, in <module>
    import moderngl_window
  File "/Users/atagliasacchi/Envs/python3/lib/python3.7/site-packages/moderngl_window/__init__.py", line 13, in <module>
    import moderngl
  File "/Users/atagliasacchi/moderngl.py", line 6, in <module>

In the last line moderngl_window is importing your module instead of moderngl. This is because python adds the directory of the script you run to be beginning of the python path. It's just one of those things to be careful about with python.

taiya commented 4 years ago

alright, I blame this to coding at 2am

einarf commented 4 years ago

if it's any consolation, I did the same last week 😄