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.
-
Related: [Installing python module within code](http://stackoverflow.com/q/12332975/95735) – Piotr Dobrogost Mar 05 '13 at 23:16
-
1Related:[How can I install Python modules programmatically / through a Python script?](http://stackoverflow.com/q/12966147/95735) – Piotr Dobrogost Mar 05 '13 at 23:19
6 Answers
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

- 438
- 5
- 9
-
9Important 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
-
-
8This is explicitly unsupported https://pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program – Samizdis Aug 07 '19 at 10:13
-
5The 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
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

- 1,198
- 7
- 8
-
7
-
1That is inverse to appreciated . It was not appreciated by some body and they killed it – abc Feb 07 '20 at 08:14
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.

- 584
- 3
- 9
-
3I want a to do somethind like: import pip; pip.install('package_name') – Bengineer Oct 17 '12 at 19:37
-
12what if using pip inside a virtualenv? it still might be possible without root permissions isn't it ?? – Javier Novoa C. Oct 24 '13 at 01:18
-
3
-
2Using pip via `subprocess` is the recommended approach https://pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program – Samizdis Aug 07 '19 at 10:13
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.

- 59
- 1
- 1
-
1You can use `ensurepip` module which is available on all newer python versions: `import ensurepip; ensurepip.bootstrap()` – MarSoft Mar 28 '19 at 16:16
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'])

- 661
- 1
- 8
- 18
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:
run
setup.py
twice, either automatically (preferred), or manually (by notifying the user that the tables failed to build prior tosetuptools.setup
.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 callsetuptools.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.

- 10,524
- 11
- 77
- 109