I'm working on a project where I would like to run a same script but with two different softwares api.
What I have : -One module for each software where I have the same classes and methods names. -One construction script where I need to call these classes and method.
I would like to not duplicate the construction code, but rather run the same bit of code just by changing the imported module.
Exemple :
first_software_module.py
import first_software_api
class Box(x,y,z):
init():
first_software_api.makeBox()
second_software_module.py
import second_software_api
class Box(x,y,z):
init():
second_software_api.makeBox()
construction.py
first_box = Box(1,2,3)
And I would like to run construction.py with the first module, then with the second module. I tryed with imports, execfile, but none of these solutions seems to work.
What i would like to do :
import first_software_module
run construction.py
import second_software_module
run construction.py
2 Answers 2
You could try by passing a command line argument to construction.py
.
construction.py
import sys
if len(sys.argv) != 2:
sys.stderr.write('Usage: python3 construction.py <module>')
exit(1)
if sys.argv[1] == 'first_software_module':
import first_software_module
elif sys.argv[1] == 'second_software_module':
import second_software_module
box = Box(1, 2, 3)
You could then call construction.py
with each import type from a shell script, say main.sh
.
main.sh
#! /bin/bash
python3 construction.py first_software_module
python3 construction.py second_software_module
Make the shell script executable using chmod +x main.sh
. Run it as ./main.sh
.
Alternatively, if you do not want to use a shell script, and want to do it in pure Python, you could do the following:
main.py
import subprocess
subprocess.run(['python3', 'construction.py', 'first_software_module'])
subprocess.run(['python3', 'construction.py', 'second_software_module'])
and run main.py
as you normally would using python3 main.py
.
-
Actually I need to run it from inside an other python script, handling path to the software, file saves, etc... Is it possible with that solution ?Gregoire– Gregoire2021年05月19日 13:58:58 +00:00Commented May 19, 2021 at 13:58
-
I've added how you can do it in Python. Does the modification work for you?Nikhil Kumar– Nikhil Kumar2021年05月19日 13:59:47 +00:00Commented May 19, 2021 at 13:59
-
You can pass a command-line argument that will tell your script which module to import. There are many ways to do this, but I'm going to demonstrate with the argparse
module
import argparse
parser = argparse.ArgumentParser(description='Run the construction')
parser.add_argument('--module', nargs=1, type=str, required=True, help='The module to use for the construction', choices=['module1', 'module2'])
args = parser.parse_args()
Now, args.module
will contain the contents of the argument you passed. Using this string and an if-elif
ladder (or the match-case
syntax in 3.10+) to import the correct module, and alias it as (let's say) driver
.
if args.module[0] == "module1":
import first_software_api as driver
print("Using first_software_api")
elif args.module[0] == "module2":
import second_software_api as driver
print("Using second_software_api")
Then, use driver
in your Box
class:
class Box(x,y,z):
def __init__(self):
driver.makeBox()
Say we had this in a file called construct.py
. Running python3 construct.py --help
gives:
usage: construct.py [-h] --module {module1,module2}
Run the construction
optional arguments:
-h, --help show this help message and exit
--module {module1,module2}
The module to use for the construction
Running python3 construct.py --module module1
gives:
Using first_software_api
construction
& let it import it & run it?