Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: module 'p5.core.p5' has no attribute 'exit' #150

Closed
Gauthameshwar opened this issue Apr 1, 2020 · 15 comments · Fixed by #309
Closed

AttributeError: module 'p5.core.p5' has no attribute 'exit' #150

Gauthameshwar opened this issue Apr 1, 2020 · 15 comments · Fixed by #309

Comments

@Gauthameshwar
Copy link

Describe the bug
I tried running the sample code in p5 README text in spyder 4, Jupyter Notebook, and external windows terminal. The same error(AttribureError: ...) pops up in the first two. The terminal , however properly executes this code. Please let me know how to fix it. (i find it comfortable to run codes in those environments...)
To Reproduce
Try running the code in those environments:

samplep5.txt

Screenshots
The errors i got in the two environments
error_jupy.txt
:
error_spy.txt

System information:

  • p5 release 0.6.0
  • Python 3.7.6
  • Operating system: Windows 10
@arihantparsoya
Copy link
Member

@Gautameshwar , by default p5 uses glfw backend, to use p5 within Jupyter notebook, there needs to be some support for WebGL inside p5 which doesnt exist (yet).

@Gauthameshwar
Copy link
Author

But what about spyder environment? It would still be a great help if i could run p5 codes on spyder, if not jupyter...

@jeremydouglass
Copy link
Contributor

Perhaps see #133, and #76 (comment)

@anugrahandi
Copy link

Running the code inside ipython, I have the same error. Then I put the script in .py file and run it on conda shell environment and it works.

So, just for note that I think this error related to ipython/jupyter notebook rather than how it works in conda environment.

If you like Spyder, then for current situation, I suggest to use VScode which have its terminal window and run the script using command python <your_files>,py

@anikoiau
Copy link

I have the same problem. In fact importing p5 in spyder also does not work when i open spyder from anaconda shell. But works if i open spyder from Windows command prompt.

@pikeas
Copy link

pikeas commented Dec 26, 2020

Just ran into this, it appears to be a bug rather than an enhancement.

  1. https://github.com/p5py/p5/blob/master/p5/sketch/userspace.py#L169 is in run(), which calls exit().
  2. https://github.com/p5py/p5/blob/master/p5/sketch/userspace.py#L262 is in exit(), which calls p5.exit().
  3. https://github.com/p5py/p5/blob/master/p5/sketch/userspace.py#L30 p5 is an alias for ..core.p5
  4. https://github.com/p5py/p5/blob/master/p5/core/p5.py does not define exit()

@pikeas
Copy link

pikeas commented Dec 26, 2020

Example which errors:

from p5 import *


def setup():
    size(1024, 768)
    stroke(255)


def draw():
    pass


if __name__ == "__main__":
    run()

Tested with p5 0.7.1 / python 3.9.1 / macOS 10.15.7.

I also see that p5.sketch.base has a on_close() method which calls exit() - this is likely what's causing it. exit() throws SystemExit, graceful exits should instead call sys.exit().

Interestingly, I don't see base.py in that directory here on Github? It looks like master has been refactored a bit since release. That method is now at https://github.com/p5py/p5/blob/master/p5/sketch/Vispy2DRenderer/base.py#L149

@pikeas
Copy link

pikeas commented Dec 26, 2020

To clarify, there are two different errors:

p5.exit() (actually p5.core.p5) isn't defined, and throws an error.

In addition, Sketch.on_close() calls exit(), which will throw a SystemExit error.

@pikeas
Copy link

pikeas commented Dec 26, 2020

Replacing exit() with sys.exit() didn't seem to help, there's an exception trap in either p5 or vispy. I worked around this by monkey-patching the following onto the Sketch class:

from vispy import app

def on_close(*args, **kwargs):
   app.quit()

I'm not sure if this has any implications for cleanup code, but at least there are no more exceptions (so far).

@tushar5526
Copy link
Member

Interestingly, I don't see base.py in that directory here on Github? It looks like master has been refactored a bit since release. That method is now at https://github.com/p5py/p5/blob/master/p5/sketch/Vispy2DRenderer/base.py#L149

There has been some refactoring for #212. See #215

@tushar5526
Copy link
Member

Example which errors:

It works fine for me, with version 0.7.1. What is the environment you are working in?

@pikeas
Copy link

pikeas commented Dec 27, 2020

It works fine for me, with version 0.7.1. What is the environment you are working in?

