title: Importing a Python module that was not available at interpreter startup
body: <p>I have code which looks like this:</p>
  
  <pre><code>import sys, importlib
  
  try:
      import pip
  except ImportError:
      raise ImportError("Please install pip")
  
  reqs = ["sh", "vcstools"]
  for req in reqs:
      sys.stdout.write("checking for %s..." % req)
      try:
          importlib.import_module(req)
          print("found")
      except ImportError:
          print("missing!")
          print("Installing %s..." % req)
          pip.main(['install', '--user', req])
          #importlib.invalidate_caches() python3 only
          # XXX fails
          importlib.import_module(req)
          new_mods = True
  
      locs = locals()
      locs[req] = sys.modules[req]
  
  print(sh, vcstools)
  </code></pre>
  
  <p>This is designed to download (rough) dependencies at runtime and import them (yes, I know, it does not respect version numbers, and I could have used virtualenv, etc. These are separate issues).</p>
  
  <p>If we run this (with nothing installed in ~/.local), we get the following:</p>
  
  <pre><code>checking for sh...missing!
  Installing sh...
  Downloading/unpacking sh
  ....
  Successfully installed sh
  Cleaning up...
  Traceback (most recent call last):
    File "test.py", line 20, in &lt;module&gt;
      importlib.import_module(req)
    File "/usr/local/lib/python2.7/importlib/__init__.py", line 37, in import_module
      __import__(name)
  ImportError: No module named sh
  </code></pre>
  
  <p>So we see that after installing the <code>sh</code> module, we cannot immediately import it. If we re-run the script, it will succeed, finding the <code>sh</code> installed in the previous run.</p>
  
  <p>What I find odd is that, we do not see the same behavior for <code>vcstools</code>; it installs and imports just fine in the same run. What gives? Is there something special about <code>sh</code>?</p>
  
  <p>Here is the full output from the second run. Notice that we pick up <code>sh</code> from the last run, we then install <code>vcstools</code> <em>and</em> import it without error:</p>
  
  <pre><code>checking for sh...found
  checking for vcstools...missing!
  Installing vcstools...
  Downloading/unpacking vcstools
    Downloading vcstools-0.1.33.tar.gz
    Running setup.py egg_info for package vcstools
  
  Downloading/unpacking pyyaml (from vcstools)
    Downloading PyYAML-3.11.tar.gz (248Kb): 248Kb downloaded
    Running setup.py egg_info for package pyyaml
  
      skipping 'ext/_yaml.c' Cython extension (up-to-date)
  Requirement already satisfied (use --upgrade to upgrade): python-dateutil in /usr/local/lib/python2.7/site-packages (from vcstools)
  Installing collected packages: vcstools, pyyaml
    Running setup.py install for vcstools
  
    Running setup.py install for pyyaml
      checking if libyaml is compilable
      cc -pthread -fno-strict-aliasing -O2 -pipe -DNDEBUG -O2 -pipe -fPIC -fPIC -I/usr/local/include/python2.7 -c build/temp.openbsd-5.5-amd64-2.7/check_libyaml.c -o build/temp.openbsd-5.5-amd64-2.7/check_libyaml.o
      build/temp.openbsd-5.5-amd64-2.7/check_libyaml.c:2:18: error: yaml.h: No such file or directory
      build/temp.openbsd-5.5-amd64-2.7/check_libyaml.c: In function 'main':
      build/temp.openbsd-5.5-amd64-2.7/check_libyaml.c:5: error: 'yaml_parser_t' undeclared (first use in this function)
      build/temp.openbsd-5.5-amd64-2.7/check_libyaml.c:5: error: (Each undeclared identifier is reported only once
      build/temp.openbsd-5.5-amd64-2.7/check_libyaml.c:5: error: for each function it appears in.)
      build/temp.openbsd-5.5-amd64-2.7/check_libyaml.c:5: error: expected ';' before 'parser'
      build/temp.openbsd-5.5-amd64-2.7/check_libyaml.c:6: error: 'yaml_emitter_t' undeclared (first use in this function)
      build/temp.openbsd-5.5-amd64-2.7/check_libyaml.c:6: error: expected ';' before 'emitter'
      build/temp.openbsd-5.5-amd64-2.7/check_libyaml.c:8: error: 'parser' undeclared (first use in this function)
      build/temp.openbsd-5.5-amd64-2.7/check_libyaml.c:11: error: 'emitter' undeclared (first use in this function)
  
      libyaml is not found or a compiler error: forcing --without-libyaml
      (if libyaml is installed correctly, you may need to
       specify the option --include-dirs or uncomment and
       modify the parameter include_dirs in setup.cfg)
  
      skipping 'ext/_yaml.c' Cython extension (up-to-date)
  Successfully installed vcstools pyyaml
  Cleaning up...
  (&lt;module 'sh' (built-in)&gt;, &lt;module 'vcstools' from '/home/edd/.local/lib/python2.7/site-packages/vcstools/__init__.pyc'&gt;)
  </code></pre>
  
  <p>This is Python-2.7 on OpenBSD.</p>
  
  <p>Cheers</p>
  
  <p>EDIT: Just noticed the <code>new_mods</code> line is redundant. I will leave it there so the line numbers in the output are not skewed.</p>