relative import packages/modules workaround

Discussion in 'Python' started by peter, Jul 11, 2005.

  1. peter

    peter Guest

    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
     
    peter, Jul 11, 2005
    #1
    1. Advertisements

  2. 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, Jul 11, 2005
    #2
    1. Advertisements

  3. peter

    peter Guest

    hmm,

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

    hopefully this will be introduced fast in python
     
    peter, Jul 12, 2005
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.