2

tl;dr: looking for:

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

There are company Python scripts owned by the teams responsible for them; my bash scripts call a sequence of them:

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

Some company scripts rely on being run within the company repo, because they call git commands, or load files by relative path. I cannot change the company scripts.

Are there ways to tell Python runtimes to use different working directories? I do not want to do cd ~/company_repo in my bash scripts.

4
  • 1
    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. Commented Aug 18, 2022 at 23:38
  • Not the best thing we can do but I do something like cd ~/company_repo/ && python3.5 executable.py Commented Aug 18, 2022 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 Commented Aug 19, 2022 at 0: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 Commented Aug 19, 2022 at 2:24

4 Answers 4

5

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
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, the second is what I am using after I failed to find a command line argument/env variable to do that.
1

You can try using env --chdir:

env --chdir=$dir python3.5 $dir/scripts/helper1.py someargument

Comments

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.

1 Comment

Upvote for the out of the box approach, but I used the pushd solution in the end.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.