0

I would like to open a powershell from my python script, then launch another python script in this newly created powershell.

import subprocess

subprocess.call("start C:\\Windows\\System32\\WindowsPowerShell\\v1.0", shell=True) 
# Here, I would like to write this in the new opened powershell : python ./hello_world.py (then press ENTER)

input("end")

Any idea how I can do that ? Thanks and have a good day !

I tried the subprocess.Popen + communicate but nothing was written in my new powershell

3 Answers 3

1

Another alternative:

import subprocess
subprocess.run(["powershell", "-Command", "python hello-world.py"], capture_output=True, cwd="C:\\your\\path")

Will output:

CompletedProcess(args=['powershell', '-Command', 'python hello-world.py'], returncode=0, stdout=b'hello world\r\n', stderr=b'')

Where stdout=b'hello world\r\n' is your output.

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

Comments

0

you can just use os.system()

import os

os.system("start powershell python path_to_file") 
# some code

5 Comments

Not really what I need for 3 reasons : 1/ I would like to open in a new window to be able to see logs for each python file I run 2/ I would like to use powershell (blue background) 3/ I would like the new powershell to be independant from parent (meaning I can close parent and still have the child powershells)
if you use threading or multiprocessing you can achieve this
try os.startfile("powershell.exe")
This works perfectly to open the new powershell, but then how do I write "python ./hello_world.py" in my new powershell please?
ok I found a way os.system("start powershell python path_to_file")
0

I think subprocess is not intended to interact with active window. but to send stream data to pipeline.

try using pyautogui to interact with Powershell window.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.