So I have this python thing that needs to process a file.
First it was:
my_project/
├── script.py
And I would simply run it with python script.py file.csv
.
Then it grew and became:
my_project/
├── script.py
├── util/
│ └── string_util.py
├── services/
│ └── my_service.py
(There is an empty __init__.py
in every directory)
But now my_service.py
would like to use string_util.py
and it's so damn not straightforward how to do this nicely.
I would like to do from ..util import string_util
in my_service.py
(which is imported into script.py
with from services import my_service
), but that does not work with python script.py
since my_service
's __name__
is then only services.my_service
(and I get the Attempted relative import beyond toplevel package
)
I can do
cd ..
andpython -m my_project.script
, but that seems so unnatural and would be really bad to put it in the README for the instructions how to run this.Right now I'm solving it with the ugly
sys.path.append()
hack.
What other options do I have?