Use "pip install / uninstall" inside python script

how, inside a python script, is it possible to install packages using pip? I do not use os.system, I want to import pip and use it.

+8
source share
7 answers

It is not recommended to install packages inside the python script, as this requires root privileges. You should send additional modules along with the created script or check if the module is installed:

try: import ModuleName except ImportError: print 'Error, Module ModuleName is required' 

If you insist on installing the package using pip inside your script, you will need to examine the call from the subprocess module (" os.system() " is deprecated).

There is no pip module, but you can easily create it using the method above.

+6
source

pip.main () no longer works in version 10 and above. You need to use:

 from pip._internal import main as pipmain pipmain(['install', 'package-name']) 

For backward compatibility, you can use:

 try: from pip import main as pipmain except ImportError: from pip._internal import main as pipmain 
+13
source

I think these answers are out of date. In fact you can do:

 import pip failed = pip.main(["install", nameOfPackage]) 

and paste any additional arguments into the list you pass to main (). It returns 0 (failed) or 1 (success)

John

+8
source

If you are behind a proxy server , you can install the module in code by following ...

 import pip pip.main(['install', '--proxy=user: password@proxy :port', 'packagename']) 
+3
source

This is a comment on this post that does not fit into the space allotted for comments.

Note that a package use case may arise inside setup.py . For example, generating ply parser ply and saving them to disk. These tables must be generated before running setuptools.setup , as they must be copied to site_packages along with the installed package.

There is the setup_requires option setuptools.setup , however this does not install packages.

Thus, the dependency that is required for both the installation process and the installed package will not be installed in this way.

Placing such a dependency inside install_requires does not always work as expected . Even if this worked, you would need to pass some setuptools.setup function, which should be performed between installing dependencies in setup_requires and installing the package itself. This approach is nested and therefore against PEP 20 .

So, the remaining two flat approaches:

  • run setup.py twice, either automatically (preferably) or manually (by notifying the user that the tables could not be created before setuptools.setup .

  • first call pip (or other equivalent solution) to install the necessary dependencies. Then proceed to create the tables (or whatever the task before installation is necessary) and call setuptools.setup last.

Personally, I prefer No.2 because No.2 can mislead the user watching the console exit during installation if they no longer know the intention to call setuptools.setup twice.

In addition, any rights necessary for installation (for example, root, if necessary) are always present when running setup.py (and just then). Thus, setup.py can be seen as a β€œcanonical” use case for this type of action.

+1
source

I used os.system to emulate the terminal installing the pip module (I know that os.system is deprecated, but it still works, and this is also the easiest way to do this) EG I am creating a Game Engine that has several python scripts, which everyone uses Pygame in the boot file, I use this code to install pygame in the user system if they don't have it:

 import os os.system('pip install pygame') 

Unfortunately, I do not know how to install pip if they do not have it, so this script depends on pip.

+1
source

You can run pip inside the python script:

 import pip pip.main(['install', 'packagename']) 
0
source

Source: https://habr.com/ru/post/1440331/


All Articles