0

I have 3 directories under S1

S1
   - D1
       --python1.py
   - D2
       --python2.py
   - D3
       --python3.py

In python3.py module, I have a statement(below) to import python2.py

import D2.python2

when I run python3.py I get a error " No module named D2.python2"

How can I fix this issue?

Note: I have __init__.py in all the directories to signify packages.

2
  • 2
    Is S1 on your module search path? Commented Apr 24, 2014 at 15:23
  • i think you have to write from .. import D2.python2, but a python program cannot import from higher directories than the directory it was started, so python3 (with the python2-import-statement) has to be importet from a file in s1 (or higher) Commented Apr 24, 2014 at 15:25

1 Answer 1

2

You need to make sure your S1 directory is on your python module search path so that the interpreter knows how to load it.

The easiest way is to add it to your PYTHONPATH environment variable.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it not easier to put the D1-, D2- and D3-directories in the site-packages?
Given D* have interdependencies it seems like S1 is intended to be one package; so I'd rather put S1 in there than the sub-packages. Either way, putting personal code in site-packages ties it unnecessarily to one particular python install in my opinion, making it hard to use virtualenv and just generally inconvenient. I'd rather use PYTHONPATH, or if intended to be available systemwide add a symlink into site-packages rather than just installing it there.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.