[Tutor] Working with interactive Python shell

Evert Rol evert.rol at gmail.com
Wed Nov 24 14:00:51 CET 2010


<snip intro>
> 
> -------
> One question for Steve (or for whoever wants to answer): you say you
> have a terminal with two tabs (neat, I wonder whether I can get tabs
> as well for my terminal in OS X)

In Terminal.app, just type command-T and you get a new tab. Switch with the mouse or command-shift-[ & command-shift-]
But sometimes, two separate terminals can be more convenient, so you can see both at the same time.

> and when you need to do debugging you
> turn to your interactive python terminal and do;
> 
> import filename  # first time only
> reload(filename)  # all subsequent times
> 
> If I do this with a "normal" python file (filename.py), I get the error:
> 
> "ImportError: No module named py"

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.

> This is if I enter the file name with the .py extension.

Perhaps try and read through the module section of the standard Python tutorial: http://docs.python.org/tutorial/modules.html
The .py extension just signifies the file is a Python file, but the actual module name comes before the extension. 
The dot in module names is to divide modules up into submodules, or package things (further down in the Python tutorial on modules).

> If I just enter the
> file name without the extension, everything seems to work fine and I
> don't get any error message but then when I call a variable I get a
> message saying "'X' is not defined".

How do you 'call the variable'? 
Btw, you don't really call a variable: access would probably be a more accurate term imo. You would call a function though (and that function would then be executed).

But if you call a variable that is defined inside the module, you'll need to prepend the module name, like:
>>> import mymodule
>>> mymodule.X

This is actually a form of namespacing: X is not defined in your global environment, just in mymodule.
And again, see the examples in the tutorial.


> --------------
> 
> I'm wondering whether I'm doing something wrong or whether I didn't
> properly understand what Steve was saying about working with the
> interactive Python shell. If I understood correctly, one can import a
> Python script into the interactive shell (not just modules) and then
> test different things from the prompt.

Yes, correct.

> I've tried to import various
> files following Steve's instructions but I get the errors I describe
> above.


Ok, I don't have Steve's instructions handy (they've been stripped of this thread now), but I assume that if you apply the above, you should get a bit further.
But without actually showing us what you've typed and the errors, we can't give you concrete advice (copy-paste is often a very good idea).

Btw, should you want to delve further into using the interactive prompt, there are various interactive prompts that provide more conveniences than the standard Python prompt. IPython is my favourite, but before you do that, try and use the default prompt.

Good luck,

  Evert




More information about the Tutor mailing list