So I want to run a PS script that I have in my Python code, I just found this article: https://data-geek.medium.com/running-powershell-script-with-python-53a908da7d34
Since I'm using MacOS, it I changed it:
import subprocess, sys
p = subprocess.Popen(["powershell.app",
"/Users/myuser/Downloads/powershellscript.ps"],
stdout=sys.stdout)
p.communicate()
I've got an error, I guess the problem is with powershell.app .
How can I execute this PS script from my Python code?
instead of using "powershell.app" you want to use "pwsh". This is the command the starts up powershell
Related
When I execute "Get-WindowsOptionalFeature -Online" command in Windows2016 server powershell, the output is all fine, but when I execute with the Python script below,
cmd = os.popen("Get-WindowsOptionalFeature -Online")
print(cmd.read())
the output is empty.
is there something wrong with my code?
I tried to exec ipconfig using this code, it worked fine
My error comes from my ignorance of powershell. I regard powershell as cmd, so I directly use os.popen to call the command to be executed. In fact, you need to call powershell first to execute the command that needs to be executed, the following is an example:
import subprocess;
process=subprocess.Popen(["powershell","Get-WindowsOptionalFeature -Online"],stdout=subprocess.PIPE);
result=process.communicate()[0]
I want run another python file in my code.It's simple:
import os
command = "python3 简易时钟.py"
os.system(command)
When I use Pycharm, it will run normally.
When I use IDLE, nothing happened. Why?
macOS 10.15.7
python 3.7.9
This works in idle(Windows) for me. Try using the exact path of the file.
Like
command = r"C:\Users\(your_username)\Documents\python3 简易时钟.py"
or whatever the path of the file is.
Or if the python3 简易时钟.py file doesn't generate any output file the python3 简易时钟.py file will run and get closed in the background once it is completed execution.
So I have a project I'm working on, and one thing I need to do is to run in the background the "netstat -nb" command at the PowerShell as admin and recive the results to the python program.
I've been searching the web for a solution but never found one efficient out there. I'd be glad for some help.
"netstat -nb"
If you want to execute the netstat command you can do so from Python directly.But you need to be running Python script as a Admin:
import subprocess
subprocess.call("netstat -nb")
If you need to access the powershell netstat values inside the Python script then you can set variable in powershell and pass it to Python script.
Following is the powershell command:
$con=netstat -nb
& python.exe "FullPath of Python script file"-p $con
Python script:
import sys
print(sys.argv[5])
for conn in sys.argv:
print(conn)
Here we are looping the parameters passed (netstat output) and displaying.So you are passing powershell command result to Python script and displaying it there.
Following columns would be displayed:
You can use subprocess library. About subprocess module you can check this documentation for more detail and features. It helps you to solve your problem. You can use subprocess.Popen or subprocess.call or to get output from another script you can use subprocess.check_output.
You can use it like this piece of code:
import subprocess
import sys
output = subprocess.Popen(['python','sample.py','Hello'],stdout=subprocess.PIPE).communicate()
print(output)
Inside of the sample.py script is:
import sys
my_message = "This is desired output. " + sys.argv[1]
print(my_message)
In your case you can use Popen as:
subprocess.Popen(['netstat','-nb'],stdout=subprocess.PIPE).communicate()
I am trying to install node.js and then check appium version using appium -v
import os,subprocess
os.system('node.msi')
os.system('exit')
os.system('appium -v')
node.msi is a node file on my computer. when i do it through cmd, appium -v works if i do it in a new cmd, but it doesn't work if i keep using the same cmd. so i was hoping that after exit, my code should have worked. can someone point out what i am doing wrong here.
Most likely, the installation of node.msi modifies your system's PATH variable. This change does not become visible inside your running Python process.
If you know the path to your node installation, you can specify it explicitly in a call such as
subprocess.run([r'C:\node\bin\apium.exe', '-v'])
I assume that you are running Windows here. When a console starts, it reads its environment from the registry. That explains why it works when you open a second cmd console.
That means that you have to ask Python to lauch the command appium - v in a new console (and not only a new cmd.exe shell).
It can be done through os.system by using start:
os.system("start /W appium -v")
or depending on what is really appium:
os.system("start /W cmd /c appium -v")
You could also directly use the subprocess module (which offer more configuration than os.system)
p = subprocess.Popen("cmd / c appium -v", creationflags=subprocess.CREATE_NEW_CONSOLE)
p.wait()
Depending on what appium is, the following could be enough:
p = subprocess.Popen("appium -v", creationflags=subprocess.CREATE_NEW_CONSOLE)
p.wait()
So as the title says, I'm having problem starting a new subprocess under Fedora. Now the situation is, I have a main python script from which I start a couple of other python processes using:
import subprocess
subprocess.Popen(['python', '-m', 'first_child.run', 'start'], shell=False)
Now this works fine on MacOS, debian and windows. On fedora if I run it from Aptana 3 IDE it also works, the only problem is when i try to run this main scrip from a terminal, where I get:
OSError: [Errno 2] No such file or directory
Do you have any ideea what can be the problem here?
Regards,
Bogdan
Sorry if this is something you've already thought of -- but the most common cause of OSError from calls to subprocess is that it cannot find the process
http://docs.python.org/library/subprocess.html#exceptions
Are you absolutely certain python is in your path?
I know you're probably going to point out you ran this script from the python executable -- but I thought I'd take a shot that perhaps you specified the full path to python when you ran it from the terminal.
For fun, right before the call to subprocess, you could dump your PATH
import os
print os.environ['PATH']
It's your current working directory. I don't think the problem is that it can't find python, the problem is that it can't find first_child.run.
Try printing os.getcwd() before you launch the subprocess, and see if it's different in the terminal vs. in the IDE.
On a side note, it's probably more reliable to use sys.executable as the python you use in your subprocess, as opposed to just saying python. For example, subprocess.Popen([sys.executable, '-m', 'first_child.run', 'start'], shell=False)