0

Contents of my project is like this

C:/{user_name}/my_project/
│  bar.sh
├─scripts/
│  │  bar.sh
│  │
│  └─bar/
│     bar.sh
│
└─src/
    foo.py

and Content of all of bar.sh is

python src/foo.py

so if I run from C:/{user_name}/my_project/ directory, bar.sh works.

bash bash.sh 
bash scripts/bar.sh 
bash scripts/bar/bar.sh

However, after moved to scripts directory, below command does not work because bar.sh tries to find /my_project/sciprts/src/foo.py

bash bar.sh

I understand we can add my_project directory to PYTHONPATH by editing window's envrionmental valiables to solve this. However, I would like to avoid this because {user_name} can be varied and this envrionmental is shared by many guys.

Therefore I am looking for a good way to add my_project directory to PYTHONPATH by adding same line(s) to bar.sh. Do you know such a magic command?

kaz0621
  • 155
  • 1
  • 11
  • 1
    Why not just modify the line of code to call `python ../src/foo.py`? – Woody1193 Apr 16 '22 at 01:56
  • 1
    If I want to get into the parent directory of my script in Bash, I usually use `cd "$(dirname "$0")/.."`. Dunno if that works on Windows, though. – Nick ODell Apr 16 '22 at 01:57
  • I don't think adding a new directory to the `PYTHONPATH` is the solution here. Instead, I would modify your `bar.sh` to add the `src` directory to `PATH` and then call `python foo.py` directly – Woody1193 Apr 16 '22 at 02:02
  • Thank you for your comments! Actual project is more complicated like import modules mutually, so I would like to resolve this just editting sh Nick's comment works for me! – kaz0621 Apr 16 '22 at 02:09
  • 1
    Windows has two levels of environment variables — User and System. Just define `PYTHONPATH` at a System level variable because that doesn't vary depending on the user name. Here's an [old answer](https://stackoverflow.com/a/4209102/355230) of mine on the topic (although exactly how to do things has changed). Here's [another](https://stackoverflow.com/questions/4208659/when-to-use-sys-path-append-and-when-modifying-pythonpath-is-enough/4209102#4209102). – martineau Apr 16 '22 at 02:15
  • You may be able to simply put the path in a .pth path configuration file in the right directory as described near the beginning of site module's [documentation](https://docs.python.org/3/library/site.html#module-site). This will add the path in the file to sys.path at every Python startup (it it exists) — so you wouldn't need to do anything in your bash script (although I suppose you could have it add the `.pth` file and remove it after running `src/foo.py`). – martineau Apr 16 '22 at 18:41

2 Answers2

1

Personally, I don't think modifying PYTHONPATH via bash script is a good solution here as you risk corrupting it. If you're interested in being able to call python foo.py directly, you should turn /src into a package. Not only will this allow you to execute foo.py from anywhere in your system, you will also be able to import the code therein into any Python code you write. Here's some documentation on modules and some more on creating packages.

Essentially, you create some files in you my_project directory to tell Python about the package itself as well as dependencies and such. Make sure that all the directories in the /src directory have an __init__.py file as well. Once you've done that, you can call pip insall -e my_project which will install it as a package on your system (make sure you have pip installed; it should have been when you installed Python, but if not you can get it here). After that, you can call your code from wherever.

Woody1193
  • 7,252
  • 5
  • 40
  • 90
0

cd "$(dirname "$0")/.." works for me!

kaz0621
  • 155
  • 1
  • 11