use “pip install/uninstall” inside a python script

2019-03-30 02:20发布

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.

标签: python pip
7条回答
戒情不戒烟
2楼-- · 2019-03-30 02:57

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
查看更多
一纸荒年 Trace。
3楼-- · 2019-03-30 03:00

You can run pip inside a python script:

import pip
pip.main(['install', 'packagename'])
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-03-30 03:02

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'])
查看更多
爷、活的狠高调
5楼-- · 2019-03-30 03:04

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

查看更多
Juvenile、少年°
6楼-- · 2019-03-30 03:12

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.

查看更多
女痞
7楼-- · 2019-03-30 03:13

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

查看更多
登录 后发表回答