[Patches] [ python-Patches-946373 ] do not add directory of sys.argv[0] into sys.path

SourceForge.net noreply at sourceforge.net
Wed Feb 16 18:31:52 CET 2005


Patches item #946373, was opened at 2004-05-02 14:51
Message generated for change (Comment added) made by wrobell
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=946373&group_id=5470

Category: Core (C code)
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: wrobell (wrobell)
Assigned to: Nobody/Anonymous (nobody)
Summary: do not add directory of sys.argv[0] into sys.path

Initial Comment:
Python adds magically directory of sys.argv[0] into
sys.path, i.e.

>>> import sys
>>> sys.path
['', '/usr/lib/python23.zip', '/usr/share/python2.3',
'/usr/share/python2.3/plat-linux2',
'/usr/share/python2.3/lib-tk',
'/usr/lib/python2.3/lib-dynload',
'/usr/lib/python2.3/site-packages',
'/usr/lib/python2.3/site-packages/gtk-2.0',
'/usr/share/python2.3/site-packages']

where '' (or /usr/bin when executed script is in
/usr/bin directory, etc.) is added automatically.

It is useful in many circumstances but fails when name
conflict occurs.
For example, create getpass.py or pdb.py scripts which
import getpass and pdb modules. Script names conflict
with modules names and modules
are not going to be imported because path to the
scripts is appended
into sys.path, so a script is imported instead of a module.

The solutions:
1. User of script with conflicting name (i.e.
getpass.py or timeit.py)
can set PYTHONPATH to system library path, i.e.
/usr/lib/python2.3.
2. User can modify the script to delete site.path[0].
3. User can rename the script.
4. Python can be modified to not add magically
directory of sys.argv[0].

The 1. is a tricky and not intuitive and quite funny:
set PYTHONPATH to system library path to import system
module (and only in
specific circumstances). ;-P

The 2. is a dirty hack: hey, we gonna import system
module, ain't it?

The 3. is, IMHO, not acceptable because there is more
than 200 python system modules, more in the future and
user cannot be forced to maintain script names blacklist.

The 4. is only acceptable, IMHO. It makes python more
inconvenient
but gives no trouble when names conflict occurs.
Moreover, fourth
solution makes python more standard with other languages
behaviour, i.e. one has to set CLASSPATH to load Java
classes.

Maybe there is another solution, but...

Patch attached.

----------------------------------------------------------------------

>Comment By: wrobell (wrobell)
Date: 2005-02-16 18:31

Message:
Logged In: YES 
user_id=387193

But the problem is not with naming of my modules/packages
(part about relative import of modules I do understand, of
course), but with script naming.

For example consider script:

  #!/usr/bin/python
   import email
   print 1

And name the script email.py, then run it, please. Python
tries to be too smart (IMHO) and variable sys.path is
polluted with directory of email.py script, therefore
standard email Python package will not be imported.

----------------------------------------------------------------------

Comment By: Josiah Carlson (josiahcarlson)
Date: 2005-02-16 17:50

Message:
Logged In: YES 
user_id=341410

If the entirety of PEP 328 made it into Python 2.4 (I don't
have an installation of 2.4, so don't know), to import your
'email.py' module, you would use 'from . import email' after
enabling the absolute import semantics with 'from __future__
import absolute_import'.  You would then import the standard
email package with 'import email'.

Is this not clear by reading PEP 328?

----------------------------------------------------------------------

Comment By: wrobell (wrobell)
Date: 2005-02-16 15:37

Message:
Logged In: YES 
user_id=387193

I am opening it again to discuss it a little more...
Question to Josiah Carlson or anybody who can answer:

  How PEP 328 is going to solve problem I have described?

If I name my script email.py, which will try to import email
standard Python
package. Then run the script, it will import itself instead
of Python package,
because directory where email.py is installed is added to
sys.path.

----------------------------------------------------------------------

Comment By: wrobell (wrobell)
Date: 2004-10-26 18:42

Message:
Logged In: YES 
user_id=387193

i will not provide the patch for 328, so closing this issue

----------------------------------------------------------------------

Comment By: Johannes Gijsbers (jlgijsbers)
Date: 2004-10-07 22:21

Message:
Logged In: YES 
user_id=469548

wrobell, would you be willing to produce a version of the
patch which implements PEP 328? I'll close this patch if not.

----------------------------------------------------------------------

Comment By: Josiah Carlson (josiahcarlson)
Date: 2004-05-21 01:55

Message:
Logged In: YES 
user_id=341410

This "problem" will be fixed in Python 2.4 with the
introduction of absolute and relative import semantics as
given in PEP 328:

http://www.python.org/peps/pep-0328.html

As stated in the PEP, to use the obviously backwards
incompatible semantics, the future import will be used for
2.4 and 2.5, where in 2.6 it will become the default.

from __future__ import absolute_import


----------------------------------------------------------------------

Comment By: Anthony Baxter (anthonybaxter)
Date: 2004-05-12 17:34

Message:
Logged In: YES 
user_id=29957

I've been bitten by this before. See e.g. the shtoom.py
script clashing with the shtoom package. I used the hacky
approach of moving '' to the end of sys.path. While it would
be nice if this wasn't needed, I can't see this being
anything other than a backwards compatibility nightmare. It
will absolutely break a lot of things to change it.


----------------------------------------------------------------------

Comment By: Ilya Sandler (isandler)
Date: 2004-05-08 01:45

Message:
Logged In: YES 
user_id=971153


Would not this cause serious backward compatibility problems??




----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=946373&group_id=5470


More information about the Patches mailing list