Run pip in python idle mode

I am wondering how to run pip. Every time I ran pip in the shell in such windows

c:\python27\script>pip install numpy 

But I was wondering if I can run it in python standby mode.

 import pip pip.install("numpy") 

Unfortunately, it does not work.

+5
source share
3 answers

This question, or should be, about how to start pip from a python program. IDLE is not directly affiliated with this version of quesiton.

To expand on JJ sys.argv comment: a command line such as pip install pillow breaks into spaces to become sys.argv . When pip starts as the main module, it calls pip.main(sys.argv[1:]) . If one of them imports a pip, you can call pip.main(arg_line.split()) , where arg_line is part of the command line after pip .

Last September (2015), I experimented using this unintended API from another python program and reported initial results on tracker 23551 issue. Discussion and further results.

The problem with executing several commands in one process is that some command commands cache not only sys.path, which usually remains constant, but also a list of installed packages, which usually changes. Since pip is designed to run one command for each process and then exit, it never updates the cache. When pip.main is used to run several commands in one process, the commands specified after caching can use an obsolete and no more correct cache. For example, the list after install shows how things are before installation .

The second problem for a program that wants to examine the output from pip is that it goes to stdout and stderr. I sent a program that captures these streams in program variables as part of starting pip.

Using a subprocess call for each pip command, as suggested by L_Pav, although less efficiently, solves both problems. The communicate method makes output streams available. See Subprocess doc.

+3
source

Actually, I think you can use subprocess.Popen(apt-get numpy) , don’t know how to do it with PIP.

+2
source

There is currently no official way to do this, you can use pip.main, but the current inactivity session will not see this installed package.

There has been a lot of discussion on how to add a high-level programming API for pip , it seems promising.

+1
source

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


All Articles