title: Import vendored dependencies in Python package without modifying sys.path or 3rd party packages
body: <h3>Summary</h3>
  
  <p>I am working on a series of add-ons for <a href="http://ankisrs.net/" rel="noreferrer">Anki</a>, an open-source flashcard program. Anki add-ons are shipped as Python packages, with the basic folder structure looking as follows:</p>
  
  <pre><code>anki_addons/
      addon_name_1/
          __init__.py
      addon_name_2/
          __init__.py
  </code></pre>
  
  <p><code>anki_addons</code> is appended to <code>sys.path</code> by the base app, which then imports each add_on with <code>import &lt;addon_name&gt;</code>.</p>
  
  <p>The problem I have been trying to solve is to find a reliable way to <strong>ship packages and their dependencies with my add-ons while not polluting global state or falling back to manual edits of the vendored packages</strong>.</p>
  
  <h3>Specifics</h3>
  
  <p>Specifically, given an add-on structure like this...</p>
  
  <pre><code>addon_name_1/
      __init__.py
      _vendor/
          __init__.py
          library1
          library2
          dependency_of_library2
          ...
  </code></pre>
  
  <p>...I would like to be able to import any arbitrary package that is included in the <code>_vendor</code> directory, e.g.:</p>
  
  <pre><code>from ._vendor import library1
  </code></pre>
  
  <p>The main difficulty with relative imports like this is that they do not work for packages that also depend on other packages imported through absolute references (e.g. <code>import dependency_of_library2</code> in the source code of <code>library2</code>)</p>
  
  <h3>Solution attempts</h3>
  
  <p>So far I have explored the following options:</p>
  
  <ol>
  <li>Manually updating the third-party packages, so that their import statements point to the fully qualified module path within my python package (e.g. <code>import addon_name_1._vendor.dependency_of_library2</code>). But this is tedious work that is not scalable to larger dependency trees and not portable to other packages.</li>
  <li>Adding <code>_vendor</code> to <code>sys.path</code> via <code>sys.path.insert(1, &lt;path_to_vendor_dir&gt;)</code> in my package init file. This works, but it introduces a global change to the module look-up path which will affect other add-ons and even the base app itself. It just seems like a hack that could result in a pandora's box of issues later down the line (e.g. conflicts between different versions of the same package, etc.).</li>
  <li><a href="https://stackoverflow.com/questions/17211078/how-to-temporarily-modify-sys-path-in-python">Temporarily modifying sys.path for my imports</a>; but this fails to work for third-party modules with method-level imports.</li>
  <li>Writing a <a href="https://www.python.org/dev/peps/pep-0302/" rel="noreferrer">PEP302</a>-style custom importer based off an example I found in <a href="https://github.com/pypa/setuptools/blob/master/setuptools/extern/__init__.py" rel="noreferrer">setuptools</a>, but I just couldn't make head nor tail of that.</li>
  </ol>
  
  <hr>
  
  <p>I've been stuck on this for quite a few hours now and I'm beginning to think that I'm either completely missing an easy way to do this, or that there is something fundamentally wrong with my entire approach.</p>
  
  <p><strong>Is there no way I can ship a dependency tree of third-party packages with my code, without having to resort to <code>sys.path</code> hacks or modifying the packages in question?</strong></p>
  
  <hr>
  
  <p>Edit:</p>
  
  <p>Just to clarify: I don't have any control over how add-ons are imported from the anki_addons folder. anki_addons is just the directory provided by the base app where all add-ons are installed into. It is added to the sys path, so the add-on packages therein pretty much just behave like any other python package located in Python's module look-up paths.</p>