By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,766 Members | 1,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,766 IT Pros & Developers. It's quick & easy.

Customizing the python search path depending on source directory

P: n/a
I'd like to modify the python search path depending on the source
directory of the script being started. The reason is: I use a version
control system, and the python scripts and modules are migrated
through several stages before they are used in production. A python
script running in a development environment should use different
modules - possibly in other directories then the script source
directory - than the same script running in a production environment.

Ideally this should work without any modifications to the python
programs, and the search path should be set up before the script
invoked by the user gains control.

My idea was to use site.py / sitecustomize.py in lib/site-packages and
to have sitecustomize.py invoke a directory-specific customizing
module - say dirCustomize.py - located in the script source directory.

But it seems that this is the wrong place, because at the moment
sitecustomize is imported

a) the source directory of the script is still not in sys.path
b) sys.argv is still not set, so I can't find out the source
directory via sys.argv[0] and temporarily add it to sys.path for the
invocation of dirCustomize.py.
c) also sys.modules["__main__"].__file__ is still not set

The following "sitecustomize.py" shows these difficulties:
#-------------------------------------------------------------------------
print "start siteCustomize.py ..."
import sys
import os

try:
print "argv=", sys.argv
except AttributeError:
print ""
print "sys.argv does still not exist!"

try:
print "sys.modules[\"__main__\"]=",
sys.modules["__main__"].__file__
except AttributeError:
print ""
print "sys.modules[\"__main__\"].__file__ does still not exist"

# ... shows that source directory is still not in sys.path
print "sys.path="
for ix1, p1 in enumerate(sys.path): print "%02.02d: %s" % (ix1, p1)

print "... end siteCustomize.py"
#-------------------------------------------------------------------------

I'd not like to use the current working directory instead of the
script directory, because the scripts may be started from anywhere
directories by specifying the path name in the invocation.

Does anyone have an idea?

Thank you in advance,
Peter
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Peter Schwalm wrote:
My idea was to use site.py / sitecustomize.py in lib/site-packages and
to have sitecustomize.py invoke a directory-specific customizing
module - say dirCustomize.py - located in the script source directory.

But it seems that this is the wrong place, because at the moment
sitecustomize is imported

a) the source directory of the script is still not in sys.path


Are you sure? Looking at site.py, it appears that the sitecustomize
import is done next-to-last, just before sys.setdefaultencoding is
deleted. If you concluded this by examining sys.path manually at
the point just as sitecustomize is imported, maybe you don't know
that the '' entry in sys.path actually represents the source
directory of the main script...

It would seem your suggested solution above should actually work...

-Peter
Jul 18 '05 #2

P: n/a
Peter Hansen <pe***@engcorp.com> wrote in message news:> >
.....
a) the source directory of the script is still not in sys.path


Are you sure? Looking at site.py, it appears that the sitecustomize
import is done next-to-last, just before sys.setdefaultencoding is
deleted. If you concluded this by examining sys.path manually at
the point just as sitecustomize is imported, maybe you don't know
that the '' entry in sys.path actually represents the source
directory of the main script...


Thank you for your answer. But I'm afraid it's not the source
directory but the current working directory. You can see the
difference if you copy the passage

"for ix1, p1 in enumerate(sys.path): print "%02.02d: %s" % (ix1,
p1)"

to a "normal script". If you start that "normal script" from a
directory other than the source directory, you can see that both
directories are included in sys.path. If you run this code inside
sitecustomize.py this is not the case.

Thank you
Peter
Jul 18 '05 #3

P: n/a
Peter Schwalm wrote:
Peter Hansen <pe***@engcorp.com> wrote in message news:> >
Thank you for your answer. But I'm afraid it's not the source
directory but the current working directory. You can see the
difference if you copy the passage

"for ix1, p1 in enumerate(sys.path): print "%02.02d: %s" % (ix1,
p1)"

to a "normal script". If you start that "normal script" from a
directory other than the source directory, you can see that both
directories are included in sys.path. If you run this code inside
sitecustomize.py this is not the case.


On the contrary. When I start that from another directory which
is *not* in the path, I do not see that directory in the sys.path
at all, but merely the directory in which the script itself is
stored. This is the defined behaviour of the Python sys.path
stuff and you must be misinterpreting something.

The current directory is *not* added to sys.path, unless it
just happens that the main script is in the current directory.

-Peter
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.