-1

I have a Python file called data.py that looks like this:

import json 

def txtToJson(textfile):
    #rest of the code

def jsontToTxt(jsonfile):
    #rest of the code 

I am also given a run.sh file to call each function from the Python file. I have tried doing this:

#!/bin/bash
if [ "$1" = '-s' ]
then
    if [ "$2" = '-j' ]
    then
        ##Serialize JSON
        python -c "import data; print(data.txtToJson($3))"
    fi    

elif [ "$1" = '-d' ]
then
    if [ "$2" = '-j' ]
    then
        ##Deserialize JSON
        python -c "import data; print(data.jsonToText($3))"
    fi
fi

However when I try running the script I get a ModuleNotFoundError: No module named 'data'. How can I get the run.sh to call the different Python functions properly?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
encrypt
  • 19
  • 4
  • 5
    Where is `data.py` located relative to `run.sh`, and relative to the directory from which you call `run.sh`? – mkrieger1 Feb 29 '20 at 20:25
  • 1
    use argparse module to define command line arguments (flags) for your script and in your bash script just write down something like `python data.py --file=/path/to/file --text_to_json` – PYPL Feb 29 '20 at 20:26
  • @mkrieger1 run.sh and data.py are in the same directory – encrypt Feb 29 '20 at 20:28
  • And what about the second part of my question? – mkrieger1 Feb 29 '20 at 20:32
  • But what's the current directory when you run `run.sh`? Do you run it like `./run.sh` or e.g. `./some-dir/run.sh`? – steinar Feb 29 '20 at 20:33
  • @steinar I run it like ./run.sh. – encrypt Feb 29 '20 at 20:36
  • @mkrieger1 I dont really understand your second part. I have a folder and in that folder both data.py and run.sh are saved there – encrypt Feb 29 '20 at 20:38
  • The second part of the question was: where is `data.py` located relative to the directory from which you call `run.sh` - in other words: from which directory do you call `run.sh`? Apparently it is in the same directory, so if you run `ls` you should see both `data.py` and `run.sh`, correct? – mkrieger1 Feb 29 '20 at 20:41
  • Could you change your `run.sh` script temporarily to only run the following: `ls ; python -c "import sys; print(sys.path)"`, and give us the output of running `run.sh` in exactly the same way as you have before? – steinar Feb 29 '20 at 20:45
  • @mkrieger1 yes that is correct – encrypt Feb 29 '20 at 20:45
  • Are there any other files or subdirectories? – mkrieger1 Feb 29 '20 at 20:50
  • @steinar that gives `['', '/Users/encrypt/anaconda3/lib/python37.zip', '/Users/encrypt/anaconda3/lib/python3.7', '/Users/encrypt/anaconda3/lib/python3.7/lib-dynload', '/Users/encrypt/anaconda3/lib/python3.7/site-packages', '/Users/encrypt/anaconda3/lib/python3.7/site-packages/aeosa', '/Users/encrypt/anaconda3/lib/python3.7/site-packages/Sentinelone-0.1-py3.7.egg']` – encrypt Feb 29 '20 at 20:50
  • @mkrieger1 yes there are a few other files but they dont pertain to the `data.py` or `run.sh` files at all. The other sub directories are created from Pycharm such as `__pycache__`, `bin`, `include` and `lib` – encrypt Feb 29 '20 at 20:53

1 Answers1

1

Given the information you've provided, I think you must be running from another directory than where these files are, i.e. that the current directory is different than the one containing run.sh. If that's correct, adding the following at the top of your script should help:

#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd $DIR

...rest of bash file...

This will change the current directory to the directory where the bash file resides.

steinar
  • 9,383
  • 1
  • 23
  • 37
  • More can be read about this (rather unreadable) piece of bash code here: https://stackoverflow.com/a/246128/282024 – steinar Feb 29 '20 at 21:14