1

I have two independent Python scripts that work independently by supplying relevant arguments. Now I need to call python1.py in python2.py and pass the relevant parameters that are accepted by python1.py.

Is this the right approach?

  1. Create a method call_python1(args) inside python2.py
  2. Use subprocess module to execute python1.py
  3. Call call_python1 in the main method of python2.py

Note: Both scripts should keep working independently as they are now.

john-hen
  • 4,410
  • 2
  • 23
  • 40
viki
  • 165
  • 2
  • 11
  • 2
    You run a python script with `subprocess` the same way you execute any other shell command. So the general idea is right. Why is this even questionable? – Barmar Dec 14 '21 at 20:55
  • Have you tried it? It probably would have been quicker to code this up then to ask the question. – Tim Roberts Dec 14 '21 at 20:56
  • How would this hypothetical `call_python1.py` function be implemented? – martineau Dec 14 '21 at 21:09
  • method name was a mistake, updated it – viki Dec 14 '21 at 21:19
  • @TimRoberts: I am asking if there is any better way because I read somewhere that calling another python.py inside a python program is antipattern – viki Dec 14 '21 at 21:20
  • 2
    @viki It is, *if* you have control over the python script, because you then have the option to tweak the script (if necessary) and make it importable as a module, so that you don't have to cross process boundaries. Otherwise, the language the program is written in is irrelevant. – chepner Dec 14 '21 at 21:24
  • Does [Run a Python script from another Python script, passing in arguments](https://stackoverflow.com/questions/3781851/run-a-python-script-from-another-python-script-passing-in-arguments) answer your question? – wwii Dec 14 '21 at 21:59

2 Answers2

2

This is the right approach under the described circumstances: that both scripts should continue to work as independent command-line tools. (If not, it would be advisable to import as a module.)

In this case, one would typically want to make sure the other script is executed by the same Python interpreter, in case several different versions are installed. So in python2.py, and possibly inside that call_python1 function, you would start the subprocess like so:

import subprocess
import sys

subprocess.run([sys.executable, 'python1.py', ...])

Here sys.executable gives "the absolute path of the executable binary for the [current] Python interpreter". The ellipsis denotes the additional command-line arguments to be passed, added as separate strings to the list.

john-hen
  • 4,410
  • 2
  • 23
  • 40
  • thanks for you valuable post. if I understand correctly, to import python1.py as a module, in that case, it can't be used as a standalone command line script? – viki Dec 14 '21 at 22:07
  • No, it can. But you'd have to pass the arguments differently. So this adds some complexity to the code. Which may be warranted. It basically depends on whether you see that Python module as an independent script, or if it's a library that provides certain functionality. And it can be both of these things too, if coded properly. – john-hen Dec 14 '21 at 22:23
  • I tried the following but still failing with error "FileNotFoundError: [Errno 2] No such file or directory" Scenario- I want to run the script python1.py inside python2.py Inside python2.py I defined a method def call_script1( a, b, c, d): – viki Dec 16 '21 at 00:08
  • Here is the code that I tried and failed with above error (File not found) I tried the following but still failing with error "FileNotFoundError: [Errno 2] No such file or directory" Scenario- I want to run the script python1.py inside python2.py Inside python2.py I defined a method def call_script1( a, b, c, d): args = " -programArgs=' " + a+ " " +b+" "+c+" " +d+" ' " script_location = /root/dir/python1.py script_launch = script_location + args subprocess.run([script_launch]) – viki Dec 16 '21 at 00:15
0

You can surely invoke the python1.py using a subprocess but I would avoid this kind of invoking since you will need to manage the subprocess lifecycle (OS permissions, check the status code of the subprocess, eventually the status of the process, etc.).

My advice is to convert python1 to an importable package (see the official python documentation for more information).

By doing so you will have a set of benefit lik: explicitly defined requirements, version, etc. It will be eventually reused by a python code that doesn't want to use subprocesses.

Giovanni Patruno
  • 651
  • 9
  • 15
  • thanks, do you know if importable package will still continue to be used as standalone command line script ? – viki Dec 14 '21 at 22:08
  • You could use it by running a submodule python which imports the scripts and execute the code, in that case you could consider creating a CLI utility – Giovanni Patruno Dec 15 '21 at 10:53
  • 1
    Nothing worked for me using subprocess.run. Not sure if its because of some security on the server or some other issue. For now I am going with importing the other python script as a module and using the relevant methods in the main script. Thanks – viki Dec 18 '21 at 21:43