0

Say I have three projects A, B and C in my home directory, all written mainly in Python:

.
├── project_A
│   ├── data
│   ├── plot
│   ├── result
│   └── run.py
├── project_B
│   ├── data
│   ├── plot
│   ├── result
│   └── run.py
└── project_C
    ├── data
    ├── plot
    ├── result
    └── run.py

They all have a similar structures: data, result and plot are all directories that contain various scripts. My question is, if I were to export the paths for each of the projects into PYTHONPATH like:

export PYTHONPATH=${HOME}/project_A
export PYTHONPATH=${HOME}/project_B:${PYTHONPATH}
export PYTHONPATH=${HOME}/project_C:${PYTHONPATH}

then there will be conflicts/confusions, because all three projects have data, plot as well as result, when I do from data.script import function, I am not sure if am importing from project A, B or C.

Given that the three projects are independent, can I do the following:

export PYTHONPATH=${HOME}

and in project A, for example, I imagine that I can safely do:

from project_A.data.script import function

Will anything unexpected happen when I put my home directory into PYTHONPATH? Is there anything I need to be careful with? Is this a good practice at all? If not, what should I be doing instead? Thanks!

zyy
  • 1,271
  • 15
  • 25
  • 1
    No, good practice would be to create a python package and import your modules just like you would any other third party library without messing with the `PYTHONPATH` – Alexander Feb 25 '23 at 01:13
  • @Alexander Thanks, but I need to work on those projects, plus these projects also contain other types of files, like `.xlsx` and so on. – zyy Feb 25 '23 at 01:24
  • Perhaps I should make a directory called `projects` and put A, B and C inside it. Then in `.bashrc` I can do `export PYTHONPATH=${HOME}/projects`? – zyy Feb 25 '23 at 01:25
  • 1
    @zyy https://stackoverflow.com/a/35064498/152043 you can create a venv (virtual env) and then install each of your modules into it in editable mode, allowing you to edit them in place and for them to see the changes you make between them. editable mode basically slaps them into the pip package repo as a symlink back to wherever the package is. – Michael Speer Feb 25 '23 at 01:58
  • @MichaelSpeer I tried to use `pip install -e` with a dummy `setup.py`, but the installation failed. Seems like `pip` wants a more detailed instruction. So I end up making a directory `projects` in my home and export it to `PYTHONPATH`. – zyy Feb 25 '23 at 04:39

0 Answers0