Import fails (newbie)

Albert Hopkins marduk at letterboxes.org
Thu Jun 17 21:18:16 EDT 2010


On Thu, 2010-06-17 at 12:04 -0700, mhorlick wrote:
> Hello,
> 
> 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?

os.chdir() is not the same as running python from inside the
Python_programs directory.

The correct way of doing this is to put Python_Programs in your path,
e.g.:

>>> import sys
>>> sys.path.insert(0, 'D:/Python_Programs')
>>> import humansize




More information about the Python-list mailing list