0

I am trying to call a shell script from a Python program which in turn runs a Python program.So i created one file called run.sh have following lines of code,

source venv/bin/activate
python main.py

The above script will be called by this code,

from pathlib import Path
import os
import subprocess

class Handler:

    ENTRYPOINT = 'run.sh'

    def run(self, project_path):
        if os.path.exists(project_path):
            subprocess.call(['bash', project_path+self.ENTRYPOINT])
        else:
            print('Project path does not exist!')

So, whenever the above run method is executed It throws this error

python: can't open file 'main.py': [Errno 2] No such file or directory

The run.sh file & main.py file resides in the same level. I don't know much about shell scripting & just Googling my way around but I can't find the reason for the above error.Not even sure if it has anything to do with that shell script.Any help would be highly appreciated.

Ropali Munshi
  • 2,757
  • 4
  • 22
  • 45
  • The location of the `run.sh` file is not relevant for your problem. Do in your script a `ls -ld $PWD/*.py` right before the Python call, to see what Python files you have in this directory. – user1934428 May 20 '21 at 07:14

1 Answers1

1

I guess you are calling run.sh from another directory than the directory the script resides on ? If so, this would explain it all. You are using a relative path, which is resolved depending on the current working directory (pwd).

To make the script work from anywhere, have a look at How can I set the current working directory to the directory of the script in Bash?.

On linux:

cd "$(dirname "$0")" # cd into the directoy where the run.sh is
python main.py
Derlin
  • 9,572
  • 2
  • 32
  • 53