2

I have a folder called TEST with inside :

  • script.py
  • script.sh

The bash file is :

#!/bin/bash

# Run the python script
python script.py

If I run the bash file like this :

./TEST/script.sh

I have the following error :

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

How could I do, to tell my script.sh to look in the directory (which may change) and to allow me to run it for inside the TEST directory ?

Tricky, my python file run a sqlite database and I have the same problem when calling the script from outside the folder, it didn't look inside the folder to find the database!

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • seems to me this doesn't really have anything to do with `sqlite`, in fact, it has nearly nothing to do with `python`. Really this is a bash and unix system question – erik258 Nov 02 '18 at 20:00

3 Answers3

1

You could use $0 which is the name of the currently executing program, as invoked, combined with dirname which provides the directory component of a file path, to determine the path (absolute or relative) that the shell script was invoked under. Then, you can apply it to the python invocation.

This example worked for me:

$ t/t.sh
Hello, world!    
$ cat t/t.sh
#!/bin/bash
python "$(dirname $0)/t.py"

Take it a step farther and change your current working directory which will also be inherited by python, thus helping it to find its database:

$ t/t.sh; cat t/t.sh ; cat t/t.py ; cat t/message.txt
hello, world!

#!/bin/bash
cd "$(dirname $0)"
python t.py
with(open('message.txt')) as msgf:
  print(msgf.read())
hello, world!
erik258
  • 14,701
  • 2
  • 25
  • 31
1

Alternative

You are able to run the script directly by adding this line to the top of your python file:

#!/usr/bin/env python

and then making the file executable:

$ chmod +x script.py

With this, you can run the script directly with ./TEST/script.py

What you asked for specifically

This works to get the path of the script, and then pass that to python.

#!/bin/sh

SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
python "$SCRIPTPATH/script.py"

Also potentially useful:

You mentioned having this problem with accessing a sqlite DB in the same folder, if you are running this from a script to solve this problem, it will not work. I imagine this question may be of use to you for that problem: How do I get the path of a the Python script I am running in?

Marcus
  • 3,216
  • 2
  • 22
  • 22
0

From the shell script, you can always find your current directory: Getting the source directory of a Bash script from within. While the accepted answer to this question provide a very comprehensive and robust solution, your relatively simple case only really needs something like

#!/bin/bash
dir="$(dirname "${BASH_SOURCE[0]}")"
# Run the python script
python "$(dir)"/script.py

Another way to do it would be to change the directory from which you run the script:

#!/bin/bash
dir="$(dirname "${BASH_SOURCE[0]}")"
# Run the python script
(cd "$dir"; python script.py)

The parentheses ((...)) around cd and python create a subprocess, so that the directory does not change for the rest of your bash script. This may not be necessary if you don't do anything else in the bash portion, but is still useful to have if you ever decide to say source your script instead of running it as a subprocess.

If you do not change the directory in bash, you can do it in Python using a combination of sys.argv\[0\], os.path.dirname and os.chdir:

import sys
import os

...

os.chdir(os.path.dirname(sys.argv[0]))
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264