This is the folder structure:

root
	common.py
	module1
		somescript.py
	module2
		someotherscript.py

i need to import common.py from somescript.py and someotherscript.py

I also need to import somescript.py from someotherscript.py

I think it would be

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

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.

Try relative imports.

http://www.python.org/dev/peps/pep-0328/#guido-s-decision

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.

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."

I'm talking about dotted relative imports.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.