Pass file paths from Python to shell script [duplicate] - python

This question already has answers here:
Actual meaning of 'shell=True' in subprocess
(7 answers)
Closed 8 months ago.
I would like to run a shell script from Python 3 in Linux passing two arguments that contain file paths to two different files. The shell script then calls a programme written in Python 2.
In Python 3, I call the shell script like this:
import os
import sys
os.chmod('/path/to/sh', 0o777)
subprocess.call(['/path/to/sh', '/path/to/file1', '/path/to/file2'], shell=True)
My shell script looks like this:
#!/bin/sh
set -x
path1=$1
path2=$2
python2 path/to/programme "$path1" "$path2"
Now, the file paths are empty, and the shell script returns something like python2 path/to/programme '' ''. Does someone know how I could correctly pass the file paths so that the programme written in Python 2 can read them?
Or is there even an easier solution such as using subprocess to directly call the programme written in Python 2?

There is no need for the shell script. You can use subprocess to run python2 directly.
a.py
#!/usr/bin/env python3
import subprocess
subprocess.call(['python2', './b.py', 'foo', 'bar'])
b.py
#!/usr/bin/env python2
import sys
print sys.argv
Running ./a.py outputs ['./b.py', 'foo', 'bar'].
You could also try using past.translation instead:
The past module provides an experimental translation package to help with importing and using old Python 2 modules in a Python 3 environment.

shell=True is only needed if you do something like
subprocess.run("/path/to/sh /path/to/file1 /path/to/file2", shell=True)
where the shell will split the single string into arguments that will identify as the program name and its arguments. But you already have the program name and its arguments identified, so
subprocess.run(['/path/to/sh', '/path/to/file1', '/path/to/file2'])
is all you need.
By using a list and shell=True, you are essentially asking Python to execute
sh -c /path/to/sh /path/to/file1 /path/to/file2
which uses /path/to/file1 to set the value of $0, not $1, in the command to execute.

Related

Runnig install.sh file with python [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 1 year ago.
How can i run install.sh file using python code.Purpose is to implement software update functionality.The sh file is generated using makeself and it is a self-extractable archive.I get this output when i use os.system or subprocess.run
Failed to parse arguments: Unknown option -title
NB: The Script file don't require any arguments
subprocess.call (Python <=3.5) and subprocess.run (Python >3.5) would be a safer alternative to os.system.
Solution 1 : Use SubProcess
import subprocess
subprocess.call(['/path/to/your/script.sh', 'argument1', 'argument2', ..., 'argumentn'])
Solution 2 : Use Os
import os
os.system('/path/to/your/script.sh argument1 argument2 ... argumentn'])
Both will work fine, if you have the choice it's better to use subprocess since it will handle for you the formatting of your command with thing such as space in the command line or special characters.

exec() python2 script from python3

I would like to know if it is possible to execute a python2 script from a python3 script.
I have a file written using py3 that must execute legacy code written in py2 to obtain dictionaries for processing within the initial file.
The line in py3 to call the mentioned py2 script is
exec(open('python2script.py').read())
The script runs without error until it begins processing python2script.py, at which point it crashes at the first difference with version3.
As the comments pointed out, exec() uses the current python implementation, so you can't execute python 2 code from python 3 using it.
Unless you port it, your best bet is simply to call it as a subprocess, using either os.system..:
./py3.py
#!/usr/bin/env python3
import os
print('running py2')
os.system('./py2.py')
print('done')
./py2.py
#!/usr/bin/env python2.7
print "hello from python2!"
Then (after making them both executable) run:
$ ./py3.py
Or alternatively you can use the more flexible subprocess, which allows you to pass data back and forward more easily using a serialising module such as json so that you can get your results from the python2 script in your python3 code:
./py3.py
#!/usr/bin/env python3
import json
from subprocess import PIPE, Popen
print('running py2')
py2_proc = Popen(['./py2.py'], stdout=PIPE)
# do not care about stderr
stdout, _ = py2_proc.communicate()
result = json.loads(stdout.decode())
print('value1 was %s, value2 was %s' % (result['value1'], result['value2']))
./py2.py
#!/usr/bin/env python2.7
import json
my_result = {
'value1': 1,
'value2': 3
}
print json.dumps(my_result)
Like that it may be easy to pack up the data you need and transport it over.
Note: I have used a very simple environment setup here using my system's python2.7 and python3. In the real world the most painful thing about getting this sort of thing to work properly is configuring the environment correctly. Perhaps, e.g., you are using virtual environments. Perhaps you are running as a user which doesn't have the right python2 version in their path. Perhaps you can't make the files executable and so have to specify the path to python in your subprocess / os.system call. There are many options and it is very complicated, but out of the scope of the question. You just have to read the doc pages very carefully and try a few things out!

