william_b
Posts: 3
Joined: Sat Nov 29, 2014 4:23 pm

[Solved] Run python script in another python script

Sat Nov 29, 2014 6:08 pm

Hello everyone,
I am new on the forum.
Two weeks ago I bought my rapsberry pi. So i'm still a beginner.

I have two python scripts:
  • sendmail.py
    7segment+buttons.py
Running the following code, results in sending an email to myself.

Code: Select all

python sendmail.py 'subject of the mail' 'body of the mail
I do want to run sendmail.py from within 7segment+buttons.py and pass some arguments. This is were I get stuck.

I have implemented it as follows:

Code: Select all

import os
try:
...
   while True:
   ...
   # button pressed
      index+=1
      os.system('python sendmail.py "update" index'
      #increase number on 7-segment display)
      ...
The problem:
  • Index is recognised as a string (not as a variable)
    Increase number on 7-segment display is delayed (it waits until the mail is send)
After some googling, I did find that I could also use subprocess.Popen. I tried it, but didn't get it to work.

Can someone help me, or give me a tip how to solve this problem.
Last edited by william_b on Sun Nov 30, 2014 8:00 am, edited 1 time in total.

User avatar
paddyg
Posts: 2441
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Run python script in another python script

Sun Nov 30, 2014 12:09 am

Yes you should use subprocess.Popen with a list of each word in the command you want to execute ie ['python', 'sendmail.py', 'subject of email' etc..]

if you want to include a variable in a string you need to use format() ie
'python sendmail.py "update" {}'.format(index)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

william_b
Posts: 3
Joined: Sat Nov 29, 2014 4:23 pm

Re: Run python script in another python script

Sun Nov 30, 2014 8:00 am

Thank you, this worked perfectly.

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

Re: [Solved] Run python script in another python script

Sun Nov 30, 2014 12:42 pm

For what it's worth, it's actually possible to do without string formatting too e.g.:

Code: Select all

subject="This is the subject of my email"
body="This is the body of my email"
subprocess.Popen(["python", "sendmail.py", subject, body])
This should work as python will treat each item in the list as a separate parameter.

Depending on how sendmail is structured you could also import it into your own script and then call the functions directly rather than having to trigger a separate python process.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

ranjithkalluri
Posts: 1
Joined: Thu Nov 16, 2017 10:31 pm

Re: Run python script in another python script

Thu Nov 16, 2017 10:36 pm

I have same issue, I tried using os.system('python abc.py'), it appears that the program is running until I interrupt with keyboard.
But abc.py program should read from sensor and create a csv file. It runs perfectly when run separately but doesn't work while called from another script.

I have tried using shebang line interpreter ( #!) inside abc.py file, but still doesn't work

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

Re: [Solved] Run python script in another python script

Fri Nov 17, 2017 7:35 am

Can you post your scripts?
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”