python: Running command line as administrator - python

I am trying to run FreeCAD, a CAD application, through python.
You can control it through the command line by providing a script (pyhthon) to the executable.
The problem is that you need administrative privileges to run the .exe file. So what I do in Windows, is the following.
I open CMD as an administrator, and then I type:
"C:\Program Files\FreeCAD 0.18\bin\FreeCADCmd.exe" -l "C:\Users\Henry\Desktop\cylinder_macro.py"
This works!
However, I am having difficulties making it work from python.I am trying to implement what has been suggested in this post: Run process as admin with subprocess.run in python
import subprocess
prog = subprocess.run(['runas', '/noprofile', '/user:Administrator', "C:\\Program Files\\FreeCAD 0.18\\bin\\FreeCADCmd.exe","C:\\Users\\Henry\\Desktop\\cylinder_macro.py"])
It does not work.
If I print prog, I get:
CompletedProcess(args=['runas', '/noprofile', '/user:Administrator', 'C:\\Program Files\\FreeCAD 0.18\\bin\\FreeCADCmd.exe', 'C:\\Users\\Dorian\\Henry\\cylinder_macro.py'], returncode=1)
Any ideas of how to get this to work are highly appreciated

Related

Run CMD code line in python, see results in terminal Anaconda

I have this simple code I use to convert files with handbrake
"C:\Program Files\HandBrake\HandBrakeCLI.exe" -i c:\folder -o c:\folder2 --preset-import-file "D:\Handbreak\Script\dudeness.json"
I want to run this line in python and see the output it generates in the terminal
My Intention is to iterate "C:\folder" and "c:\folder2"
First,
import os
os.system("your command")
You can then use a cmd script to run python script

python subprocess - cygwin call - error while loading shared libraries

I am trying to run a .exe file on Windows 10, by calling it inside of a Python 3.8 script with subprocess. I want to execute everything on Cygwin.
The following is my Python function doing that:
os.chdir(r"c:\cygwin64\bin")
cmd = ["bash", "-c", 'cd "C:/Users/usr/file"; ./myexefile']
subprocess.call(cmd)
This will give me the error
C:/Users/usr/file/myexefile.exe: error while loading shared libraries: ?: cannot open shared object file: No such file or directory
While I was trying to figure out, what is the problem,
I executed the file on the cygwin shell directly, without a problem
I ran other commands (cd, pwd,...) with said python script, again without a problem.
I read some solutions of that specific error, in other contexts, saying one is supposed to change path variables, but as the function call works within Cygwin, I don't think this works.
I hope someone can help me, I'm very new to this topic.
EDIT: I also discovered, that the command "ls" does not work. "cd", "pwd" do work.
Solved: I fixed it with adding C:/cygwin64/bin to the Path variable.
This looks like a Windows python trying to run a Cygwin shell
os.chdir(r"c:\cygwin64\bin")
cmd = ["bash", "-c", 'cd "C:/Users/usr/file"; ./myexefile']
subprocess.call(cmd)
assuming that myexefile is a Cygwin program, as the bash is not run with login option
the PATH is not correctly set and the needed shared lib are not found.
If you need to know which DLLs are needed for a program or shared lib:
$ objdump -x octave-5.2.0.exe |grep "DLL Name:"
DLL Name: cygwin1.dll
DLL Name: cygX11-6.dll
DLL Name: cyggcc_s-seh-1.dll
DLL Name: cygstdc++-6.dll
DLL Name: KERNEL32.dll

How to execute 'netsh' command through subprocess.check_output()

I am running a batch file which in turn runs a python file. I want to execute netsh command to change the IPv4 settings from static to dhcp and vice versa.
When I run the below code it executes and returns without any error
import subprocess
cmd = "netsh interface ip show config"
output = subprocess.check_output(cmd)
print output
But when I execute the this piece of code through a batch file the python file exits with exit status 1.
import subprocess
cmd = '''netsh interface ip set address "Local Area Connection 9" dhcp'''
output = subprocess.check_output(cmd)
print output
And It throws an error
Python: can't open file 'Update_DHCP.py': [Errno 2] No such file or directory
The python file and the batch file are in same folder and as per my understanding of updating the dhcp Ip, requires admin rights. I am even running the .bat file as 'Run as Administrator' but got the error [Errno 2]
I have also tried to create a task using task scheduler to grant admin rights to the .bat file and run it. But still, it din't work for me.
Can any one help me execute the command using subprocess.check_output() as I want to use the output returned by the check_output() call. Any help would be appreciated.
I am working on Windows and my python version is 2.7

Running a python script from command prompt

I'm working on a tutorial for a big data class and am having trouble in the command line (Windows 7 Pro). I'm not very familiar with the command line environment so this is probably something simple, but here goes:
I have a python script called mapper.py that is stored in the directory
E:\Documents\School\Math\M 461\MapReduce\PythonScripts
and a file named
Medicare_Provider_Util_Payment_PUF_CY2013.txt
that is stored in
E:\Documents\School\Math\M 461\MapReduce\Data
Python (Anaconda) is installed at C:\Program Files\Anaconda3. I'm trying to feed the file to the script and execute it from the command line using
type Medicare_Provider_Util_Payment_PUF_CY2013.txt
| 'C:\Program Files\Anaconda3\python' mapper.py
I use the apostrophes because otherwise it doesn't like the space in Program Files. However, when I execute this command it says that "The filename, directory name, or volume label syntax is incorrect." I'm not sure where to go from here so any guidance would be appreciated.
The text file and the Python file aren't in the same directory, so your example can't work. Try this series of commands in at your command prompt:
First, make the common parent the current directory
E:
cd E:\Documents\School\Math\M 461\MapReduce
Then run your script, giving the path to the data and python files:
type Data\Medicare_Provider_Util_Payment_PUF_CY2013.txt | 'C:\Program Files\Anaconda3\python' PythonScripts\mapper.py

"No such file or directory" error when calling fc-list in python

I am attempting to scrape a terminal window of the list of fonts installed on the curent hosting server. I have written the following code:
import subprocess
cmd = 'fc-list'
output = subprocess.Popen(cmd, stdout=subprocess.PIPE ).communicate()[0]
but when i call this code, an exception is raised:
[Errno 2] No such file or directory
I can open a terminal window, and this works fine. What am i doing wrong?
You need to provide the absolute path to the executable. When you open a terminal window you then have a shell running which will search in $PATH to find the program. When you run the program directly, via subprocess, you do not have a shell to search $PATH. (note: it is possible to tell subprocess that you do want a shell, but usually this leads to security vulnerabilities)
Here is what you would want to use:
import subprocess
cmd = '/usr/local/bin/fc-list'
output = subprocess.Popen(cmd, stdout=subprocess.PIPE ).communicate()[0]

Categories