1

I have ran into strange problem, which I cannot find an answer.

I want to use file which may be located in different modules, with same path names (folders contain empty init.py files as well):

road1/pato/
road2/pato/modtest.py

where modtest contains simply a=1 Simple script for testing, test.py , contains:

import pato.modtest
print(pato.modtest.a)

and running

PYTHONPATH=road2/ python test.py

runs fine as expected. What is confusing, is that

PYTHONPATH=road1/:road2/ python test.py

gives an error

ImportError: No module named 'pato.modtest'

All the documentation I have read states that PYTHONPATH may contain multiple path-s and it should be just fine, running program is just looking through them in order. In this case, however, adding empty path in the front of path seem to prevent reading from later path's. If this is expected behaviour, fine, I'd appreciate links to good docs about it.

3
  • what OS are you running this on? Commented Jun 30, 2015 at 19:58
  • It is OS X 10.10.3, tried both python2.7 and python3.4 Commented Jun 30, 2015 at 20:17
  • gotcha, I was wondering if it was a delimiter issue, but Konrads is right Commented Jun 30, 2015 at 20:18

1 Answer 1

2

You have a namespace clash.

  • According to your PYTHONOPATH, when you import "pato.modtest" Python first looks if "pato" or "pato.modtest" are present in the current namespace.
  • As they are not present it then goes to sys.path and tries the first path which in your case is "road1/".
  • It finds the module "pato" there and then looks for object "modtest", not having found, it looks for a module road1/pato/modtest, not having found, it gives up.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I think I can live with that explanation. It is still not entirely clear to me, why Python does not look object from the next module if it is available, as it would with any regular file.
Because Python had already found "pato", it has no reason to think there is another one named the same way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.