__main__ has no attribute preload problem from p5 library in python
Question:
every time I run this code I get the error: AttributeError: module ‘main‘ has no attribute ‘preload’
I run this code
from p5 import setup, draw, size, background, run
import numpy as np
width = 500
height = 500
def setup():
size(width, height)
def draw():
background(51)
run()
and get the error message
Traceback (most recent call last):
File "c:/Users/Admin/main.py", line 15, in <module>
run()
File "C:UsersAdminAppDataLocalProgramsPythonPython38libsite-packagesp5sketchuserspace.py", line 160, in run
preload_method = __main__.preload
AttributeError: module '__main__' has no attribute 'preload'
Answers:
convention is to include if __name__ == '__main__':
when running a python file by itself. the docs follow this convention in their examples. this script ran without the error but I’m not familiar with p5 and what it’s supposed to show
│ File: test.py
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ from p5 import *
2 │ import numpy as np
3 │
4 │
5 │ width = 500
6 │ height = 500
7 │
8 │ def setup():
9 │ size(width, height)
10 │
11 │
12 │ def draw():
13 │ background(51)
14 │
15 │ if __name__ == '__main__':
16 │ run()
every time I run this code I get the error: AttributeError: module ‘main‘ has no attribute ‘preload’
I run this code
from p5 import setup, draw, size, background, run
import numpy as np
width = 500
height = 500
def setup():
size(width, height)
def draw():
background(51)
run()
and get the error message
Traceback (most recent call last):
File "c:/Users/Admin/main.py", line 15, in <module>
run()
File "C:UsersAdminAppDataLocalProgramsPythonPython38libsite-packagesp5sketchuserspace.py", line 160, in run
preload_method = __main__.preload
AttributeError: module '__main__' has no attribute 'preload'
convention is to include if __name__ == '__main__':
when running a python file by itself. the docs follow this convention in their examples. this script ran without the error but I’m not familiar with p5 and what it’s supposed to show
│ File: test.py
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ from p5 import *
2 │ import numpy as np
3 │
4 │
5 │ width = 500
6 │ height = 500
7 │
8 │ def setup():
9 │ size(width, height)
10 │
11 │
12 │ def draw():
13 │ background(51)
14 │
15 │ if __name__ == '__main__':
16 │ run()