Hyun-geun Kim
Hyun-geun Kim

Reputation: 1069

Run pip in python idle

I am curious about running pip. Everytime I ran pip in command shell in windows like that

c:\python27\script>pip install numpy

But, I wondered if I can run it in python idle.

import pip
pip.install("numpy")

Unfortunately, it is not working.

Upvotes: 10

Views: 66753

Answers (5)

rtrrtr
rtrrtr

Reputation: 692

Still cannot comment so I added another answer. Pip has had several entrypoints in the past. And it's not recommended to call pip directly or in-process (if you still want to do it, "runpy" is kind of recommended):

import sys
import runpy

sys.argv=["pip", "install", "packagename"]
runpy.run_module("pip", run_name="__main__")

But this should also work:

try:
    from pip._internal import main as _pip_main
except ImportError:
    from pip import main as _pip_main
_pip_main(["install", "packagename"])

Upvotes: 8

Grifin
Grifin

Reputation: 39

If your on a Mac you should be able to do it like this:

  1. Go to your IDLE.
  2. Run help('modules').
  3. Find the HTML module.
  4. Run help('HTML')
  5. There should pop up a file map, for example this/file/map/example/.
  6. Go to the finder and do command+shift+g and paste the file map there. Please delete the last file, because then your gonna go to the modules files.
  7. There are all the modules. If you want to add modules, download the files of the module and put them there.

I hope this helps you.

Upvotes: 2

Terry Jan Reedy
Terry Jan Reedy

Reputation: 19219

This question is, or should be, about how to run pip from a python program. IDLE is not directly relevant to this version of the quesiton.

To expand on J. J. Hakala's comment: a command-line such as pip install pillow is split on spaces to become sys.argv. When pip is run as a main module, it calls pip.main(sys.argv[1:]). If one imports pip, one may call pip.main(arg_line.split()), where arg_line is the part of the command line after pip.

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

The problem with executing multiple commands in one process is that some pip commands cache not only sys.path, which normally stays constant, but also the list of installed packages, which normally changes. Since pip is designed to run one command per process, and then exit, it never updates the cache. When pip.main is used to run multiple commands in one process, commands given after the caching may use a stale and no-longer-correct cache. For example, list after install shows how things were before the install.

A second problem for a program that wants to examine the output from pip is that it goes to stdout and stderr. I posted a program that captures these streams into program variables as part of running pip.

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

Upvotes: 6

L_Pav
L_Pav

Reputation: 291

Actually, I think, you can use subprocess.Popen(apt-get numpy), not sure how to do it with PIP though.

Upvotes: 4

fn.
fn.

Reputation: 2956

At moment there are no official way to do it, you could use pip.main but you current idle session will not 'see' this installed package.

There been a lot a discussion over how to add a "high level" programmatic API for pip, it's seems promising.

Upvotes: 5

Related Questions