Installing python module within code

爷,独闯天下 提交于 2019-11-25 23:59:26

问题


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.


回答1:


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')



回答2:


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.




回答3:


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.




回答4:


This should work:

import subprocess

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



回答5:


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).




回答6:


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)



回答7:


I think it can be more straightforward by using os.system

from os import system

package = # the name of the package
system('pip install %s' % package)



回答8:


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.




回答9:


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()



回答10:


i added some exception handling to @Aaron's answer.

import subprocess
import sys

try:
    import pandas as pd
except ImportError:
    subprocess.call([sys.executable, "-m", "pip", "install", 'pandas'])
finally:
    import pandas as pd



回答11:


You can install using sys module a well

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



回答12:


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.




回答13:


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()



回答14:


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.




回答15:


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



回答16:


I hope this will be useful, you are able to get installed packages, install or upgrade them.

from subprocess import call, check_output
from sys import executable

def packages():
    return dict(package.split('==') for package in check_output([executable, '-m', 'pip', 'freeze']).decode().split())

def install(package):
    call([executable, '-m', 'pip', 'install', package])

def upgrade(package):
    call([executable, '-m', 'pip', 'install', package, '--upgrade'])

if __name__ == "__main__":
    package = 'numpy'
    version = '1.17.3'

    packages = packages()

    if package not in packages.keys():
        install(package)
    elif packages[package] < version:
        upgrade(package)

    print(packages)



回答17:


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.



来源:https://stackoverflow.com/questions/12332975/installing-python-module-within-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!