[Tutor] Working with interactive Python shell

Steven D'Aprano steve at pearwood.info
Wed Nov 24 14:16:27 CET 2010


Josep M. Fontana wrote:

> 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) 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 probably writing:

import filename.py

instead of just:

import filename


When you import a module, it doesn't necessarily come from a .py file. 
It could come from a pre-compiled .pyc or .pyo file, or from a Windows 
.pyw file, or a .dll or .so compiled C library, or out of a zip file, to 
mention just a few. There are many different possibilities. So Python 
expects you to just give the module name, "filename", without the file 
extension, and it will search for the correct file regardless of the 
extension.

Python uses the dot notation for packages. "import filename.py" looks 
for a module called "py" inside a package called "filename". Since it 
doesn't find one, it raises ImportError.


> This is if I enter the file name with the .py extension. 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".

Please show the actual line of code you use, and the actual error 
message, in full, copied and pasted, and not retyped from memory, or 
paraphrased, or simplified, or otherwise changed in any way.



-- 
Steven



More information about the Tutor mailing list