32

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.

2

6 Answers 6

35

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
Sign up to request clarification or add additional context in comments.

4 Comments

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.
This works on AWS Glue python shell service.
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.
8

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

2 Comments

depreceated now
That is inverse to appreciated . It was not appreciated by some body and they killed it
5

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.

1 Comment

You can use ensurepip module which is available on all newer python versions: import ensurepip; ensurepip.bootstrap()
4

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.

5 Comments

I want a to do somethind like: import pip; pip.install('package_name')
what if using pip inside a virtualenv? it still might be possible without root permissions isn't it ??
outdated answer. see the other answers.
Using pip via subprocess is the recommended approach pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program
they can be installed in a directory with the '-t --target' flag
3

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

Comments

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

1 Comment

Outdated by PEP 518 / buid system requires.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.