I'm quite new to python and still learning. I am currently have some difficulties using the import function in python. script2.py contains functions that enable me to use my OLED display. I currently have problems as when def primitives is called, I get an error that says it has not been defined.
The functions from this script (script2.py) are what I want to import to script1.py (primitives, main):
import time
import datetime
from luma.core.render import canvas
def primitives(device, draw):
# First define some constants to allow easy resizing of shapes.
padding = 2
shape_width = 20
top = padding
bottom = device.height - padding - 1
# Move left to right keeping track of the current x position for drawing shapes.
x = padding
# Write two lines of text.
size = draw.textsize('World!')
x = device.width - padding - size[0]
draw.rectangle((x, top + 4, x + size[0], top + size[1]), fill="black")
draw.rectangle((x, top + 16, x + size[0], top + 16 + size[1]), fill="black")
draw.text((device.width - padding - size[0], top + 4), 'Hello', fill="cyan")
draw.text((device.width - padding - size[0], top + 16), 'World1!', fill="purple")
time.sleep(5)
def main():
from luma.core.interface.serial import spi
from luma.core.render import canvas
from luma.oled.device import ssd1351
serial = spi(device=0, port=0, gpio_DC=20)
device = ssd1351(serial)
device.width=128
device.height=128
print("Testing basic canvas graphics...")
for _ in range(2):
with canvas(device) as draw:
primitives(device, draw)
print("Drawing stuff 1")
time.sleep(3)
print("Testing clear display...")
time.sleep(1)
device.clear()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
pass
The script that is importing these functions (script1.py1) is:
import time
import otherscript
variable_intermediate=1
while True:
if variable_intermediate==1:
from script2 import primitives, main #does not seem to work
main()
primitives (device, draw) #error printed in terminal, it says draw not defined
time.sleep(0.01)
asked Jan 25, 2018 at 13:37
Craver2000
4531 gold badge10 silver badges24 bronze badges
1 Answer 1
Try this : Script1.py
import time
import otherscript
variable_intermediate=1
while True:
if variable_intermediate==1:
from script2 import primitives, main
main() # this is the line you're missing
time.sleep(0.01)
answered Jan 25, 2018 at 13:42
Gwendal Grelier
5361 gold badge7 silver badges21 bronze badges
Sign up to request clarification or add additional context in comments.
7 Comments
Craver2000
Ok, I got what you mean. I now added
main() and primitives (device,draw)Craver2000
But I got another error:
NameError: name 'draw' is not definedCraver2000
Isn't draw already defined when I imported and call
main()?Craver2000
The line with
primitives(device, draw)Gwendal Grelier
That's normal, draw is not defined in script1.py, you should start primitive() from the script2.py But that is already what you are doing in your definition of main(), you call
primitives(device, draw). Why do you call it twice ? |
lang-py
from script2 import primitives, mainin your script you allow the first script to access theses function but if you want to run them you have to call them