Duplicate modules problem

David Bolen db3l at fitlinxx.com
Sat Feb 24 02:34:26 CET 2001

Chuck Esterbrook <echuck at mindspring.com> writes:

> Regarding '' in sys.path, if you say:
>    > python someprog.py
> you get '' as the first element in the path. Again that's Python, not me.
> However, if you say:
>    > someprog.py
> then you get an absolute path in place of ''. Geoff ran into this first and 
> it caused problems, hence the fix. Since the fix puts you back to the 
> sys.path you would have gotten from Python anyway (by launching 
> differently), it doesn't feel so evil to me.

I expect that in both cases Python is just consistently taking the
dirname off of the script it is supplied on the command line and
inserting it as the first search entry (to include the script location
for imports).

When you run the script directly in the second case, I expect the
surrounding environment (Windows?) is using the full path when it
calls python.

> Regarding my choice of:
>    os.chdir(os.pardir)
> I could done something like this instead:
>    sys.path.insert(1, os.path.abspath(os.pardir))

You'd want to use 0 to put it at the front of the path, right?

> The first seemed a tad simpler.

Simpler, but also environmentally more damaging since it affects an OS
environmental item that can affect non-Python code that might
eventually run in the process.

I'm not that familiar with how your application might be launched, but
if there's any chance of this code path being imported and run that
way (rather than directly from the command line), then I'd definitely
vote for the latter, since the former changes the current directory
for the existing process which might be unexpected by someone
importing your launch module.  Better to adjust a Python internal item
such as sys.path than an OS-environment feature, at least IMHO.

Even if it isn't generally expected to start that way, if there's any
good reason someone might wrap that module in their own code it might
prove unexpected.

I seem to have missed a post or two in this thread on my server, so
perhaps this violates some other requirement, but is there a reason
you just can't remove the first entry from sys.path in your launcher?

Since that's the location of the script, removing it would force
Python to use the normal search path, which your package should
already be on in order to be a package to Python?

-- David
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \

More information about the Python-list mailing list