I'm trying to write a code that will make changes to an external Python venv. I'm using the Subprocess module to connect to cmd to do so.
But I faced a problem. The program is finishing running before the cmd code actually finishes the job.
Is there anyway to sync it? And maybe even display live cmd results?
The code:
import subprocess
cmds = [f"""cd {venv_path}""",
"activate",
"timeout 5", #for example
"exit"]
shell_instance = subprocess.Popen('cmd.exe',
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
shell=True)
for cmd in cmds:
shell_instance.stdin.write(cmd+"
")
shell_instance.stdin.close()
for line in shell_instance.stdout:
print(line.strip())
print(shell_instance.returncode)
The results:
Microsoft Windows [Version 10.0....]
(c) 2020 Microsoft Corporation. All rights reserved.
(venv) C:Usersdani2PycharmProjectsautoIISconfig>cd C:Usersdani2PycharmProjectsautoIISconfigvenvScripts
(venv) C:Usersdani2PycharmProjectsautoIISconfigvenvScripts>activate
(venv) C:Usersdani2PycharmProjectsautoIISconfigvenvScripts>timeout 5
(venv) C:Usersdani2PycharmProjectsautoIISconfigvenvScripts>exit
None
Process finished with exit code 0
The code does not wait the 5 seconds.
Thank you for your help :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…