Installing python module within code
Solution 1
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])
Solution 2
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')
Solution 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.
Solution 4
This should work:
import subprocess
def install(name):
subprocess.call(['pip', 'install', name])
Solution 5
i added some exception handling to @Aaron's answer.
import subprocess
import sys
try:
import pandas as pd
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", 'pandas'])
finally:
import pandas as pd
chuwy
Updated on January 25, 2022Comments
-
chuwy over 2 years
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 likepypi.install('requests')
and requests will be installed into my virtualenv. -
chuwy over 11 yearsYes 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 over 11 years@Downvoter: What exactly is wrong with my answer? This answer has all the OP wanted. It doesn't even use a shell.
-
Lukas Graf over 11 yearsThis 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.
-
fiatjaf over 10 yearsHere this exits after installing.
-
GaryBishop over 10 yearsIt 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.
-
jason over 9 years@Rikard Anglerud. Is there an option to upgrade the packages in a batch? something like
'install --upgrade'
? -
Patrick over 9 years@RikAng What to change in your code if i want to install multiple packages
-
nbro almost 9 yearsIs 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 almost 9 years@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'])
-
Myer almost 9 yearsSee also stackoverflow.com/questions/6457794/…, which shows how to handle the case where an install fails.
-
kgadek almost 9 yearsAny idea how to do that on Python 3?
imp.reload(site)
gets meRuntimeError: dictionary changed size during iteration
-
Smit Johnth almost 9 yearsWindows even don't have pip on PATH by default.
-
Jared over 8 yearsThis 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.
-
Natim over 8 yearsDepending on how the script is running you wont call the right pip.
-
Xabs about 8 yearsHow 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 about 8 yearsReplying to myself: to install in a specific virtualenv:
pip install --target=my-virtualenv/lib/python2.7/site-packages
. -
kaycee about 8 yearsI'm getting
ValueError: Unable to configure handler 'console': 'OutStream' object has no attribute 'closed'
when using this function. Anyone know why ? -
Ishan Khare about 8 yearswhere does this install the package, after i did this, i was not able to do
pip uninstall <package_name>
. I can still uninstall it usingpip.main
but just wanted to know where does it install the package? -
tribbloid over 7 yearsDoesn't work and gave me the error: Traceback (most recent call last): File "/home/***/git/pip.py", line 9, in <module> install('arg') File "/home/***/git/pip.py", line 4, in install pip.main(['install', package]) AttributeError: 'module' object has no attribute 'main'
-
running.t over 7 yearsThere 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.
-
Corey Goldberg over 7 yearswhile this is proper advice, it doesn't answer the question asked
-
hoefling over 7 yearsWhile packaging is a topic, there are a lot of other use cases, for example a deployment script written in python.
-
Fallenreaper about 7 yearsWas curious. would this work properly if i do:
pip install requests[security]
? I wasnt sure if it would properly define the globals correctly. -
pitchblack408 almost 7 yearsHow do you do this with pip3?
-
uchuugaka over 6 yearsApparently, this approach is preferred by the pip team at this time.
-
Kelvin Ng about 6 yearsSadly, this works well with virtualenv github.com/explosion/spaCy/commit/…
-
3pitt almost 6 years
from pip._internal import main as pipmain
then you can usepipmain()
just like the deprecatedpip.main()
see stackoverflow.com/questions/43398961/… -
bli almost 6 yearsFor 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 almost 6 yearshow do you use a requirements file and install packages to a different directory?
-
Morse almost 6 yearsits depreceated now.
-
Jordan Mackie almost 5 years"Sadly, this works well with..." @KelvinNg did you mean to say it doesn't work well with virtualenv?
-
Kelvin Ng almost 5 yearsMay not work with virtualenv github.com/explosion/spaCy/commit/…
-
Kelvin Ng almost 5 years@JordanMackie No, I just feel sad call
pip
programatically doesn't work but this one does. -
nbk almost 5 yearsIt's deprecated for a reason & not recommended anymore. see pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program
-
cowlinator over 4 yearsThis fails with
AttributeError: module 'pip' has no attribute '_internal'
. However, it does work if youimport pip._internal
-
Shantanu Bedajna over 4 yearsnice implementation of subprocess and pip, better than most solutions here
-
Basj over 4 yearsThis works for me on Python 3.6.7:
import pip._internal.main ; pip._internal.main.main(['install', 'MODULENAMEHERE'])
. -
wim over 4 yearsDoes not work, update please. pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program
-
wim over 4 yearsOutdated.
pip.main
no longer works. pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program -
wim over 4 yearsCalling
[sys.executable, '-m', 'pip', 'install', name]
is making sure to get the "right" pip here. -
Antti Haapala -- Слава Україні over 4 yearsYou're not checking the retunr value of
subprocess.call
so the code might fail. -
dotbit over 4 yearsthe official python docs explicitly recommend AGAINST doing such a thing. Whyever was this upvoted?
-
jdpipe about 4 yearsOne 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.
-
Borislav Aymaliev about 4 yearsBecause 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.
-
parvij about 4 yearsCalledProcessError: Command '['C:\\ProgramData\\Anaconda3\\pythonw.exe', '-m', 'pip', 'install', 'googleapiclient']' returned non-zero exit status 1.
-
user441669 over 3 yearsI'm trying to use this approach, but my python is embedded/started from another executable, so "sys.executable" doesn't return the right path. Is there an alternative that would work for python that's started by some other process?
-
Coddy about 3 yearswhy
subprocess.check_call
and notsubprocess.call
? -
sh37211 about 3 yearsDoes importing within a function really import into the main namespace, or just the namespace of that
install_and_import
function? -
Glen Thompson almost 3 yearsThis is just a copy of Tanmay Shrivastava's answer
-
Daniel Bandeira over 2 yearsOk, running "import pandas as pd" brings no problem, but... logically isn't it ugly?
-
Shubham Garg over 2 yearsthis is so easy and simple to understand for beginners as compared to all the other answers. Thank you very much.
-
CIsForCookies over 2 yearsWhy doesn't this work if the script that installs and reloads calls another script that does the import?
-
Serge Stroobandt over 2 yearsThe asterisk in front of
*missing
serves to unpack the set represented by the variable namemissing
. See also: Understanding the asterisk(*) of Python -
BND almost 2 yearsstrange thing I need to rerun the script the module was installed from so that it will be recognized
-
Aaron de Windt almost 2 years@BND You could automate this using
os.execv(...)
or it's variants. Something likeos.execv(" ".join((sys.excecutable, *sys.argv)))
might work. -
Aaron de Windt almost 2 years@BND Another solution would be to use
imp.reload(...)
to reload the module if you already imported it before reinstalling.