[Tutor] name error

Steven D'Aprano steve at pearwood.info
Tue Jul 15 04:52:31 CEST 2014


On Mon, Jul 14, 2014 at 12:13:33PM -0400, ugajin at talktalk.net wrote:

[...]
> To draw a default rectangle using the nodebox 1, IDE you would simply 
> write: rect(0, 0, 100, 100) to a .py file.  When run in nodebox 1, IDE 
> it does what you might expect. It renders a 100 x 100 (px) rectangle 
> shape, with the top left vertex at the canvas x, y coordinate 0, 0 

Can you confirm that it is spelled "rect" in lowercase, not Rect with an 
initial capital? I suspect that is actually wrong, and you need to spell 
it with a capital R.

Remember that Python is case-sensitive and that "rect", "Rect", "RECT", 
etc. are all considered different.


> However, when I try to call the .py file as a module (using the nodebox 
> IDE) which is what I am trying to acheive, I get a traceback error 
> message e.g. 
> # import_test_1.py 
> rect(0, 0, 100, 100) 

So you have a file called "import_test_1.py", correct? It contains just 
a single command, "rect(...)", correct?

I know nothing about nodebox, but my *guess* is that before calling 
rect, you might need to import the name:

from nodebox import rect  # or perhaps Rect
rect(0, 0, 100, 100)  # Rect?

Reading further along, I would try this:

from nodebox.graphics import Rect
Rect(0, 0, 100, 100)



> #importer_test_1.py 
> import_test_1 
> Traceback (most recent call last): 
>  File "nodebox/gui/mac/__init__.pyo", line 358, in _execScript 
>   File "/Users/apple/Documents/nodeBox/07_10_2014/importer_2.py", line 
> 2, in <module> 
>   File "/Users/apple/Documents/nodeBox/07_10_2014/import_test_2.py", 
> line 22, in <module> 
> NameError: name 'rect' is not defined 

The file names you show do not match the file names in the traceback:

- you give file names import_test_1.py and importer_test_1.py

- Python reports the file names are import_test_2.py and importer_2.py

So I think the code you are showing us is different from the code you 
are running.


> This begins to look like a scope issue. 

NameError is almost always either a scope issue or a misspelling issue.


> However, when I execute/run the following command, I get the following 
> output 
> # import_test_2.py 
> print 'dir:', dir() 
>  
> dir: ['BOOLEAN', 'BUTTON', 'BezierPath', 'CENTER', 'CLOSE', 'CMYK', 
[snip long output]


> This seems to suggest (to me) that there is a class object named Rect 
> in the default dir() 

This hints that your earlier code needs to say

Rect(0, 0, 100, 100)

rather than lowercase "rect".


> I may not have the correct nomenclature here, is there as default IDE 
> dir() module? 

dir(obj) inspects the argument "obj" and returns the names of any 
attributes, methods or functions in obj. With no argument, dir() does 
the same to the current namespace. Roughly speaking, you can consider 
"namespace" to be almost the same as "the current module", at least when 
running interactively at the top-level (not inside a class or function).

IDEs often add things to the top-level namespace without your knowledge, 
so it is possible that when running in the nodebox IDE, it does the 
equivalent of "from nodebox import *" before you start.


> Also, when I execute or run the following command, I get: 
> print 'dir(Rect):', dir(Rect) 
> dir(Rect): ['__call__', '__class__', '__cmp__', '__delattr__', 
> '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', 
> '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', 
> '__str__', 'im_class', 'im_func', 'im_self'] 

Now you're looking at the methods and attributes provided by the Rect 
object, which isn't very interesting. More interesting would be:

print Rect
print Rect.__name__


which may help track down where it is coming from.

 
> Now, I am not sure if this helps, but if I put those two lines of code 
> into a file and call the module with the import command, I now get the 
> following output:: 
> # importer_test_2 
> import_test_2 
> print 'dir:', dir() 
> print 'dir(Rect):', dir(Rect) 

That code cannot work, because "import_test_2" should give a NameError. 
I think you mean:

import test_2  # note the space, not underscore

Please, it is important that when asking questions about your code, 
DON'T retype them from memory, always copy and paste. Otherwise you 
waste our time trying to debug a problem in code A by reading code B, 
which may be critically different.


> dir: ['__builtins__', '__doc__', '__file__', '__name__', 'draw_rect', 
> 'nodebox'] 

Where do draw_rect and nodebox come from? I suspect you may not be 
showing us all the code you are running. Either that, or the notebox IDE 
is importing things in the background without telling you. (This is why 
I dislike IDEs. You never know what they are doing.)

  
> dir(Rect):Traceback (most recent call last): 
>  File "nodebox/gui/mac/__init__.pyo", line 358, in _execScript 
>   File "/Users/apple/Documents/nodeBox/07_10_2014/importer_2.py", line 
> 2, in <module> 
>   File "/Users/apple/Documents/nodeBox/07_10_2014/import_test_2.py", 
> line 18, in <module> 
> NameError: name 'Rect' is not defined 

Try replacing "Rect" with "draw_rect". What happens?



> After some further tests, with Rect, and some Traceback messages 
> telling me that Rect(), 

The text you quote does not say that Rect is deprecated. It says 
DrawingPrimitives is deprecated and you should use graphics instead.


> which was located in DrawingPrimitives, was 
> deprecated, I found some changes noted posted on GitHub. Those at v 
> 1.9.0 seem apposite (I am running v1.9.5):
> for nodebox 1, v 1.9.7:
[...snip irrelevent information...]

You are running v1.9.5. The comment you quote is for 1.9.7. It is not 
relevent to you, or us.


> for v1.9.0:
> NodeBox is now packaged: DrawingPrimitives is obsoleted in favor of 
> nodebox.graphics.This also means you can work directly with the context:
>  
> After some further try it and see tests, I noted the following:
> #nodebx_graphics.py
> print 'dir(nodebox.graphics.BezierPath):', 
> dir(nodebox.graphics.BezierPath)

I don't trust that this is the code you actually are running. I would 
expect that to give a NameError that nodebox is not defined.

Did you have a line

import nodebox

first, that you negected to show?


> Also, noted I can call the rect() function as follows:
> #nodebx_graphics.py
> import nodebox
> nodebox.graphics.BezierPath(rect(0, 0, 100, 100))

I do not expect that would work. I expect that you would have to write 
this:

import nodebox
nodebox.graphics.BezierPath.rect(0, 0, 100, 100)

or possibly:

import nodebox
nodebox.graphics.Rect(0, 0, 100, 100)



At this point, I'm going to stop. I've already spent well over 30 
minutes typing up this response, and I feel like I have got nowhere 
because I have no confidence that the code you show us is the code you 
ae actually running. Please, please please, in future, do not retype 
your code from memory, copy and paste it. I believe that many of the 
problems you are experiencing come from mistyping names, or failing to 
import, and that by telling us "this code works" when it doesn't 
actually work, you are just adding confusion.

Please try the suggestions I have made, and if you are still having 
problems, report back and we'll see what further assistence we can give.

Good luck!



-- 
Steven


More information about the Tutor mailing list