Search code examples
pythoninstallationpip

Cannot import dynamically installed python module


I am dynamically installing code during the runtime of a script by directly importing the pip module and installing a requirements.txt:

import pip
import site
import sys

# since I'm installing with --user, packages should be installed here,
# so make sure it's on the path
sys.path.insert(0, site.USER_SITE)

pip.main(["install", "--user", "package1"])
import package1

The package installs without error, but import package1 fails. If I quit the script and run it again without doing the install (since it's already been installed), it works just fine.

I've also double checked that the site.USER_SITE is where package1 was installed. Everything is on sys.path, but I still can't import it.


Solution

  • Well, this was a fast turnaround. I've been trying to figure this one out for a few days and finally came up with my answer a few minutes after asking on here.

    If a path that does not yet exist is added to sys.path, it doesn't seem like it will ever be checked again when importing modules, even if it exists at a later point (or at least in python 2.7).

    In my testing, site.USER_SITE didn't exist when I added it to sys.path. If I first made sure that that directory exists, then everything works how you would think it should:

    import os
    import pip
    import site
    import sys
    
    # this makes it work
    if not os.path.exists(site.USER_SITE):
        os.makedirs(site.USER_SITE)
    
    # since I'm installing with --user, packages should be installed here,
    # so make sure it's on the path
    sys.path.insert(0, site.USER_SITE)
    
    pip.main(["install", "--user", "package1"])
    import package1