Was ist eine sehr * einfache * Möglichkeit, ein Python-Projekt zu strukturieren?

So habe ich diese Python-Sache, die eine Datei verarbeiten muss.

Zunächst war es:

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

What other options do I have?

Antworten auf die Frage(2)

Ihre Antwort auf die Frage