4
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by lrh9
Featured Replies
  • 1

    Try relative imports. [url]http://www.python.org/dev/peps/pep-0328/#guido-s-decision[/url] Seem like just the thing. I've used them. They permit you to import modules from a relative parent package. They're hard to figure out though. The documentation is somewhat lacking so you might have to tinker until you get it right. Read More

0

I think it would be

import os
os.path.join('root/module1')
os.path.join('root/module2')
import somescript
import someotherscript
0

You need that root and root/module2 be in sys.path which can be done like this in somescript :

import os
import sys
rootpath = os.path.dirname(os.path.abspath(os.curdir))
sys.path.append(rootpath)
mod2path = os.path.join(rootpath,'module2')
sys.path.append(mod2path)
import common
import someotherscript

Of course you also need to be sure that all these things exist; it would be nice not to double add them (doesn't actually hurt) to sys.path; and other niceties. You may want to create an empty file named __init__.py in the various directories. See http://effbot.org/pyfaq/what-is-init-py-used-for.htm.

Edited by griswolf: n/a

0

I'm not a fan of, for example from canvas import point because I've spent far too much of my time dealing with subtle bugs introduced by, for example, from coastline import point in the same chunk of code. (Mostly, I admit, not in Python). I just like the self documentation and safety of seeing for example

import canvas
import coastline
myCorner = canvas.point(200,400)
myPromontory = coastline.point(43.32576, -124.38773)

Thus, I'm also not very fond of relative imports. Not saying "bad", but just "I dunno, maybe not usually for me."

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.