4

I am working on a project that requires to have several modules with the same name. This is a representative extract of the architecture, with __init__.py files to show which folders are modules:

/path1
    /ProjectA
        __init__.py
        /src
            __init__.py
            /ctrl
                __init__.py
                somectrl.py
            ...
/path2
    /ProjectA
        __init__.py
        /src
            __init__.py
            someclass.py

And in my class someclass.py, I want to import somectrl.py like this :

from ProjectA.src.ctrl import somectrl

But the import fails: it tells me that there is no ctrl package. Seems like it just looks into ProjectA from path2, and completely ignores ProjectA from path1 ! Both path1 and path2 are in my PYTHONPATH. So can't they both be reached?

Is there a clean way out of this nasty situation?

3
  • 2
    Except making "path1" and "path2" packages on their own I don't see much solution here. Commented Oct 1, 2015 at 9:48
  • 5
    "I am working on a project that requires to have several modules with the same name" - well there's your problem. Commented Oct 1, 2015 at 9:56
  • jonrsharpe - i am well aware that the situation itself is the problem :) Commented Oct 1, 2015 at 12:16

1 Answer 1

5

One way would be to use the imp module.

import imp
somectrl = imp.load_source("somectrl", "path1/ProjectA/src/ctrl/somectrl.py")
someclass = imp.load_source("someclass", "path2/ProjectA/src/someclass.py")
Sign up to request clarification or add additional context in comments.

Comments

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.