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.

Author terry.reedy
Recipients markroseman, terry.reedy
Date 2015-10-27.09:25:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1445937913.03.0.194834202195.issue25488@psf.upfronthosting.co.za>
In-reply-to
Content
Problem: Consider module idlelib.run.  The following is correct.

C:\Users\Terry>python -c "import run"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named 'run'

But in IDLE started from icon or command line
>>> import run
>>> 

With respect to the goal of developing code that can run outside of IDLE, not raising ImportError is a bug. Suppose a user erroneously writes 'import run' or 'import help' (in repository IDLE).  The error could be that the file is 'runner' or 'helper' or that 'run' or 'help' is not in the same directory. The import should fail rather than the user getting a error later because the wrong file was imported.

Reason: When python runs 'somepath/file.py', it prepends 'somepath' to sys.path.  So when python runs '.../idlelib/whatever.py', it prepends '.../idlelib' to sys.path.  This occurs in both IDLE and user processes.

When 'somepath/file.py' is either on startup or from the editor, IDLE first prepends 'somepath' to sys.path just as python does.

We are planning to add perhaps 50 new short lowercase (PEP8) names to idlelib.  This will greatly increase the possibility of accidentally matching something users write, if only in error, or even of  masking a stdlib module.

Solution: In PyShell.main and run.main, remove the prepended idlelib entry *if it is there* (see below).  On Windows, at least, for installed python, sys.path would then be identical on startup whether a file is run with python or IDLE.  

Would this affect IDLE itself?  I am sure not. AFAIK, IDLE does not exploit the presence of idlelib on sys.path, and always imports idlelib files via idlelib.

Further more, when IDLE is started from console Python with 'import idlelib.idle', which I regularly do for the repository version, '.../idlelib' is *not* prepended.  This is why IDLE should not depend on its presence and why removal should check first.  This also means that whether a file runs in IDLE depends on how IDLE is started, whether directly or via '>>> import idlelib.idle' within console python.


Note 1. idlelib.idle prepends repository 'lib' even when already present; this could be fixed also.  Another issue to track down, probably not due to IDLE, is that at least on Windows the binary directory is present twice.)

Note 2: The contents of sys.modules would still be different, along with the problem that 'import tkinter; tkinter.font' runs in IDLE.  (This example comes from a Stackoverflow question.) But this is a different issue and I cannot see a fix for it.
History
Date User Action Args
2015-10-27 09:25:13terry.reedysetrecipients: + terry.reedy, markroseman
2015-10-27 09:25:13terry.reedysetmessageid: <1445937913.03.0.194834202195.issue25488@psf.upfronthosting.co.za>
2015-10-27 09:25:12terry.reedylinkissue25488 messages
2015-10-27 09:25:11terry.reedycreate