How do you run another program from a python script? - python

This is my code so far
from subprocess import call
call("/Users/oscar/Desktop/Controlled Assessment/Currency Converter.py")
When I run it I get this error message
Traceback (most recent call last):
File "/Users/oscar/Desktop/Controlled Assessment/ISBN Checker.py", line 10, in <module>
call("/Users/oscar/Desktop/Controlled Assessment/Currency Converter.py")
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/subprocess.py", line 470, in call
return Popen(*popenargs, **kwargs).wait()
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/subprocess.py", line 744, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/subprocess.py", line 1394, in _execute_child
raise child_exception_type(errno_num, err_msg)
OSError: [Errno 8] Exec format error

Since it looks like you're trying to execute another Python script, why not import it?
import Currency_Converter
And then run the modules you want in there? Take the space out of the file name and replace it with an underscore.

Check the interpreter directive from your script.
It should start with:
#!/usr/bin/env python
print 'your python script...'
That means that when you call this script directly, it will be interpreted by python binary.
You can get more info on http://en.wikipedia.org/wiki/Shebang_(Unix)

use subprocess.Popen
import sys, subprocess
subprocess.Popen([sys.executable, "myscript.py"])
This should run myscript.py within the Python command line.

Related

Using python subprocess to use the "source" linux command with a bashrc file [duplicate]

