A
A
Alexander Lomunov2020-04-22 10:29:21
Python
Alexander Lomunov, 2020-04-22 10:29:21

How to work with cmd through code in a file in Python?

the simplest example, installing an update for a specific pip package in cmd looks like this:

pip install <...> --upgrade

the question is: how, using a python file (script), to automate module updates, without manual input on the command line?

Roughly speaking, as I imagine it, in the .py file I have to write the command that I want to pass to cmd. I just don't know how.

The question seems simple to me, I'm just a beginner

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dr. Bacon, 2020-04-22
@bacon

There is already a ready-made pipenv, poetry or maybe some other similar ones. Well, read the doc about pip, it's possible to work with it from python

O
o5a, 2020-04-22
@o5a

subprocess module.
subprocess.run to run a command with execution pending.
subprocess.Popen to run the command in a separate process (will not interrupt the execution of the main script).
Example

import subprocess

module = "lxml"
subprocess.run(["pip", "install", module, "--upgrade"])

S
siaila, 2020-04-22
@siaila

The most standard tool is the os module. What you need:

import os

os.system(f'pip install {module} --upgrade' )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question