473,698 Members | 2,370 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 1538
"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
9726
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
2357
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
10421
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
2913
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
1897
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
1463
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
3344
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
1517
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
1562
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
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
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,...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7723
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
6518
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
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2328
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
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.