0

tl;dr: What I am looking for:

python3.5 --working-dir=~/company_repo/ ~/company_repo/something.py

In the codebase I am working on, there are some scripts that help with various tasks - let's call them company scripts. These are owned by the teams responsible for those features.

Next to the codebase, I have my own repo with my own helper scripts - let's call them my scripts. Now my scripts are just bash scripts, and call a sequence of the python company scripts:

myscript.sh
python3.5 ~/company_repo/scripts/helper1.py someargument
python3.5 ~/company_repo/scripts/helper2.py

Problem is, some of the company scripts rely on being run within the company repo, because they call git commands, or load other files by relative path. I cannot change the company scripts right now.

Is there a way to tell the python runtime to use different working directory? I do not want to do cd ~/company_repo in my bash scripts.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • Note that I did a lot of googling, but all results were showing how to set working dir from WITHIN a python script, whereas I want to set it from outside, without changing the script. – Tomáš Zato Aug 18 '22 at 23:38
  • Not the best thing we can do but I do something like `cd ~/company_repo/ && python3.5 executable.py` – Pranjal Doshi Aug 18 '22 at 23:48
  • @PranjalDoshi I disagree, that is a perfect suggestion. You should post it as an answer. Maybe make it to `(cd... & python... ) ` using the parantheses will not alter the current working dir of the bash script – FlyingTeller Aug 19 '22 at 00:34
  • You can wrap `cd... & python...` in a bash file and push it in one of folder in PATH, something like: `cd $1 && python3 $2`. If additional arguments for python is desired, maybe this can help: [Process all arguments except the first one](https://stackoverflow.com/questions/9057387/process-all-arguments-except-the-first-one-in-a-bash-script) – The Long Nguyen Aug 19 '22 at 02:24

4 Answers4

1

This is not a pythonic way but we can use the bash to mimic the same behavior.

you can try as suggested by @FlyingTeller

(cd company_repo && python3 helper.py)

or you can also use pushd & popd

pushd company_repo && python3 helper.py && popd
Pranjal Doshi
  • 862
  • 11
  • 29
1

You can try using env --chdir:

env --chdir=$dir python3.5 $dir/scripts/helper1.py someargument
FelipeC
  • 9,123
  • 4
  • 44
  • 38
1

Create a script like this one

#! /bin/bash
if cd company_repo
then
    exec /usr/local/bin/python3.5 "$@"
fi

name it ~/bin/python3.5, make it executable

chmod +x ~/bin/python3.5

be sure ~/bin is before the directory containing the real pythno3.5 in your PATH:

PATH=~/bin:$PATH

then when you run (that would remain unchanged):

python3.5 ~/company_repo/scripts/helper1.py someargument
python3.5 ~/company_repo/scripts/helper2.py

it will change the directory and then use the real python3.5 to execute it.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

You can define a function (called python3-5):

#!/usr/bin/env bash
  
python3-5()(
    cd "$(dirname "$1")"
    python3.5 "$@"
)

python3-5 ~/company_repo/scripts/helper1.py someargument
python3-5 ~/company_repo/scripts/helper2.py
Philippe
  • 20,025
  • 2
  • 23
  • 32