Python Dynamic Import Can't Find Packages in Virtualenv
Question: So, I have a directory structure:

    
    
    main.py
    \_ modules/
       \_ a.py
       \_ b.py
    

In main.py, modules are dynamically loaded at runtime, depending on which
modules are specified. (This allows for a hypothetical `c.py` to be added,
`main.py` to be rerun, and the program to detect the addition of `c.py` and
run it.)

The problem is that `b.py` imports a module installed via pip (in a
virtualenv). (I'm going to refer to it as a library to help avoid confusion.)
When `b.py` is run directly (`python b.py`) the library imports just fine.
When the shell is opened and the library is imported by hand, it works.

But, when `main.py` is run and `b.py` is dynamically imported (using
`pkgutil.iter_modules` to detect the modules and then
`importlib.import_module` to import the required ones), the library that
`b.py` imports isn't found - an `ImportError: No module` is thrown.

To recap: a module imports an installed library, and this works when the
module is run directly or when the library in question is imported manually in
the python interpreter, but when the module is dynamically imported, the
library isn't found. What gives?


Answer: Virtualenvs are notorious. They tinker around with paths lots of times and
mess up quite a lot of things.

You need to check the `PATH` variable inside `b.py` in both scenarios. This
will most probably not be the same.

You'd need to set the path to include your `c.py`'s directory. You can check
the PATH in `main.py` and if it is correct there, it would mean that one of
your other imports is changing your `sys.path` ina way to remove that.

