Is it possible to use two Python packages with the same name?

前端 未结 4 1073
迷失自我
迷失自我 2020-12-11 02:38

I have a question about imports. The question might seem a bit contrived, but its purpose is to explore the limitations of using absolute imports for all imports in a packa

4条回答
  •  失恋的感觉
    2020-12-11 03:09

    Actually, you should use namespaces (packages) to separate properly what modules you want to end up using. In your above code.

    /pkg1
     mod1 - can just import mod2
     mod2.py
     __init__.py
    
    /pkg2
     mod1 - can just import mod2
     mod2.py
     __init__.py
    

    And at rest of the places you should do import pkg1.mod1 or import pkg2.mod1 as desirable.

提交回复
热议问题