Should I use pip.main () or subprocess.call () to call command commands?

I am writing a program that should install dependencies using pip. What is the right way to do this and why?

Ideally, this should be agnostic for the platform, but the program will run on a Linux machine.

Method 1: pip.main()

import pip
args = ['param1', 'param2']
version = 0.1
package = ['some_package=={}'.format(version)]
pip.main(['install'] + args + package) 

Method 2: subprocess.call()

import subprocess
import sys
version = 0.1
package = 'some_package'

subprocess.call([sys.executable, '-m', 'pip', 'install', '{}=={}'.format(package, version)])
+4
source share
3 answers

Usually

- main(), . , sys.exit() os.exec*(). , umask . , , .

(, , "", , main() , .)

pip.main() . .

+7

. pip . subprocess, .

+1

Looking at PEP 338 , while pip python source seems to be python -m pip [args]equivalent import sys, pip; sys.exit(pip.main(*args)). Therefore, any option should work without problems.

I would prefer to call directly main(), because it is on one level of difficulty.

However, pip.main()it is not a public interface, so it may change in future releases. Thus, one could argue that a subprocess call is being used.

+1
source

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


All Articles