Forums

Run another .py file from current python script. Permission Denied.

I am trying to execute python file from another python file using os.system('/home/user/mydir/file.py') but it throws "Permission Denied". Any ideas how to come over this problem?

There are two options:

Specify that you want to use Python to run the process. That's easiest if you use the subprocess module:

import subprocess
...
subprocess.check_call(["python2.7", "/home/user/mydir/file.py"])

Alternatively, give the Python script execute permissions. From a bash console:

chmod +x /home/user/mydir/file.py

For this option, you'll need to make sure that file.py starts with a hashbang to specify that it's a Python file, ie.

#!/usr/bin/python3.4

One thing I should add -- it's pretty unusual to run another Python file by doing a system call. People would normally import the Python file as a module and then execute functions from it.

Thanks for quick respond! First option works, but it's unfortunately not what i wanted. I am making a mini service which implies automatically creating and running scripts with user's data, so I can't use one script for all of them and each must be autonomous and independent. Is it possible? Have a nice day, Art G.

I think the first option should do the job if that's what you want to do -- what problems do you foresee?

the problem is when I start running next subprocess, it's automatically killing the previous one.