473,407 Members | 2,676 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,407 software developers and data experts.

relative import packages/modules workaround

Hello all,

I've read all the posts concerning relative imports of modules and I am
glad I saw some consensus on a syntax.
At this moment I am however bound to python 2.3.

Does anyone know a good workaround (or a library) which can achieve a
relative import for this python version

kind regards

Peter

Jul 21 '05 #1
2 1411
On Monday 11 July 2005 09:19 am, peter wrote:
I've read all the posts concerning relative imports of modules and I am
glad I saw some consensus on a syntax.
At this moment I am however bound to python 2.3.

Does anyone know a good workaround (or a library) which can achieve a
relative import for this python version


Basically, you can use information from the sys module to dynamically
determine your module or package's absolute path. Then you construct
the absolute path to the module you want by a fairly simple application
of os.path from the relative path.

How exactly to do that is not something I remember off the top of my
head, but I remember it's not hard -- check the "sys" and "os.path"
modules' documentation. You probably also want to look at the
"__path__" property of packaged loaded with __init__.py, as I believe
there is some special "magic" behavior designed for packages to make
this a little easier.

You can easily encapsulate this in a function under your module with
just a few lines of code. Zope provides a function called "package_home"
that does this. I'm sure there are other examples.

The missing thing here is not that there's no way to do it, but rather
that it's a common enough task that there ought to be *one* way to do
it and it ought to be in the library. Otherwise, there's a lot of
redundancy.

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 21 '05 #2
hmm,

it seems to be less trivial than you mentioned...

hopefully this will be introduced fast in python

Jul 21 '05 #3

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

Similar topics

0
by: Remy C. Cool | last post by:
Hello, My application uses a remote import scheme using the sys.path_hooks solution as explained in PEP302 (http://www.python.org/peps/pep-0302.html) The importer works fine for packages,...
0
by: Remy C. Cool | last post by:
Hello, A couple of months ago, I posted a question related to the same problem, but didn't get any replies. I've created a ugly workaround but still would like to get some insight on the matter....
0
by: Anders J. Munch | last post by:
Now 2.5 is out, and we have a syntax for explicit relative imports (PEP 328, http://www.python.org/dev/peps/pep-0328/, in case anyone wasn't paying attention). The long-term plan is that the...
12
by: Alan Isaac | last post by:
Are relative imports broken in 2.5? Directory ``temp`` contains:: __init__.py test1.py test2.py File contents: __init__.py and test2.py are empty test1.py contains a single line::
0
by: Kay Schluehr | last post by:
Since their introduction in Python 2.5 I only reviewed the new "relative import" notation briefly by reading the "What's new in Python 2.5" article. Now I wanted checkout if I get comfortable with...
0
by: John Millikin | last post by:
With the old import system, and a package that looks like this: foo/ __init__.py main.py bar/ __init__.py baz.py If I wanted to delay importing baz until it was actually used, I could
5
by: John Ladasky | last post by:
Hi folks, Running Python 2.5 on both a Windows XP laptop, and an Ubuntu Linux 7.04 desktop. I've gotten tired of maintaining multiple copies of my personal modules that I use over and over. ...
7
by: Robert Hancock | last post by:
mypackage/ __init__.py push/ __init__.py dest.py feed/ __init__py subject.py In subject.py I have
4
by: DG | last post by:
Alright, I have searched and searched and read many conversations on the topic of relative and absolute imports and am still not getting the whole thing through my skull. Highlights of what I've...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...

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.