I shared my environment earlier, may have gotten a bit lost in the thread - tested with p5 0.7.1 / python 3.9.1 / macOS 10.15.7.

@pikeas
Copy link

pikeas commented Dec 27, 2020

$ python -V
Python 3.9.1
$ python -m venv p5-test && cd p5-test && ./bin/pip install p5
$ cat <<EOF >example.py
> from p5 import *
>
>
> def setup():
>     size(1024, 768)
>     stroke(255)
>
>
> def draw():
>     pass
>
>
> if __name__ == "__main__":
>     run()
> EOF
$ ./bin/python example.py
Exception ignored on calling ctypes callback function: <bound method CanvasBackend._on_close of <vispy.app.backends._glfw.CanvasBackend object at 0x119a757f0>>
Traceback (most recent call last):
  File "/Users/pikeas/code/tmp/p5-test/lib/python3.9/site-packages/vispy/app/backends/_glfw.py", line 404, in _on_close
    self._vispy_canvas.close()
  File "/Users/pikeas/code/tmp/p5-test/lib/python3.9/site-packages/vispy/app/canvas.py", line 456, in close
    self.events.close()
  File "/Users/pikeas/code/tmp/p5-test/lib/python3.9/site-packages/vispy/util/event.py", line 455, in __call__
    self._invoke_callback(cb, event)
  File "/Users/pikeas/code/tmp/p5-test/lib/python3.9/site-packages/vispy/util/event.py", line 471, in _invoke_callback
    cb(event)
  File "/Users/pikeas/code/tmp/p5-test/lib/python3.9/site-packages/p5/sketch/base.py", line 148, in on_close
    exit()
  File "/Users/pikeas/.asdf/installs/python/3.9.1/lib/python3.9/_sitebuiltins.py", line 26, in __call__
    raise SystemExit(code)
SystemExit: None
Traceback (most recent call last):
  File "/Users/pikeas/code/tmp/p5-test/example.py", line 14, in <module>
    run()
  File "/Users/pikeas/code/tmp/p5-test/lib/python3.9/site-packages/p5/sketch/userspace.py", line 167, in run
    exit()
  File "/Users/pikeas/code/tmp/p5-test/lib/python3.9/site-packages/p5/sketch/userspace.py", line 250, in exit
    p5.exit(*args, **kwargs)
AttributeError: module 'p5.core.p5' has no attribute 'exit'

The exception is thrown after clicking the close window "X" in p5's GUI.

@DeltaC6
Copy link

DeltaC6 commented Jan 2, 2021

This solved for me:

Inside the following file:

\site-packages\p5\core\p5.py

just add these few lines.

import builtins
def exit():
builtins.exit()

@barathbheeman
Copy link

The issue still persists, as of this comment. I installed it today (10/march/2021). And ran the simple example from the docs page:
https://p5.readthedocs.io/en/latest/examples/structure/statements%20and%20comments.html

I ran it from the terminal and still get this error:

Exception ignored on calling ctypes callback function: <bound method CanvasBackend._on_close of <vispy.app.backends._glfw.CanvasBackend object at 0x7efd26e8e7c0>> Traceback (most recent call last): File "/usr/local/lib/python3.8/dist-packages/vispy/app/backends/_glfw.py", line 404, in _on_close self._vispy_canvas.close() File "/usr/local/lib/python3.8/dist-packages/vispy/app/canvas.py", line 456, in close self.events.close() File "/usr/local/lib/python3.8/dist-packages/vispy/util/event.py", line 455, in __call__ self._invoke_callback(cb, event) File "/usr/local/lib/python3.8/dist-packages/vispy/util/event.py", line 471, in _invoke_callback cb(event) File "/usr/local/lib/python3.8/dist-packages/p5/sketch/base.py", line 148, in on_close exit() File "/usr/lib/python3.8/_sitebuiltins.py", line 26, in __call__ raise SystemExit(code) SystemExit: None Traceback (most recent call last): File "hello_world.py", line 19, in <module> run() File "/usr/local/lib/python3.8/dist-packages/p5/sketch/userspace.py", line 167, in run exit() File "/usr/local/lib/python3.8/dist-packages/p5/sketch/userspace.py", line 250, in exit p5.exit(*args, **kwargs) AttributeError: module 'p5.core.p5' has no attribute 'exit'

OS: Ubuntu 20.04
python version: 3.8.5
p5py version: 0.7.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants