Chiplotle command 'plotter' results in NameError - not defined when run from py file

Hi! Please forgive me if I’m not following proper protocol! I’m new, haven’t posted to any forum in many years, and my brain is fried from trying to figure things out on my own over the last few days. I’ve overcome several hurdles, but now I’m stuck! I prepared some information in Notepad++ which I’ve copied and pasted below with the triple backticks. This should explain my current hurdle better than I could in a normal set of sentences. I’m sorry if it’s too long or not formatted properly! I’m not use to any of this (Python, Chiplotle, etc.). Thanks so much! (I tried using colors to differentiate between my comments and what appeared on the screen, but they didn’t transfer. (Although, I later discovered I could creatively use single or double quotes to color my notes in red.) Therefore, I started each of my comments indented with “NOTE:” and ended each with “END NOTE”. Thanks again!)

	"NOTE:
	I am using Windows 11 and Windows PowerShell.  (BTW, 
    I did not have any better luck using IDLE.)
	END NOTE"

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

	"NOTE:
	I use the following line to set communication
	parameters for the USB>Serial adapter I am using.
	END NOTE"

PS C:\Users\dream> mode com7:9600,n,8,1,p

	"NOTE:
	Doing so returns the following information:
	END NOTE"

Status for device COM7:
-----------------------
    Baud:            9600
    Parity:          None
    Data Bits:       8
    Stop Bits:       1
    Timeout:         OFF
    XON/XOFF:        OFF
    CTS handshaking: ON
    DSR handshaking: ON
    DSR sensitivity: OFF
    DTR circuit:     HANDSHAKE
    RTS circuit:     HANDSHAKE

	"NOTE:
	Here I run Chiplotle3.
	END NOTE"

PS C:\Users\dream> chiplotle3

	"NOTE:
	Doing so returns the following information:
	END NOTE"

  +-----------------------+
  |   Chiplotle! v.0.4.1  |
  +-----------------------+

Instantiated plotter HP7475A in port COM7:
   Drawing limits: (left 0; bottom 0; right 11040; top 7721)
   Buffer Size: 512
   
	"NOTE:
	If I enter each of the next two lines (after "chiplotle>") 
    one at a time, pressing 'Enter' after each line, the 
    plotter responds as expected.
	END NOTE"
   
chiplotle> plotter.write(hpgl.PD([(500,500), (2500,4000), 
           (1000,2000), (500,500)]))
chiplotle> plotter.write(hpgl.PU([(100,100)]))

	"NOTE:
	I enter the following to exit out of Chiplotle:
	END NOTE"

chiplotle> quit()

	"NOTE:
	I change directories to where I have a couple 'py' files and 
	run one called 'hello.py' by entering "python hello.py", which,
	as expected, results in the two following lines of "Hello world".
	END NOTE"

PS C:\Users\dream> cd C:\Users\dream\Documents\Python
PS C:\Users\dream\Documents\Python> python hello.py
Hello world
Hello world

	"NOTE:
	Next I try running a file to send some commands to the plotter.
	Following is the code that is in the file Square.py.  (I know
	it is not a square, I am just trying to talk to the plotter.)
	END NOTE"

	"PROGRAM CODE
	Line 01  from chiplotle3 import *
	Line 02  plts = instantiate_plotters( )[0]
	Line 03  plotter.write(hpgl.PU([(100,100)]))
	Line 04  plotter.write(hpgl.PD([(500,500), (2500,4000), 
             (1000,2000), (500,500)]))
	Line 05  plotter.select_pen(0)
	END PROGRAM CODE"

PS C:\Users\dream\Documents\Python> python Square.py

Instantiated plotter HP7475A in port COM7:
   Drawing limits: (left 0; bottom 0; right 11040; top 7721)
   Buffer Size: 512
Traceback (most recent call last):
  File "C:\Users\dream\Documents\Python\Square.py", line 3, in <module>
    plotter[0].select_pen(2)
    ^^^^^^^
NameError: name 'plotter' is not defined. Did you mean: 'plotters'?
PS C:\Users\dream\Documents\Python>
	
	"NOTE:
	The first two lines of code function as expected, but at line 3, a 
	NameError results saying that plotter is not defined.  I do not
	understand why I am getting this error since I do not get it when I 
	enter and run that same line when at the chiplotle prompt. Does
	anyone know what I am missing/not understanding, aka the solution?
	Thanks!
	END NOTE"

This:

plts = instantiate_plotters( )[0]

binds plts to the first plotter, but then you’re not referring to plts again, but to plotter.

It should be:

plotter = instantiate_plotters()[0]

What’s available at the chipotle3 prompt is a different matter…

Matthew,

You, my friend, are a GENIOUS, and a lifesaver! At least you’ve saved my head from losing any more hairs! Absolutely wonderful! Great catch! So grateful! Thank you so very much!

Yes, I understand what you’re saying, and it makes perfect sense now.

I changed
plts = instantiate_plotters( )[0]
to
plotter = instantiate_plotters( )[0]
like you said, and that took care of the problem! I’m now able to and in the process of using the plotter to print something else that I’ve been wanting to for days. It’s actually been years that I’ve been wanting to do what I’m now able to do (thanks in part to you)!

The main reason I snatched the plotter up for $75 when I found it in a second hand computer store 25 years ago: to use this machine that originally went for $1,895 when new in 1983 to make Spirograph-type drawings. LOL! Attached (hopefully) is a picture of a print hot off the plotter!

Thanks again! You have answered a prayer! I thank God and I thank you! Have a blessed evening and a blessed week!

Adrian

When you run python, it creates a module with the __name__ attribute '__main__'. When you ran chiplotle3 from the command line, python ran the code in chiplotle3.py or chiplotle3/main.py in that main module. Either way, that file likely has code something like the following:

if __name__ == '__main__':
    plotter = instantiate_plotters( )[0]
    <more initialization code>
    <looping code to give the interactive 'chiplotle>' prompt and respond to input>

When you imported chiplotle3, it was run in a module with __name__ ‘chiplotle3’ and the initializaition code, including the line defining ‘plotter’, was not run.

1 Like

Thank you for your help Terry! I appreciate you responding!