0

I am trying to run 3 python programs simultaneously by running a single python program

I am using the following script in a separate python program sample.py

Sample.py:

import subprocess
subprocess.Popen(['AppFlatRent.py'])
subprocess.Popen(['AppForSale.py'])
subprocess.Popen(['LandForSale.py'])

All the three programs including python.py is in the same folder.

Error:  OSError: [Errno 2] No such file or directory

Can someone guide me how can i do it using subprocess.Popen method?

user3265370
  • 121
  • 1
  • 2
  • 12
  • What os you are running this script? – venpa Mar 25 '14 at 10:54
  • possible duplicate of [Running several programs from one program](http://stackoverflow.com/questions/22629397/running-several-programs-from-one-program) – bosnjak Mar 25 '14 at 11:11

3 Answers3

0

The file cannot be found because the current working directory has not been set properly. Use the argument cwd="/path/to/script" in Popen

Steve Rossiter
  • 2,624
  • 21
  • 29
0

It's because your script are not in the current directory when you execute sample.py. If you three script are in the same directory than sample.py, you could use :

import os
import subprocess
DIR = os.path.dirname(os.path.realpath(__file__))

def run(script):
    url = os.path.join(DIR, script)
    subprocess.Popen([url])

map(run, ['AppFlatRent.py','AppForSale.py', 'LandForSale.py'])

But honestly, if i was you i will do it using a bash script.

Luc DUZAN
  • 1,299
  • 10
  • 18
0

There might be shebang missing (#!..) in some of the scripts or executable permission is not set (chmod +x).

You could provide Python executable explicitly:

#!/usr/bin/env python
import inspect
import os
import sys
from subprocess import Popen

scripts = ['AppFlatRent.py', 'AppForSale.py', 'LandForSale.py']

def realpath(filename):
    dir = os.path.realpath(os.path.dirname(inspect.getsourcefile(realpath)))
    return os.path.join(dir, filename)

# start child processes
processes = [Popen([sys.executable or 'python', realpath(scriptname)])
             for scriptname in scripts]

# wait for processes to complete
for p in processes:
    p.wait() 

The above assumes that script names are given relative to the module.

Consider importing the modules and running corresponding functions concurently using threading, multiprocessing modules instead of running them as scripts directly.

jfs
  • 399,953
  • 195
  • 994
  • 1,670