title: How to deal with multiple projects that use absolute imports and have the same folder and filenames in python?
body: <p>How can I deal with importing multiple projects that use absolute imports and have the same folder and filenames?</p>
  <p>I'm working on ML code where many authors have written code that is not meant to be a pip package, and the folders and filenames have high overlap and imports are absolute assuming the root of their code is the root of PYTHONPATH. A setup is this:</p>
  <pre><code>myproject
    myversion
      mymodel.py
    third_party
      version1
        model.py
        util.py
      version2
        model.py
        util.py
  </code></pre>
  <p>With contents like:</p>
  <pre><code># myproject/third_party/version1/model.py
  import util
  
  def process(x):
    return 2*util.preprocess(x)
  </code></pre>
  <pre><code># myproject/third_party/version2/model.py
  import util
  
  def process(x):
    return 3*util.preprocess(x)
  </code></pre>
  <pre><code># myproject/third_party/version1/util.py
  
  def preprocess(x):
    return 1
  </code></pre>
  <pre><code># myproject/third_party/version2/util.py
  
  def preprocess(x):
    return 0
  </code></pre>
  <p>This is what I want to do for example, running <code>PYTHONPATH=/path/to/myproject myversion/mymodel.py</code></p>
  <pre><code># myproject/myversion/mymodel.py
  
  from third_party.version1.model import process as process1
  from third_party.version2.model import process as process2
  
  def myprocess(x):
    return x
  
  def compare(x):
    return process1(x), process2(x), myprocess(x)
  </code></pre>
  <p>But I'm trapped because each version1 and version2 needs a different path to be able to find their own util!</p>
  <p>What is the real solution here? I know if there were relative imports used in version1 and version2 files (not authored by me) it would be easy. I also know if I only used one of the two, it would be easy to just give <code>PYTHONPATH=/path/to/myproject:/path/to/myproject/third_party/version1</code> and everything would be fine. But I would like to use both. In an ideal world I wouldn't change the code inside version1 and version2 very much or at all.</p>
  <p>What I wish I could do is fully encapsulate version1 and version2 code and when run they magically know where to look for their own files. I tried using <code>setuptools</code> but I still have path problems. I suspect bazel can be used for this somehow?</p>
  <p>p.s. Using python 3</p>
  <p>p.p.s. I uploaded a <a href="https://github.com/ilyakava/bazel_demo" rel="nofollow noreferrer">demo on gh here</a></p>