mattyjb88
Posts: 1
Joined: Fri Dec 22, 2017 12:18 am

Running python script within a python script

Fri Dec 22, 2017 12:32 am

Hi
I am as new as can be to python.
I am trying to run one python script from within another.

The first script is drcontrol.py wich is a basic script for operating a USB 8 relay board.
An example of using this from the command line would be:

Code: Select all

python drcontrol.py -d DAE003zi -r 1 -c on
the -d term is the serial number of the board
the -r term is the relay which is to be controlled
the -c term is the control action

So the above would turn relay 1 on.

The script works quite happily when run as above.
In order to interface the board with Cayenne, I am writing a parent script to run drcontrol.py. I'll call this parent script ircontrol.py.
All I want it to do is run the same as above, changing the -r and -c term depending on the Cayenne interface input.

Most of that I can figure out, however I am stuck at getting the ircontrol.py script to run the drcontrol.py script with the all the additional arguments (if this is the correct term for the -d, -r and -c terms?).

Can anyone give me a starter on this?

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Running python script within a python script

Fri Dec 22, 2017 2:03 pm

Well you could use subprocess:

Code: Select all

import subprocess
subprocess.call(["python", "drcontrol.py", "-d", "DAE003zi",  "-r",  "1",  "-c",  "on"])
However, it may be neater just to import drcontrol into your ircontrol.py script and then call the relevant functions from your script.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”