Import fails (newbie)
Peter Otten
__peter__ at web.de
Fri Jun 18 05:00:54 EDT 2010
mhorlick wrote:
> I'm a newbie and I have a small problem. After invoking IDLE -->
>
> Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "copyright", "credits" or "license()" for more information.
>>>> import os,glob
>>>> os.chdir('D:/Python_Programs')
>>>> print(os.getcwd())
> D:\Python_Programs
>>>> print(glob.glob('*.*'))
> ['humansize.py', 'humansize.pyc']
>>>> import humansize
> Traceback (most recent call last):
> File "<pyshell#5>", line 1, in <module>
> import humansize
> ImportError: No module named humansize
>>>>
>
> In a DOS command window I have no problems with the program:
>
> Directory of D:\Python_Programs
>
> 17/06/2010 01:56 PM <DIR> .
> 17/06/2010 01:56 PM <DIR> ..
> 17/06/2010 02:53 PM 1,266 humansize.py
> 17/06/2010 01:56 PM 1,315 humansize.pyc
> 2 File(s) 2,581 bytes
> 2 Dir(s) 104,122,085,376 bytes free
>
> D:\Python_Programs>c:\python31\python humansize.py
> 1.0 TB
> 931.3 GiB
>
> This 'humansize.py' program I got from the book 'Dive Into Python 3'.
> I don't know if you need to know the source.
>
> Can someone explain why the 'import' fails?
If you run the same code from the command line it works. That's because the
python interactive interpreter automatically includes the current directory
into the search path for modules. When you are running Idle's shell you have
to do that manually with
>>> import sys
>>> sys.path.insert(0, "")
I don't know if there is a reason for this difference or if it's just an
omission.
Peter
More information about the Python-list
mailing list