17

how, inside a python script can I install packages using pip? I don't use the os.system, I want to import pip and use it.

Bengineer
  • 6,108
  • 6
  • 23
  • 27

6 Answers6

25

pip.main() no longer works in pip version 10 and above. You need to use:

from pip._internal import main as pipmain

pipmain(['install', 'package-name'])

For backwards compatibility you can use:

try:
    from pip import main as pipmain
except ImportError:
    from pip._internal import main as pipmain
Damian Russak
  • 360
  • 3
  • 9
  • 8
    Important to know: this will *break* if you call pip several times from your script, or if you first call it like this from your script and then call it in any other way (personally I used `flit`). This is because pip will set `os.environ['PIP_REQ_TRACKER']` to a temporary directory name which gets deleted afterwards, but environment is not cleared. So it is better to call pip in a subprocess using `os.system` or any other method available. – MarSoft Mar 28 '19 at 16:39
  • This works on AWS Glue python shell service. – Gianmar Jul 26 '19 at 18:28
  • 5
    This is explicitly unsupported https://pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program – Samizdis Aug 07 '19 at 10:13
  • 1
    The precise reason for the name "_internal" is to be a clear indicator that you should NOT be using pip from within a script like this. see @Samizdis response above for a proper guide. – ch4rl1e97 Dec 08 '19 at 16:42
9

I think those answers are outdated. In fact you can do:

import pip
failed = pip.main(["install", nameOfPackage])

and insert any additional args in the list that you pass to main(). It returns 0 (failed) or 1 (success)

Jon

Jon
  • 1,057
  • 6
  • 8
7

It's not a good idea to install packages inside the python script because it requires root rights. You should ship additional modules alongside with the script you created or check if the module is installed:

try:
   import ModuleName
except ImportError:
   print 'Error, Module ModuleName is required'

If you insist in installing the package using pip inside your script you'll have to look into call from the subprocess module ("os.system()" is deprecated).

There is no pip module but you could easily create one using the method above.

tehmisvh
  • 586
  • 3
  • 9
1

If you are behind a proxy, you can install a module within code as follow...

import pip
pip.main(['install', '--proxy=user:password@proxy:port', 'packagename'])
jesusperalta
  • 601
  • 1
  • 8
  • 18
1

I used the os.system to emulate the terminal installing a pip module, (I know os.system is deprecated, but it still works and it is also the easiest way to do it), E.G I am making a Game Engine which has multiple python scripts that all use Pygame, in the startup file I use this code to install pygame onto the user's system if they don't have it:

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

Unfortunately, I don't know how to install pip if they don't have it so this script is dependent on pip.

X0FloH
  • 19
  • 1
  • You can use `ensurepip` module which is available on all newer python versions: `import ensurepip; ensurepip.bootstrap()` – MarSoft Mar 28 '19 at 16:16
-2

This is a comment to this post that didn't fit in the space allotted to comments.

Note that the use case of installing a package can arise inside setup.py itself. For example, generating ply parser tables and storing them to disk. These tables must be generated before setuptools.setup runs, because they have to be copied to site_packages, together with the package that is being installed.

There does exist the setup_requires option of setuptools.setup, however that does not install the packages.

So a dependency that is required both for the installation process and for the installed package will not be installed this way.

Placing such a dependency inside install_requires does not always work as expected. Even if it worked, one would have to pass some function to setuptools.setup, to be run between installation of dependencies in setup_requires and installation of the package itself. This approach is nested, and thus against PEP 20.

So the two flat approaches that remain, are:

  1. run setup.py twice, either automatically (preferred), or manually (by notifying the user that the tables failed to build prior to setuptools.setup.

  2. first call pip (or some other equivalent solution), in order to install the required dependencies. Then proceed with building the tables (or whatever pre-installation task is necessary), and call setuptools.setup last.

Personally, I prefer No.2, because No.1 can be confusing to a user observing the console output during installation, unless they already know the intent of calling setuptools.setup twice.

Besides, whatever rights are needed for installation (e.g., root, if so desired), are certainly present when setup.py is run (and exactly then). So setup.py could be considered as the "canonical" use case for this type of action.

Ioannis Filippidis
  • 8,272
  • 8
  • 66
  • 96