Run pip in python idle
Asked Answered
A

5

9

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.

Anstus answered 25/1, 2016 at 9:58 Comment(2)
Try pip.main(["install", "--user", "somepackagenamehere"]) for example.Tonjatonjes
python 3.8: from pip._internal import main; main("install", "--user", "pypackage")Schnook
U
7

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"])
Us answered 20/8, 2020 at 14:18 Comment(0)
P
6

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.

Pyro answered 26/1, 2016 at 8:44 Comment(0)
T
5

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.

Trike answered 25/1, 2016 at 10:40 Comment(3)
Did you actually test the claim that a current IDLE session will not see a newly installed package? I am pretty sure that if you add a package to site-packages by any means, whether with pip or otherwise, from the command line or otherwise, it should be available to the next import statement run by the same python version, whether IDLE is involved or not. I just tried installing pillow from a command line while IDLE was running and 'import PIL' worked immediately after.Pyro
@Terry I tried on IDLE with py2exe using pip.main, it couldn't import soon after. Thinking about, could be because this installation i tested on was a fresh one and i used the '--user' flag on pip.Trike
I have Python installed for all users and don't use the -user flag. The pip help says "Install to the Python user install directory for your platform. Typically ~/.local/, or %APPDATA%\Python on Windows. (See the Python documentation for site.USER_BASE for full details.)" My sys.path actually has 'C:\\Users\\Terry\\AppData\\Roaming\\Python\\Python35\\site-packages', Check that where pip actually puts files with -user matches what is on sys.path.Pyro
W
4

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

Wideawake answered 25/1, 2016 at 12:5 Comment(1)
You need to either quote the command passed to Popen, in this case, "apt-get numpy", or pass a list of strings, such as ['pip', 'install', 'numpy']. For a reason given in my answer, using subprocess is currently the easiest way to run pip from within a python program.Pyro
V
1

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.

Ventage answered 7/11, 2019 at 15:2 Comment(1)
I don't believe this answers the original question. The original question was poorly formulated anyway.Attain

© 2022 - 2024 — McMap. All rights reserved.