[Tutor] Working with interactive Python shell

Evert Rol evert.rol at gmail.com
Wed Nov 24 15:00:13 CET 2010


>> You're not really showing what exactly you type. That's often more clearer than describing what you do, although in this case we can get a pretty good picture anyway.
> 
> OK, here's what I do:
> 
>>>> import test
> 
> I know the shell is importing the file because I can see the following message:
> 
> <module 'test' from 'test.py'>
> 
> 
> The file test.py has the following contents (this is a little line of
> code I constructed to see how the interactive shell worked importing
> the file):
> 
> words = 'in the garden on the bank behind the tree'.split()
> 
>> How do you 'call the variable'?
> 
> Sorry, you are right.  That was a sloppy use of the term 'call'. I
> meant to say "print the variable".
> 
> When I do:
> 
>>> print words
> 
> and
> 
>>> print words[3]
> 
> In principle I should get:
> 
> ['in', 'the', 'garden', 'on', 'the', 'bank', 'behind', 'the', 'tree']
> 
> and
> 
> on
> 
> What I do get is:
> 
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> NameError: name 'words' is not defined
> 
> If the file has the contents that I showed above (and it does),
> 'words' should be defined, shouldn't it?

Then you haven't read my previous response carefully enough, or I haven't phrased it properly. The example I gave was:

>>> import mymodule
>>> mymodule.X

where X is defined in a file called mymodule.py
In your case, replace mymodule with test, and X with words.

Also, really, read the tutorial on modules. I know we tutor here, but those tutorials are there to explain the most common cases (and the standard Python tutorial isn't the only tutorial, but since it comes with Python, it's the one I generally point to).
Of course, if something is unclear in the tutorial, let us know!

Cheers,

 Evert



More information about the Tutor mailing list