By chuwy

2012-09-08 17:33:02 8 Comments

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.


@Tanmay Shrivastava 2020-07-26 06:09:57

for installing multiple packages, i am using a file with following code:

import sys
import subprocess
import pkg_resources

required = {'numpy','pandas','<etc>'} 
installed = {pkg.key for pkg in pkg_resources.working_set}
missing = required - installed

if missing:
    # implement pip as a subprocess:
    subprocess.check_call([sys.executable, '-m', 'pip', 'install',*missing])

@Sohan Das 2019-09-21 12:58:14

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

import subprocess
import sys

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

@Shantanu Shady 2019-09-23 11:30:41

nice implementation of subprocess and pip, better than most solutions here

@Antti Haapala 2019-12-07 13:42:51

You're not checking the retunr value of so the code might fail.

@Aaron de Windt 2018-05-09 13:47:15

The officially recommended way to install packages from a script is by calling pip's command-line interface via a subprocess. Most other answers presented here are not supported by pip. Furthermore since pip v10, all code has been moved to pip._internal precisely in order to make it clear to users that programmatic use of pip is not allowed.

Use sys.executable to ensure that you will call the same pip associated with the current runtime.

import subprocess
import sys

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

@jdpipe 2020-04-17 01:27:36

One issue with this is that, for novice users on Windows, python and pip are not always on their PATH, and so a .py file that could be double-clicked would be quite convenient, whereas a "pip install xxx" comment can be quite tricky.

@parvij 2020-05-13 18:30:21

CalledProcessError: Command '['C:\\ProgramData\\Anaconda3\\pythonw.exe', '-m', 'pip', 'install', 'googleapiclient']' returned non-zero exit status 1.

@Rikard Anglerud 2013-04-11 13:54:43

You can also use something like:

import pip

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

# Example
if __name__ == '__main__':

@fiatjaf 2013-10-30 20:37:09

Here this exits after installing.

@jason 2014-09-16 06:39:21

@Rikard Anglerud. Is there an option to upgrade the packages in a batch? something like 'install --upgrade'?

@Patrick 2014-10-13 06:25:24

@RikAng What to change in your code if i want to install multiple packages

@nbro 2015-06-03 22:19:40

Is there a way to pass options when installing, for example the version of the package? If yes, could you please add it to your answer just for completeness.

@Kaos 2015-07-20 10:18:39

@nbro you pass options to pip.main() as you would on the command line (but with each option as a separate entry in the list, instead of a single string). And to specify which version of the package you want, do the same as on the command line.. ex: pip.main(['install', 'pip==7.1.0'])

@P. Myer Nore 2015-07-20 11:21:56

See also…, which shows how to handle the case where an install fails.

@Jared 2015-11-28 14:26:51

This is also a great option for installing new python libraries on Windows via the Python console if you don't have a machine with powershell on it.

@Xabs 2016-03-22 16:48:00

How can I have this working for a particular virtualenv? I'm trying to automate generating wheels for a matrix of packages and python versions. What I want is to create a virtualenv, install some dependencies, and create the wheel.

@Xabs 2016-03-22 17:00:12

Replying to myself: to install in a specific virtualenv: pip install --target=my-virtualenv/lib/python2.7/site-packages.

@kaycee 2016-04-11 19:56:22

I'm getting ValueError: Unable to configure handler 'console': 'OutStream' object has no attribute 'closed' when using this function. Anyone know why ?

@tribbloid 2016-09-04 07:52:17

Doesn't work and gave me the error: Traceback (most recent call last): File "/home/***/git/", line 9, in <module> install('arg') File "/home/***/git/", line 4, in install pip.main(['install', package]) AttributeError: 'module' object has no attribute 'main'

@pitchblack408 2017-05-24 22:33:56

How do you do this with pip3?

@3pitt 2018-05-30 15:48:30

from pip._internal import main as pipmain then you can use pipmain() just like the deprecated pip.main() see…

@bli 2018-05-31 08:52:27

For some reason, after compiling and installing a local version of python 2.7.15, the command-line pip was not working (it tried to import stuff from the system-installed python and complained about missing pip distribution). The above solution worked for me, but after upgrading pip (pip.main(["install", "--upgrade", "pip"])), I had to use the modification suggested by @MikePalmice to further install packages.

@red888 2018-06-14 22:54:57

how do you use a requirements file and install packages to a different directory?

@Morse 2018-07-08 01:12:51

its depreceated now.

@Kelvin Ng 2019-07-11 20:34:42

May not work with virtualenv…

@NBK 2019-07-28 16:56:14

It's deprecated for a reason & not recommended anymore. see‌​m

@cowlinator 2019-08-21 19:47:40

