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.
问题:
回答1:
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.
回答2:
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
回答3:
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
回答4:
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:
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.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.
回答6:
You can run pip inside a python script:
import pip
pip.main(['install', 'packagename'])
回答7:
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.