FAQ
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?

Thanks,

Mike

Search Discussions

  • Albert Hopkins at Jun 18, 2010 at 1:18 am

    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
  • Peter Otten at Jun 18, 2010 at 9:00 am

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 17, '10 at 7:04p
activeJun 18, '10 at 9:00a
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase