This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Import fails in Idle
Type: Stage:
Components: IDLE Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: kbk Nosy List: jepler, kbk, lesliebrooks, nnorwitz
Priority: normal Keywords:

Created on 2002-10-07 14:15 by lesliebrooks, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Messages (8)
msg12612 - (view) Author: Leslie Brooks (lesliebrooks) Date: 2002-10-07 14:15
The import command works in the command line
interpreter, but fails in Idle:

>>> os.listdir('.')
['Debug', 'example.cxx', 'example.dsp', 'example.h',
'example.i', 'example.ncb', 'example.py',
'example.pyc', 'example.sln', 'example.suo',
'example.vcproj', 'example_wrap.cxx', 'foo',
'index.html', 'Makefile', 'runme.py', '_example.dll',
'_example.ilk']
>>> import _example
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in ?
    import _example
ImportError: No module named _example
>>> 

     I have gotten it to work _once_ in Idle, but was
never able to repeat it.

     I am running Python 2.2.1 under Win2K (5.00.2195 SP3).
msg12613 - (view) Author: Leslie Brooks (lesliebrooks) Date: 2002-10-07 21:53
Logged In: YES 
user_id=271417

     I should have noted that 'import' _does_ work to a
degree - 'import os' worked.

     The '_example.dll' that I am trying to import is the
'shapes' example that is distributed with SWIG 1.3.15, and
should import without any trouble.  It does import just fine
from the command line version of Python, but not from Idle.
msg12614 - (view) Author: Jeff Epler (jepler) Date: 2002-10-13 13:02
Logged In: YES 
user_id=2772

Don't shared modules need the extension .pyd instead of .dll?

Is sys.path[0] some absolute directory (the one containing
idle.py, for instance) instead of '.'?

Is there a way to run idle with -v so that it lists
directories and files it tries to import as modules?
msg12615 - (view) Author: Leslie Brooks (lesliebrooks) Date: 2002-10-14 13:15
Logged In: YES 
user_id=271417

> Date: 2002-10-13 09:02
> Sender: jepler
> Logged In: YES 
> user_id=2772
> 
> Don't shared modules need the extension .pyd instead of .dll?
> 

No, I am able to load the .dll from the command line version
of Python.
Also:
   >>> imp.get_suffixes()
   [('.pyd', 'rb', 3), ('.dll', 'rb', 3), ('.py', 'r', 1),
('.pyw', 'r', 1), ('.pyc', 'rb', 2)]
shows that Python is searching for DLLs.

> Is sys.path[0] some absolute directory (the one containing
> idle.py, for instance) instead of '.'?
> 

Ahh, that is the true problem.  sys.path in Idle is:
   >>> sys.path
   ['C:\\PROGRA~1\\Python22\\Tools\\idle',
'C:\\PROGRA~1\\Python22', 'C:\\Program
Files\\Python22\\DLLs', 'C:\\Program 
   Files\\Python22\\lib', 'C:\\Program
Files\\Python22\\lib\\lib-tk', 'C:\\Program
Files\\Python22', 'C:\\Program Files
   \\Python22\\lib\\site-packages']
while sys.path in the command line interpreter is:
   >>> sys.path
   ['', 'C:\\PROGRA~1\\Python22', 'C:\\Program
Files\\Python22\\DLLs', 'C:\\Program Files\\Python22\\lib',
'C:\\Program
   Files\\Python22\\lib\\lib-tk', 'C:\\Program
Files\\Python22', 'C:\\Program
Files\\Python22\\lib\\site-packages']

So the correct title for this bug report should be "Idle
replaces sys.path[0] rather than appending a new entry to
the list".  This means that programs that work when run
through the command-line interpreter may break quite
dramatically when run from Idle, and vice versa.  

Leslie
msg12616 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2002-12-15 01:39
Logged In: YES 
user_id=33168

Leslie, do you have this problem with 2.2.2?  If so, can you
try idlefork?  See http://sf.net/projects/idlefork
msg12617 - (view) Author: Kurt B. Kaiser (kbk) * (Python committer) Date: 2002-12-19 01:17
Logged In: YES 
user_id=149084

On Linux, using 2.2.1, I don't see the problem.  IDLE starts with
'' as sys.path[0] and expands it to the absolute path of the 
directory in which it was started.

Idlefork on Linux is the same.

Python 2.2.1 (#1, Dec 14 2002, 21:10:27) 
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> import sys
>>> sys.path
['/home/kbk', '/usr/lib/python2.2/Tools/idle', '/usr/lib/python2.2', '/usr/lib/python2.2/plat-linux2', '/usr/lib/python2.2/lib-tk', '/usr/lib/python2.2/lib-dynload', '/usr/lib/python2.2/site-packages']
>>> 
msg12618 - (view) Author: Kurt B. Kaiser (kbk) * (Python committer) Date: 2002-12-19 18:07
Logged In: YES 
user_id=149084

In W2K I changed the Start In item in the IDLE button
properties to 
d:\mydoc\PythonFiles, and then ran
a short script saved in that 
directory.  Note that the
Start In directory becomes sys.path[1].  I'm 
looking at
a cleaner way to do this in Idlefork, probably by 
adding
the directory of the current file to sys.path.  You
could also 
frob IDLE's sys.path.

Python 2.2.1 (#34, Apr  9 2002, 19:34:33) 
[MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" 
for more information.
IDLE 0.8 -- press F1 for help
>>> import 
sys
>>> sys.path
['D:\\PROGRA~1\\PYTHON\\Tools\\idle', 
'D:\\mydoc\\PythonFiles', 'D:\\Program Files\\Python\\DLLs', 
'D:\\Program Files\\Python\\lib', 'D:\\Program Files\\Python\\lib\\lib-
tk', 'D:\\Program Files\\Python', 'D:\\Program Files\\Python\\lib\\site-
packages']
>>> import foo

Foo is a script which will print its 
current working directory

D:\mydoc\PythonFiles

>>> 
import os
>>> os.listdir(".")
['foo.py', 'foo.pyc']
>>> 
msg12619 - (view) Author: Kurt B. Kaiser (kbk) * (Python committer) Date: 2003-07-09 04:51
Logged In: YES 
user_id=149084

.../Lib/idlelib PyShell.py Rev 1.67
Also fixed in IDLEfork.
History
Date User Action Args
2022-04-10 16:05:43adminsetgithub: 37278
2002-10-07 14:15:03lesliebrookscreate