Installing python module within code

I need to install a package from PyPi straight within my script. Maybe there's some module or distutils (distribute, pip etc.) feature which allows me to just execute something like pypi.install('requests') and requests will be installed into my virtualenv.

Answers


You can also use something like:

import pip

def install(package):
    if hasattr(pip, 'main'):
        pip.main(['install', package])
    else:
        pip._internal.main(['install', package])

# Example
if __name__ == '__main__':
    install('argh')

Rickard's solution is not supported by the pip development team and will crash in some cases (e.g. multi threaded). Furthermore since pip 10 the code has been moved to pip._internal in order to make it clear that this is not supported, This solution will thus fail with an import error on "pip>=10.0.0".

Quantum's solution is the prefered way of solving the problem, however his implementation does not guarantee it will call the correct pip.

This solution guarantees it calls the pip of the python interpreter running the code.

import subprocess
import sys

def install(package):
    subprocess.call([sys.executable, "-m", "pip", "install", package])

It does the same as Quantum's solution, however instead of running pip directly, it runs the same python executable running the code and tells it to execute the pip module it has installed and passes the rest of the arguments to it.


If you want to use pip to install required package and import it after installation, you can use this code:

def install_and_import(package):
    import importlib
    try:
        importlib.import_module(package)
    except ImportError:
        import pip
        pip.main(['install', package])
    finally:
        globals()[package] = importlib.import_module(package)


install_and_import('transliterate')

If you installed a package as a user you can encounter the problem that you cannot just import the package. See How to refresh sys.path? for additional information.


This should work:

import subprocess

def install(name):
    subprocess.call(['pip', 'install', name])

You define the dependent module inside the setup.py of your own package with the "install_requires" option.

If your package needs to have some console script generated then you can use the "console_scripts" entry point in order to generate a wrapper script that will be placed within the 'bin' folder (e.g. of your virtualenv environment).


I hope this question is still valid. I did the above something like this:

    import sys
    import os
    import site 

    try:
       import pip
    except ImportError:
       print "installing pip"
       cmd = "sudo easy_install pip"
       os.system(cmd)
       reload(site)

    try: 
       import requests
    except ImportError:
       print "no lib requests"
       import pip
       cmd = "sudo pip install requests"
       print "Requests package is missing\nPlease enter root password to install required package"
       os.system(cmd)
       reload(site)

the second try block can be written in the else part of the first try block as well, however in my problem statement I has to write two seperate blocks.


This is how i install all packages for my projects.

Just add all names to the list and execute one before running the main code.

Combining this with the use of "venv" is great!

"""call this module to setup your python packages via pip"""

from pip._internal import main as pip

pip_install_argument = "install"

# packages to install
packages_to_install = [
        "numpy",        # math magic 1
        "scipy",        # math magic 2
        ]

def install(packages):
    """installes given packages via pip

    Args:
        package names as list

    Returns:
        None

    """
    global pip_install_argument
    for package in packages:
        pip([pip_install_argument, package])

if __name__ == '__main__':
    install(packages_to_install)

I assume that we have a pip on our machine, and will try to catch the a specific dependency which missing.. try this method and let me know what you think.

import os
try: 
    import something
    .....
    .....
except ImportError as e:
    pip = lambda : os.system('pip install' + str(e)[15:])
    pip()

Also, please consider working with pip module itself if using lambda functions to apply "self-aware" importer mechanism. Simply following this code snippet :)

try:
    import somthing
    ..........
    ..........
except ImportError as e:
    # importing pip module to handle pip functionalites
    import pip
    # create a lambda that import using pip by module_error_name=e[15:]
    installer = lambda : pip.main(['install', str(e)[15:])
    # create a lambda that import a module by linear_lambda_call
    importer = lambda x : __import__(str(x)).import_module()
    # install the module
    installer()
    # import the module
    importer()

For me this works (python 2.x), assuming all requirements are defined inside requirements.txt:

def pip_auto_install():
    """
    Automatically installs all requirements if pip is installed.
    """
    try:
        from pip._internal import main as pip_main
        pip_main(['install', '-r', 'requirements.txt'])
    except ImportError:
        print("Failed to import pip. Please ensure that pip is installed.")
        print("For further instructions see "
              "https://pip.pypa.io/en/stable/installing/")
        sys.exit(-1)
    except Exception as err:
        print("Failed to install pip requirements: " + err.message)
        sys.exit(-1)


pip_auto_install()

You can install using sys module a well

import sys
!{sys.executable} -m pip install <package> --user

I use the os.system to emulate an os terminal importing a module because then, you can use the module in all the other scripts.

For example, I am creating a Game Engine that runs on separate scripts added together, most of these scripts use Pygame and if the user doesn't have pygame, the startup file will add it using this code:

import os
os.system('pip install pygame')

Unfortunately, I have no idea on how to get pip onto the user's machine so this is dependent on the user having pip.


Since importable pip-module is not always working, there is more robust way:

def PackageInstall(error):
    '''
    Finds out which package is missing and downloads it automatically after five seconds.

    Use example:

    try:
        import numpy as np
        import matplotlib.pyplot as plot

    except ImportError as error:
        PackageInstall(error)

    '''
    import time, subprocess, os, sys
    lib = str(error)[15:].replace('\'', '')
    print('>>>',str(error))
    print('>>> Download will start after five seconds')
    time.sleep(5)
    subprocess.call("pip install " + lib)

    print('>>> Restarting')
    os.startfile(__file__)
    sys.exit()

This works at Windows.


1st

> Download
> https://github.com/GeoNode/geonode-win-installer/blob/master/python_deps/pip-9.0.1-py2.py3-none-any.whl

2nd

extract .whl file and;  use winRar
copy extracted file and paste sitepackage [or python module container]

files are.. pip and pip-19.0.3.dist-info

3rd

import pip
pip.main(['install','scpy'])

scpy or any package you want to install

you could always download

import os

then right all the terminal commands to download it from there.

like

while True:

code = input("")

os.system("code")

whatever it is i'm not sure but if you don't even know how to do it in terminal how are you gonna do it in python.


Need Your Help

CHOLMOD supernodal factorization fails when sparse matrix becomes too dense

c matrix linear-algebra sparse-matrix suitesparse

I am using CHOLMOD in SuiteSparse to factor an N by N large band-diagonal matrix that is relatively sparse, ie, it contains only a few diagonals that are non-zero. The sparsity of the matrix is set...