gis20
gis20

Reputation: 1094

open and close windows console from python

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

Upvotes: 0

Views: 1468

Answers (1)

patthoyts
patthoyts

Reputation: 33203

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.

Upvotes: 2

Related Questions