Python import from other directories

Cześć, mam problem i nie wiem jak sobie elegancko z nim poradzić.
Mam w repo dwa katalogi z narzędziami, jeden zawiera większe narzędzie, drugi zbiór mniejszych. Chcę aby wszystkie skrypty korzystały ze wspólnej implementacji znajdującej się w katalogu common.
Użycie ..common.common_tool_1 nie działa. Dodanie root dira do PYTHONPATH pomaga, ale trochę śmierdzi hakiem. Znacie jakieś inne sposoby?

common/
├── common_tool_1.py
└── common_tool_2.py
large_tool/
├── main_script.py
├── component_1.py
├── component_2.py
└── component_3.py
additional_scripts/
├── script_1.py
├── script_2.py
└── script_3.py

This sounds familiar. I had the exact same problem some years ago

https://stackoverflow.com/questions/28580962/structure-python-project-with-multiple-entry-points-and-centralised-config-fil

Didn’t really get to a good answer then. Right now I think how I would do it (but I have to admit that I would need to actually build it to understand all the subtilties and if it makes sense) is make a single package of this whole thing.

You mean - no subdirectories containing reusable modules?

So like

mytool/
├── common/
   ├── common_tool_1.py
   └── common_tool_2.py
├──large_tool/
   ├── main_script.py
   ├── component_1.py
   ├── component_2.py
   └── component_3.py
└──additional_scripts/
   ├── script_1.py
   ├── script_2.py
   └── script_3.py
pyproject.toml
tests/

Then I’m pretty sure that in each tool you just import things like import mytool.common.common_tool_1

or something

But as I said, I would have to do some tests to be sure

If you figure it out, feel free to add it to that stackoverflow question :slightly_smiling_face:

I think I’ll check the solution with pyproject.toml, I wanted to avoid that because I already have 2 layers of packaging :grinning_face_with_smiling_eyes:

Although… now that I think of it.. Having the pyproject.toml will not auto-add the toplevel dir to PYTHONPATH…

It will work in production to be sure. But not entirely sure how you could get it to work during development…

I’m pretty sure that what I did back then was my solution nr 3. just add this to the top of each “executable”:

import os
import sys
sys.path.append(os.path.join(os.path.abspath(
    os.path.dirname(__file__)), ".."))

I figured I can add a small file containing the following

but this still has to be copied to each directory containing executables, and imported by each executable

To make it work in development you can just run pip install -e . in the root directory

Reactions: :+1: ×1 (Arek)

Użycie ..common.common_tool_1 nie działa
a masz w tym katalogu common __init__.py?

tak

Hehe yeah pathlib makes this easier. However it still needs an “abspath” somewhere if you want to be able to also run it from the directory itself (ie if file doesn’t have any slash in it)

Reactions: :+1: ×2 (Arek, RafaAutomation)

I’ll check that, but pathlib also contains a method for making path absolute

Reactions: pajton ×1 (RafaAutomation)