Hacker News new | ask | show | jobs
by frogcoder 1082 days ago
I've been organizing my python projects starting with package folders.

Say I name my package "foo", foo is the top level folder for the source code. Inside foo there is a "main.py" file as the project's start point, and other various modules, let's have one called "module1.py". Now, add another package under "foo", the obvious example name here would be "bar", and add another module under "bar" called "module2.py".

The project structure looks like this:

  - foo/
    |- main.py
    |- module1.py
    |- bar/
       |- module2.py
To reference "module1.py" in "module2.py", just write

  import foo.module1
To reference "module2.py" in "main.py", do as follows

  import foo.bar.module2

There is no problem importing from any level.

To start the program from command line, enter:

python -m foo.main

1 comments

anecdotally I can support that `python -m x.py` is a better way to go than `python x.py`. Somewhere I found the tip that writing `python3.9 -m pip install $pkg` is a more reliable way to run `pip` for a specific version.

It should not be so complicated.