pythonbash

How to run python script with a specific working directory?


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.


Solution

  • 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