0

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+"\n")


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:\Users\dani2\PycharmProjects\autoIISconfig>cd C:\Users\dani2\PycharmProjects\autoIISconfig\venv\Scripts

(venv) C:\Users\dani2\PycharmProjects\autoIISconfig\venv\Scripts>activate

(venv) C:\Users\dani2\PycharmProjects\autoIISconfig\venv\Scripts>timeout 5

(venv) C:\Users\dani2\PycharmProjects\autoIISconfig\venv\Scripts>exit
None

Process finished with exit code 0

The code does not wait the 5 seconds.

Thank you for your help :)

  • 1
    Please read about the [XY problem](https://xyproblem.info/). What you are doing is completely nonsense. `activate` is just a batch file. Open a [command prompt](https://www.howtogeek.com/235101/), run where `activate` or `dir "%UserProfile%\PycharmProjects\autoIISconfig\venv\Scripts\activate*" /B` to see its full qualified file name. A batch file is a text file. You can view it with any text editor like the one used to write the Python script. It just defines multiple environment variables in environment of Windows command process `cmd.exe` interpreting this batch file. – Mofi Dec 30 '20 at 17:27
  • There is no reason to use `%SystemRoot%\System32\timeout.exe` to wait five seconds after execution of the batch file finished. The environment variables are defined already after finishing execution of the batch file in environment of started `cmd.exe`. It is in general a complete nonsense to use a Python script interpreted by very powerful `python.exe` to start very old and less powerful interpreter `cmd.exe` and interact with it via the standard input, output and error streams. – Mofi Dec 30 '20 at 17:31
  • The environment variables defined for `cmd.exe` process executing the batch file can be defined also for `python.exe` process which is processing the Python script. Read in Python documentation about [os.environ](https://docs.python.org/3/library/os.html) and look on [How to set environment variables in Python?](https://stackoverflow.com/questions/5971312/) or any other Python tutorial page about getting and setting of environment variables with Python. – Mofi Dec 30 '20 at 17:37
  • 1
    In fact the `subprocess` module makes it possible to start a process with a list of environment variables which is not a copy of the currently running Python process environment variables list. Read in the Python documentation about [subprocess module](https://docs.python.org/3.8/library/subprocess.html) about the *env* argument. It looks like you have not yet read the documentation of the function used by you. Otherwise you would not run `cmd.exe` with `subprocess.Popen()` with argument `shell=True` resulting in starting `cmd.exe` on Windows to run `cmd.exe`, i.e. two instances of `cmd.exe`. – Mofi Dec 30 '20 at 17:42
  • 1
    Please take a look on [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) It explains very detailed how environment variables are managed on Windows. It is impossible that any process modifies the environment variables of any already started process as that would mean any application could access the memory of any other application to just read or even modify it. That would be a real big security problem if that would be possible. – Mofi Dec 30 '20 at 17:44

0 Answers0