This fails with AttributeError: module 'pip' has no attribute '_internal'. However, it does work if you import pip._internal

@Basj 2019-11-12 12:23:44

This works for me on Python 3.6.7: import pip._internal.main ; pip._internal.main.main(['install', 'MODULENAMEHERE']).

@wim 2019-12-05 16:09:28

@dotbit 2019-12-11 07:35:33

the official python docs explicitly recommend AGAINST doing such a thing. Whyever was this upvoted?

@Borislav Aymaliev 2020-04-27 09:30:17

Because in specific cases (e.g. when your cannot update your PATH on a corporate network) this is the only way to install a module. Generally, it is hard to take the docs seriously when it recommends "against something" without providing an alternative solution.

@rominf 2014-07-16 06:45:37

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
    except ImportError:
        import pip
        pip.main(['install', package])
        globals()[package] = importlib.import_module(package)


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.

@kgadek 2015-08-03 16:35:22

Any idea how to do that on Python 3? imp.reload(site) gets me RuntimeError: dictionary changed size during iteration

@Ishan Khare 2016-04-26 12:20:02

where does this install the package, after i did this, i was not able to do pip uninstall <package_name>. I can still uninstall it using pip.main but just wanted to know where does it install the package?

@Fallenreaper 2017-05-08 16:47:12

Was curious. would this work properly if i do: pip install requests[security] ? I wasnt sure if it would properly define the globals correctly.

@wim 2019-12-05 16:10:19

@quantum 2012-09-08 17:52:29

This should work:

import subprocess

def install(name):['pip', 'install', name])

@chuwy 2012-09-08 18:03:08

Yes it's definitely should work. But I thought there is more elegant way;) I'll be waiting a little bit, may be there is.

@quantum 2012-09-08 18:09:11

@Downvoter: What exactly is wrong with my answer? This answer has all the OP wanted. It doesn't even use a shell.

@GaryBishop 2014-01-17 11:57:34

It depends on the right version of pip being first on the path. If the user is running an alternate python installation, pip will install into the first one instead of the current one. The import approach above will install in the right place. I upvoted anyway to counter the down vote.

@Smit Johnth 2015-08-07 03:46:18

Windows even don't have pip on PATH by default.

@Natim 2016-01-13 13:13:13

Depending on how the script is running you wont call the right pip.

@running.t 2017-01-10 14:38:37

There is a lot of situations when this approach will not work. e.g. when you use several versions of python on one machine and you interpret current script with not default python.

@uchuugaka 2017-08-27 16:58:52

Apparently, this approach is preferred by the pip team at this time.

@Kelvin Ng 2018-04-16 10:46:45

Sadly, this works well with virtualenv…

@Jordan Mackie 2019-07-10 15:58:57

"Sadly, this works well with..." @KelvinNg did you mean to say it doesn't work well with virtualenv?

@Kelvin Ng 2019-07-11 20:36:58

@JordanMackie No, I just feel sad call pip programatically doesn't work but this one does.

@wim 2019-12-05 16:11:56

Calling [sys.executable, '-m', 'pip', 'install', name] is making sure to get the "right" pip here.

@Andreas Jung 2012-09-08 17:38:59

You define the dependent module inside the 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).

@Lukas Graf 2012-09-08 20:48:23

This is the correct answer and the only sensible way to manage a Python projects' dependencies. It will work with virtualenv, Fabric, buildout, you name it. The method described by @xiaomao, even though answering exactly what the OP asked, is pure madness.

@Corey Goldberg 2017-01-20 16:29:34

while this is proper advice, it doesn't answer the question asked

@hoefling 2017-02-02 18:58:15

While packaging is a topic, there are a lot of other use cases, for example a deployment script written in python.

Related Questions

Sponsored Content

21 Answered Questions

[SOLVED] How do I install pip on macOS or OS X?

26 Answered Questions

[SOLVED] Does Python have a ternary conditional operator?

55 Answered Questions

[SOLVED] How to upgrade all Python packages with pip?

  • 2010-04-27 09:23:25
  • thedjpetersen
  • 1251024 View
  • 2104 Score
  • 55 Answer
  • Tags:   python pip

38 Answered Questions

[SOLVED] How to install pip on Windows?

22 Answered Questions

[SOLVED] What are metaclasses in Python?

61 Answered Questions

[SOLVED] Calling an external command from Python

10 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

6 Answered Questions

[SOLVED] How to write a Python module/package?

10 Answered Questions

[SOLVED] Installing Python packages from local file system folder to virtualenv with pip

  • 2013-02-22 19:21:34
  • chadgh
  • 428236 View
  • 351 Score
  • 10 Answer
  • Tags:   python pip

11 Answered Questions

[SOLVED] Installing specific package versions with pip

Sponsored Content