0

I am developing a project (project-1) that depends on another project (project-2). I am doing constant modification on both, so I don't feel comfortable to install the project-2 to import it. (except if there is a way to easily work on an install library, but even then, I like my work to be self contained since I often deploy it on remote servers using Pycharm)

very simple overview:

project-1/
└── LOT of stuff there

project-2/
└── project_2/
    ├── __init__.py
    ├── config.py
    ├── main.py
    └── also LOT of stuff there

currently I have a file called config.py in my project-2

import sys
from pathlib import Path

# project_1
project_1_path = Path(__file__).parents[2].joinpath('project_1')
project_1_databases_folder = project_1_path.joinpath('databases')
sys.path.insert(0, str(project_1_path))
print('project_2 set in system path')

# project_2
path_project_2 = Path(__file__).parent.parent
path_models_folder = path_project_2.joinpath('models')
path_datasets_folder = path_project_2.joinpath('datasets')
path_experiments_folder = path_project_2.joinpath('experiments')

When I launch the main of my project_2, I do import config as c. So the path is set and I can use the different path constants.

I was wondering if it is a "good" way and what should I do to improve it. I was thinking of creating a configure function in the config file and execute it in the __init__.py of the package. Not sure which solution is the best

def configure():
    sys.path.insert(0, str(project_1_path))

Any feedback would be very much appreciated! thanks

th0mash
  • 185
  • 3
  • 13
  • 1
    this question seems to better suit Code Review since I assume You have no errors – Matiiss Apr 21 '21 at 16:47
  • 1
    You can always install `project-2` in editable mode - chek https://stackoverflow.com/q/35064426/4046632 – buran Apr 21 '21 at 16:52
  • 1
    "I don't feel comfortable to install the project-2 to import it" - why not? I would just do `pip install -e .` from project-2 and then you can use it from project-1 but any changes to the code in project-2 will work. In general, I think messing around with paths manually is almost never the right option; it makes things very brittle (e.g. what if you decide to move project-2 to another folder? Now things are broken) – Nathan Apr 21 '21 at 16:52
  • @Matiiss oh right, I forgot about this stackexchange – th0mash Apr 21 '21 at 17:23
  • @buran ohh thank you, I did try to search a little bit and couldn't find this information, I'll look into it – th0mash Apr 21 '21 at 17:24
  • @Nathan it is for my researches, and I didn't feel comfortable enough yet to mess around with packages on important projects, but it seems actually simpler. I didn't know the package would stay in the same directory etc.. Thanks! I'll certainly do that – th0mash Apr 21 '21 at 17:26

0 Answers0