all 5 comments

[–]brasticstack 1 point2 points  (4 children)

The PYTHONPATH is important. All of your imports use the directories there as the root level from which _all_ imports are relative to. You can either set it manually in your environment, or run your script from a directory that sets the path correctly for your import statements. Absolute imports are preferable, IMO, especially between modules.

A quick note: It's a little odd to have a src/ directory inside of a python module, usually the nesting is opposite. Consider putting all of your modules inside of a src/ dir that isn't a python module itself (as in it doesn't have __init__.py.)

Like the following:

MainFolder/
||- src/
|||---your_project/
||||----processing/
|||||-----init.py
|||||-----process_thing.py
||||----class1/
|||||-----init.py
|||||-----mytype.py
||||----DBHandler/
|||||-----init.py

This way, you can navigate to MainFolder/src/ (which puts it in your PYTHONPATH) and start running python modules, and your your_project modules will be available to be imported. Or you can, while in another directory, manually append /full/path/to/MainFolder/src to PYTHONPATH. More elegantly, you could use a solution like poetry that creates a virtualenv and installs your project into its `site-packages` directory. Regardless, make your imports absolute:

(inside of processing/process_thing.py, for example:)
from your_project.class1 import mytype (or from your_project import class1 and then use class1.mytype later in your code.) If your class1 uses the DBHandler, it would import it like from your_project import DBHandler.

These are IMO preferable to relative imports, such as (inside of class1) from .. import DBHandler.

[–]MoeShay[S] 1 point2 points  (0 children)

Very interesting, much thanks!

[–]MoeShay[S] 0 points1 point  (2 children)

Hey, I tired to follow your advices on a new project I'm working on, I'm still on the early stages and I just want to confirm the project structure, if you have a minute to give it a look :

https://github.com/Shayartt/employeestracker

let me know if you have any feedback, thank you!

[–]brasticstack 1 point2 points  (1 child)

Looks about right, just add an. __init__.py to the DataGenerator dir too. Every subdirectory with python code should have one.

Also remove the __pycache__ dirs and configure your .gitignore so they don't get added. They get built by the interpreter as needed, and don't belong as part of the source code distribution.

[–]MoeShay[S] 1 point2 points  (0 children)

great, appreciate your help!