python: how to run a program with a command line call (that takes a user's keystroke as input) from within another program?

I can run one program by typing: python enable_robot.py -e in the command line, but I want to run it from within another program.
In the other program, I imported subprocess and had subprocess.Popen(['enable_robot', 'baxter_tools/scripts/enable_robot.py','-e']), but I get an error message saying something about a callback.
If I comment out this line, the rest of my program works perfectly fine.
Any suggestions on how I could change this line to get my code to work or if I shouldn't be using subprocess at all?
If enable_robot.py requires user input, probably it wasn't meant to run from another python script. you might want to import it as a module: import enable_robot and run the functions you want to use from there.
If you want to stick to the subprocess, you can pass input with communicate:
p = subprocess.Popen(['enable_robot', 'baxter_tools/scripts/enable_robot.py','-e'])
p.communicate(input=b'whatever string\nnext line')
communicate documentation, example.
Your program enable_robot.py should meet the following requirements:
The first line is a path indicating what program is used to interpret
the script. In this case, it is the python path.
Your script should be executable
A very simple example. We have two python scripts: called.py and caller.py
Usage: caller.py will execute called.py using subprocess.Popen()
File /tmp/called.py
#!/usr/bin/python
print("OK")
File /tmp/caller.py
#!/usr/bin/python
import subprocess
proc = subprocess.Popen(['/tmp/called.py'])
Make both executable:
chmod +x /tmp/caller.py
chmod +x /tmp/called.py
caller.py output:
$ /tmp/caller.py
$ OK

Standard input inconsistency between command line and subprocess.call

I would like to create a file that will be used as standard input for a python script, and invoke said script with subprocess.call.
When I do it directly in the command line it works fine:
The input file:
# test_input
1/2/3
The python script
# script.py
thisDate = input('Please enter date: ').rstrip()
The following command works just fine:
python script.py < test_input
But when I try to do the following from within another python script, it doesn't work. (from this)
outfile1 = open('test_input', 'w')
outfile1.write('1/2/3')
outfile1.close()
input1 = open('test_input')
subprocess.call(['python', 'script.py'], stdin=input1)
But then I get the following error:
>>>thisDate = input('Please enter date: ').rstrip()
>>>AttributeError: 'int' object has no attribute 'rstrip'
When I did some debugging, it seems that it is getting the integer 0 as the input.
What is causing the inconsistency here? Are the two methods not equivalent (evidently they are not, but why)? My ultimate goal is to perform the exact same task as the above command line version that worked.
Thank you
You are using input when it should be raw_input, input in python2 will eval the string. If you run the script with python3 it will work as is, for python2 change to raw_input.
Using check_call is usually a better approach and using with to open your files.
import subprocess
with open('test_input') as input1:
subprocess.check_call(['python3', 'script.py'], stdin=input1)
So chepner was correct. When I amended the following line:
subprocess.call(['python', 'script.py'], stdin=input1)
to:
subprocess.call(['python3', 'script.py'], stdin=input1)
it worked just fine.
(I am trying to do this in python3)
In the first instance, the file has two lines, and input() reads and parses the first line, which is a comment.
In the second case, the comment line is missing, so Python reads and parses a number.
You probably meant to use raw_input(), or run the script with Python 3.
(You probably also meant for the input file to end with a newline, and it doesn't really make sense to use subprocess.call() to run Python when you are already running Python.)
python script.py < test_input command should fail. You might mean: python3 script.py < test_input instead due to the difference between input() vs raw_input() on Python 2 as mentioned in other answers. python as a rule refers to Python 2 version.
if the parent script is run only using python3 then you could use sys.executable to run the child script using the same python version (the same executable):
#!/usr/bin/env python3
import subprocess
import sys
with open('test_input', 'rb', 0) as input_file:
subprocess.check_call([sys.executable or 'python3', 'script.py'],
stdin=input_file)
If the parent and the child may use different python versions then set the correct shebang in script.py e.g., #!/usr/bin/env python3 and run the script directly:
#!/usr/bin/env python
import subprocess
with open('test_input', 'rb', 0) as input_file:
subprocess.check_call(['./script.py'], stdin=input_file)
Here, the child script may choose its own python version. Make sure the script has executable permissions: chmod +x script.py. Note: Python Launcher for Windows understands the shebang syntax too.
Unrelated: use .communicate() instead of outfile1.write('1/2/3'):
#!/usr/bin/env python3
from subprocess import Popen, PIPE
with Popen(['./script.py'], stdin=PIPE, universal_newlines=True) as p:
p.communicate('1/2/3')

Passing Command line argument to Python program using IDLE? [duplicate]

This question already has answers here:
When running a python script in IDLE, is there a way to pass in command line arguments (args)?
(12 answers)
Closed 10 years ago.
I have downloaded a python file xxxxxx.py that is supposed to run on the command line by
typing: python xxxxxx.py filename1 filename2
and that should take these two files as arguments.
I was wondering if there is a way I can use IDLE to pass in these arguments. Is there a way other than setting sys.argv ?
Thanks
It depends on the content of your Python file. If it is well-written, like:
#! /usr/bin/env python
def process(files):
for file in files:
# ...
if __name__ == '__main__'
# some error checking on sys.argv
process(sys.argv[1:])
sys.exit(0)
Then you could simply import the python file and run it like:
import name_of_file
# ...
name_of_file.process([file1, file2, file3])
# ...
So, it really depends on how it is written. If it isn't written well but you can edit it, I would refactor it so that it can be used as a library; otherwise, I would use the subprocess module to invoke the program.
You can do this from the command line with:
idle.py -r scriptname.py put arguments here
You can try a different IDE like ActivePython
Or you can patch IDLE:
http://bugs.python.org/issue5680

Categories

Resources