2

I just started learning Python and a bit confused about how packages are distributed and installed. I am aware of helper scripts easy_install and pip which can be used to install the dependent modules,howerver I am not clear how to do with programatically,can someone help me on this?

How to install dependent modules automatically when running python applications? I have a dependency on subprocess32 and other modules,I want to automatically install them if they are not present....

  File "script.py", line 6, in <module>
    import subprocess32 as subprocess
ImportError: No module named subprocess32

I have looked at some posts online below but not clear...really appreciate guidance here

locallyoptimal.com/blog/2014/03/14/executable-python-scripts-via-entry-points/ Python packages installation in Windows

Community
  • 1
  • 1
  • You have to write your own configuration file for `distutils` or `setuptools`. In this file you have to specify the dependencies. – Bakuriu Jul 05 '16 at 22:12
  • @Bakuriu - Wouldnt JeffL solution below work?why to write own configuration file for distutils or setuptools ,please share details –  Jul 05 '16 at 23:29
  • I misunderstood your question. In any case I believe what you have in mind is a really bad practice. Either you want to provide a standalone program, or you want to provide an installation script. Installing things on the go isn't a good idea. For example you now need admin privileges to run your program. In any case I believe Jeff L. solution still requires the user to confirm the installation and if the user doesn't want to your program will fail anyway. – Bakuriu Jul 06 '16 at 07:34
  • @Bakuriu - can you provide more details on how to create an installation script that can install `pip` and any required dependent modules that can be installed using `pip` –  Jul 06 '16 at 17:33

1 Answers1

1

Easiest way to handle module installs within a program is with pip:

import pip
pip.main(["install", "numpy"])

For example will install numpy and its dependencies. You can also specify versions using numpy=xxx, or upgrade by passing "-upgrade" between those two above.

Jeff
  • 2,158
  • 1
  • 16
  • 29
  • how do you handle `ImportError: No module named pip` install errors –  Jul 06 '16 at 00:08
  • Pip is almost always installed already, even in a download of plain Python, so that's odd. But here's the link with instructions to install Pip if you are missing it: https://pip.pypa.io/en/stable/installing/ – Jeff Jul 06 '16 at 00:10
  • is there a way to check if the required setup tools are already present on the machine like `pip`,`easy_install` –  Jul 06 '16 at 00:21
  • Sure, you can just try running them from a command line or bash, e.g. `easy_install numpy`. You can also try importing pip like I showed above. If both of those fail then you either don't have things installed (and should probably look at a free distribution like Anaconda or Canopy), or there's a problem with your system and python paths. – Jeff Jul 06 '16 at 00:26
  • I understand.I meant is there a way to install `pip` programatically if `pip` is not present,an extension of the script you showed.. –  Jul 06 '16 at 00:31
  • Ah I see. Not that I am aware of, but Pip is standard with all builds of Python beginning with version 2.7.9, so it should always be there unless for some reason you need an earlier version of Python built directly from Python.org. – Jeff Jul 06 '16 at 00:36
  • ya,I am using python `2.7.3` ,is it possible to pre-install `pip`and then package the python app and `pip` together as an installer...what other options are there? –  Jul 06 '16 at 00:41