Import fails (newbie)

M

mhorlick

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
 
A

Albert Hopkins

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.:
 
P

Peter Otten

mhorlick said:
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

I don't know if there is a reason for this difference or if it's just an
omission.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top