views:

178

answers:

4

Hi , I am a newbie to python. I do have two modules. Model M1 and module m2. From m2 , i need to refer m1 and m2 and m1 resides at two different locations in disk.

When I am trying to import m1 before executing m2 , of course it's saying can't find m1. How I can point my interpreter to m1's location.

Thanks J

+3  A: 

It's not entirely clear what your specific problem is (give more details!), but you may find these useful (further Googling will help you reach concrete answers for your specific needs):

  • The PYTHONPATH environment variable
  • .pth files in directories that appear in PYTHONPATH
  • Manipulating sys.path before importing

However, if m2 depends on m1, and they're distributed together, perhaps it's a better idea to place them in the same directory tree using packages.

Eli Bendersky
+2  A: 

If you can't modify the shell environment, you can append any directories you want the interpreter to search for modules to sys.path from within your script. In fact, the PYTHONPATH environment variable is read and used to initialize sys.path.

Chris Connett
A: 

Hi,

What's possible depends on the details of the modules, but usually you can just import the specifics objects needed from the modules, like this:

in B.py

from A import classA1, funA1

in A.py

from B import classB1, funB1

so that you only import what's needed. If the dependencies are more complex, this may not work, but in general it should be possible (unless you actually have real mutual, recursive dependencies at the object level which you can't resolve!).

Andrew Jaffe
A: 

Thanks you all. I tried sys.path . It solves my problem.

Jijoy