This question already has answers here:
Calling the "source" command from subprocess.Popen
(9 answers)
Closed 8 months ago.
I am tasked with automating the process of running bash script using python. Unfortunately I am not responsible for the bash script itself, so I have no idea how it works. When I run the script directly in the terminal using source adastralrc.sh , it works perfectly and gives the desired output.
However when I try to get python to run the file by using subprocess and the exact same command as the argument:
import subprocess
commands = ['source' , 'adastralrc.sh']
p = subprocess.run(commands)
I get the following error:
Traceback (most recent call last):
File "test2.py", line 6, in <module>
p = subprocess.call(commands)
File "/usr/lib64/python3.6/subprocess.py", line 287, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/lib64/python3.6/subprocess.py", line 729, in __init__
restore_signals, start_new_session)
File "/usr/lib64/python3.6/subprocess.py", line 1364, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'source adastralrc.sh': 'source adastralrc.sh'
(venv-c3dns) [vivegab#adl20213d1bld01 vivegab] (cth01/dns_mano_dev)
I am fairly inexperienced with subprocess but I thought that it was just an improved version of os.system() and should just enter the commands as if they were being typed by a person. So why am I getting the error above and what can be done to fix this?
def shell_source(script):
"""Sometime you want to emulate the action of "source" in bash,
settings some environment variables. Here is a way to do it."""
import subprocess, os
pipe = subprocess.Popen(". %s; env" % script, stdout=subprocess.PIPE, shell=True)
output = pipe.communicate()[0]
env = dict((line.split("=", 1) for line in output.splitlines()))
os.environ.update(env)
shell_source(adastralrc.sh)
Looks like a duplicate

subprocess.call says there's no such file or directory, but os.path.isfile says there is

I have an executable file that I'd like to run from Python. I define a path variable pointing at it:
>>> path = '/root/Cognos/Cognos/linuxi38664h/issetupnx'
I verify that I am in fact pointing at a file and not a directory:
>>> from os.path import isdir, isfile
>>> isdir(path)
False
>>> isfile(path)
True
But as soon as I try to run the executable file via subprocess.call...
>>> from subprocess import call
>>> call([path])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1308, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
It tells me the file doesn't exist now.
The only possibility I can think of is that maybe the executable itself is being found and run fine, but the executable is failing saying that something it needs (what?) isn't found. I'm not sure how I would test this theory, though... or even if it's possible.
Another possibility might be some kind of permissions issue? Although I can't think of why I would have proper permissions to see the file but then I suddenly wouldn't be able to see it the moment I try running it.
Instead of using call, I should have used check_output. Then the error would have included all of the messages printed to stdout and stderr.
path = '/root/Cognos/Cognos/linuxi38664h/issetupnx'
from subprocess import check_output
check_output([path])
Then I would have gotten a more detailed message about how it failed to load shared libraries.
execute this code as root:
import subprocess as sp
path = '/root/Cognos/Cognos/linuxi38664h/issetupnx'
proc = sp.Popen([path],stdin=sp.PIPE)
proc.communicate()

Cannot run Python from Cron

I have a python script that will read the temperature of from a probe on the GPIO pins of a Raspberry-Pi, and will append that temperature to a log file. Running the script form terminal with sudo permissions works fine:
sudo python /home/pi/temp.py
I've attempted to run the script every 15 minutes from sudo's crontab file with the line:
*/15 * * * * python /home/pi/temp.py
This fails, with the output being
Traceback (most recent call last):
File "/home/pi/temp.py", line 8, in <module>
subprocess.call(['modprobe', 'w1-gpio'])
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
I know the issue is with the modprobe subprocess call, but I can't identify what exactly. In my script, I have the following code related to the issue:
import subprocess
subprocess.call(['modprobe', 'w1-gpio'])
subprocess.call(['modprobe', 'w1-therm'])
This is because cron has its own PATH variable and doesn't use the same path that you do.
For that reason, it would be advisable to call any programs that you use (especially through python's subprocess) with an absolute path to the executable
You could do which modprobe on the commandline to find where modprobe lives (probably in /bin/), and then change your call in subprocess.py to subprocess.call(['/bin/modprobe', 'w1-gpio'])

subprocess call error, invalid application

So i am running into a problem using subprocess.call() and i think i may just be calling it wrong. I am using:
subprocess.call('testingosfile.py')
and i get the traceback:
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
subprocess.call('testingosfile.py')
File "C:\Python27\lib\subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 893, in _execute_child
startupinfo)
WindowsError: [Error 193] %1 is not a valid Win32 application
and the contents of testingosfile.py is:
print "hello world!"
raw_input('....')
how do i manage to get this running?
Thank you in advance for your replies.
The error message makes perfect sense - with subprocess, you can only start an executable. So, to fix it, you should start an executable. Specifically, you should start the Python interpreter and tell it to run your script. Something like
subprocess.call(['python.exe', 'testingosfile.py'])
should work, although you might have to provide the full path to the Python interpreter (I can't test at the moment).
However, have you considered importing testingosfile.py instead? Whenever you import a Python script, all the commands in that script are run. Using
import testingosfile
inside a function in order to execute the commands would be poor style, but you could package up the useful commands of testingosfile.py into some function. Then, you could use
import testingosfile
at the top of your main script, and just call that function whenever you want to print Hello World and get the user's input.
try subprocess.call("myfile.ext", shell=True)

how to use subproces.Popen correctly on windows xp?- shows windowserror 2 while accessing npm

I am trying to run an existing python code, and having issues with it.
This program required npm program installed and which is installed at C:\Program Files\nodejs\npm in my computer. When I run the following code, as a part of the whole program, it throws errors.
def popen_results(args):
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
return proc.communicate()[0]
def installed():
"""docstring for npm_installed"""
return popen_results(["which", "npm"]).strip()
This is the complete stack of the error thrown--
Checking for node and dependencies
Traceback (most recent call last):
File "deploy\deploy.py", line 344, in <module>
main()
File "deploy\deploy.py", line 287, in main
if not check_deps():
File "deploy\deploy.py", line 201, in check_deps
return npm.check_dependencies()
File "C:\Documents and Settings\Sunil\workspace\khan\src\deploy\npm.py", line
38, in check_dependencies
if not installed():
File "C:\Documents and Settings\Sunil\workspace\khan\src\deploy\npm.py", line
13, in installed
return popen_results(["which", "npm"]).strip()
File "C:\Documents and Settings\Sunil\workspace\khan\src\deploy\npm.py", line
8, in popen_results
proc = subprocess.Popen(args, stdout=subprocess.PIPE)
File "C:\python25\lib\subprocess.py", line 594, in __init__
errread, errwrite)
File "C:\python25\lib\subprocess.py", line 822, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
I agree with martineau, it is unable to find which. The script may have been written with the assumption it was going to be run in a unix environment which would most likely have the "which" command available and in the default PATH. Since it looks like you're running this on Windows, I don't think it's going to work.
It looks like there is some alternatives to which on Windows though, discussed here: Is there an equivalent to which on Windows?

Categories