473,725 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Safest manner to extend search path for modules?

Hi,

What is the safest manner to extend search path for modules, minimizing
the likelihood of shooting oneself in the foot?

The system (which includes scripts and their shared modules) may be
checked out in several different locations, but a script in a
particular checked-out version of the system should only use modules
from that checkout location.

e.g. if the system contains a directory scripts/foo/ where all the
scripts are housed, and scripts/modules/ where all the modules are
housed, then is it correct for each script in scripts/foo/ to begin
with:
import sys, os.path
sys.path.append (os.path.join(s ys.path[0], "../modules"))

If so, is there a cleaner way of doing this than including the above
text in all scripts?

Thanks,

Joseph

Jul 26 '05 #1
2 1542
"Joseph Turian" <tu****@gmail.c om> writes:
Hi,

What is the safest manner to extend search path for modules, minimizing
the likelihood of shooting oneself in the foot?

The system (which includes scripts and their shared modules) may be
checked out in several different locations, but a script in a
particular checked-out version of the system should only use modules
from that checkout location.

e.g. if the system contains a directory scripts/foo/ where all the
scripts are housed, and scripts/modules/ where all the modules are
housed, then is it correct for each script in scripts/foo/ to begin
with:
import sys, os.path
sys.path.append (os.path.join(s ys.path[0], "../modules"))

If so, is there a cleaner way of doing this than including the above
text in all scripts?


Put that text in a module that is already on the path, and then import
that module.

Or mabye generalize it into a function:

import sys, os.path

def fixpath(*new):
sys.path.extend (os.path.join(s ys.path[0], x) for x in new)

This requires 2.4. Change it to ([os.path ... new]) for earlier versions.

Of course, I note that sys.path[0] is '', so the join doesn't do anything.

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 27 '05 #2
On 25 Jul 2005 21:50:20 -0700, Joseph Turian <tu****@gmail.c om> wrote:
What is the safest manner to extend search path for modules, minimizing
the likelihood of shooting oneself in the foot?


Put a .pth file in a directoy already on the system path.

http://bob.pythonmac.org/archives/20...n-development/

--
Daniel Bickett
dbickett at gmail.com
http://heureusement.org/
Jul 27 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9729
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? ----------------------------------------------------------------- When loading modules, Python looks for modules in the following places in the following order:
3
2361
by: Peter Schwalm | last post by:
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...
5
10426
by: Russ | last post by:
I have a Python program that I want to run in Jython so I can get Java bytecode output. The program runs fine in Python, but when I change the first line of the main program to make it run in Jython, it fails to find some of the imported modules. These are just plain Python imports of code I wrote myself in another directory. Apparently Jython does not use the PYTHONPATH environment variable. I created an environment variable called...
2
2917
by: mr_gees100_peas | last post by:
Hi, I've been trying for days to make either boost.python or swig to work for me. The one I have gotten the closest to is boost. Note that this is for windows XP. I'm not much of an unix person besides doing simple ls and copy paste. What I have done is to download the boost library: C:\boost_1_34_0 I downloaded the bjam library: C:\boost-jam-3.1.14-1-ntx86
5
1900
by: grt | last post by:
I'm new to python, running it under cygwin. This must be easy but I haven't figured it out. I'm trying to run a script by typing: python test.py where test.py is in a directory I've added to PYTHONPATH, but test.py is not found. Perhaps PYTHONPATH only applies to modules imported within a script. I didn't notice an option to python to search the standard path, like Perl -S.
1
1466
by: mhearne808[insert-at-sign-here]gmail[insert-dot-he | last post by:
I think I don't understand how the module search path works... Let's say I have a folders called 'test'. Underneath it, I create two more folders called 'foo' and 'bar'. In 'foo', I create an empty '__init__.py' file, indicating that this folder is a package 'foo'. I then create a simple python script 'foo.py' consisting of the following code: ----------------------------
3
3345
by: =?iso-8859-1?q?Christian_R=FChl?= | last post by:
Hi folks! I have a little noob problem here with XSLT. I have a XML file that looks like this: <archiveFiles> <module path="c:/temp/module_m17_blabla.tmp"/> <module path="c:/temp/module_m34_blabla.tmp"/> ... <moduleAddition path="c:/temp/moduleaddition_m17.tmp"/>
0
1523
by: Martin P. Hellwig | last post by:
Hello all, I had some troubles in the past how to arrange my packages and modules, because I usually don't develop my stuff in the Lib\site-packages directory I have some troubles when importing depending modules that are in 'sibling' directories. Like in the following scenario: pkg_root\ -__init__ - common\ - - __init__
8
1563
by: tow | last post by:
I have a python script (part of a django application, if it makes any difference) which is exhibiting the following behaviour: import my_module # succeeds imp.find_module("my_module") # fails, raising ImportError which is completely baffling me. According to sys.path, both should fail; the directory containing my_module is not in sys.path (though the my_module directory itself is). More puzzlingly, printing out my_module.__file__...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.