473,396 Members | 1,921 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,396 software developers and data experts.

sys.path and importing modules from other directories

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__
- - something
- other\
- - __init__
- - working_on

So when I am busy with 'working_on' and want to import from common
something it won't let me do that because I don't have pkg_root in my
path. No biggy here, I just add the pkg_root to the path. Once I am
finished most of the time all modules are initiated from a module
directly at the pkg_root level so then importing works without worries.

However it has irritated me enough that I wrote a little script I called
'relative_path.py' that I put in my python path that does these things
for me, so that I only need to import that one and set the parent path,
probably I missed a point here somewhere but if not maybe somebody else
finds it useful.

So while I am developing the depending modules, these modules begin with:
import relative_path
relative_path.add_package_root()
+++
import os, sys

def getparent(depth=0):
"Returns the absolute pathname of (grand)parents directory."
# By default it returns the current working directory.
# The level of 'grand' is set by depth.

getcwd = os.getcwd
sep = os.sep
abspath = os.path.abspath
if depth == 0:
grand = None
else:
grand = depth * -1

# Main function
# From the current working directory get the absolute path,
# split it and join the wanted portion of the list.
pathname = sep.join(abspath(getcwd()).split(sep)[:grand])

# Make sure to throw an exception if depth results in an empty string.
if len(pathname) == 0:
raise_string = "(grand)Parent depth of %s is before root." % depth
raise(LookupError(raise_string))
else:
return(pathname)
def add_package_root(directory=1):
"Add a parent directory relative to cwd to sys.path, default the
first."
sys.path.append(getparent(directory))
+++

--
mph
Apr 6 '08 #1
0 1495

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

Similar topics

3
by: Jeff Wagner | last post by:
Is it possible to append a new Path to some file permanently? It seems like a sys.path.append('/my/new/path') statement is temporary. In other words, where and in what file on a Win32 box or...
4
by: Irmen de Jong | last post by:
Hello, I don't understand why the following doesn't work. What I want to do is dynamically import some generated Python code and I'm doing this using compile and exec'ing it in the dict of a new...
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? ...
3
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...
2
by: Vsevolod (Simon) Ilyushchenko | last post by:
Hi, Last year I have written a Perl module to serve as a backend to Macromedia Flash applications: http://www.simonf.com/amfperl I have just rewritten it in Python, which I have not used...
4
by: Jesse B. | last post by:
I have just begun to learn python using o'reilly's Learning Python, and in chapter 3 it talks about importing modules. It has me make modules to practice using the 'import' and 'from' statements...
4
by: gnah | last post by:
Greetings, I hope my problem is easy to fix, I'm pretty new with php - but I am getting weird results with the opendir() function. It may just be a path problem, but I don't see which variable...
2
by: Dr. Pastor | last post by:
I installed Python 2.5 on Windows XP. I got the following system that works well. --- Python 2.5b3 (r25b3:51041, Aug 3 2006, 09:35:06) on win32 Type "copyright", "credits" or "license()" for...
17
by: mohan | last post by:
Hi Guys, I've been using the following IDE, "Pythonwin - Python IDE and GUI Framework for Windows. Copyright 1994-2001 Mark Hammond " With respect to my work, I had created my own modules...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...

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.