473,320 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Customizing the python search path depending on source directory

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
3 2340
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
3
by: Stephen Ferg | last post by:
I need a little help here. I'm developing some introductory material on Python for non-programmers. The first draft includes this statement. Is this correct? ...
1
by: M.E.Farmer | last post by:
Hello c.l.py!, I have just finished this and decided to share. PySourceColor is a module to convert Python source into colored html. Yes it has been done before, but I like this better:) You can...
2
by: Roose | last post by:
With some googling I have found these resources: http://docs.python.org/ext/win-dlls.html http://www.python.org/doc/faq/windows.html I have a large Win32/MFC/C/C++ application that has an...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.