0

I am trying to write a script to open the windows console, then install a python package with pip and finally close.

This was my initial try

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

The installations fails. It seems I need to open first the console and then pip, to make it work so 2nd try

import os os.system('cmd.exe') os.system('pip install package') 

If I do it in this way it is waiting until the console is closed to execute pip

3rd try

import os os.system('cmd.exe') os.system('exit') os.system('pip install package') 

Exit is not recognised

I tried also with os.system('taskkill cmd.exe') 

or

import sys sys.exit() 

or

raise SystemExit 

No success so far

1
  • os.system('C:\Python\Python35-32\Scripts\pip install package') - give path to pip on your system Commented May 12, 2016 at 7:55

1 Answer 1

2

pip is a package. This means you can do import pip and run the python function directly. A quick look using help(pip) shows there is a pip.commands package which provides install which looks promising.

You cannot run multiple system commands as your earlier examples. Each such command will run in a separate subprocess. Most likely os.system("cmd /c pip install package") might have worked as that runs a cmd shell and passes a command to it to be run. I'd expect to have to pass the full path to the pip executable though.

Sign up to request clarification or add additional context in comments.

1 Comment

os.system already runs using cmd /c. But you don't need the cmd shell. It works with subprocess.call without shell=True. pip.exe opens its own console window (that's conhost.exe, not cmd.exe) if it doesn't inherit one. When pip exits, so does conhost if no other program has attached to it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.