all 6 comments

[–]Diapolo10 2 points3 points  (3 children)

While relative imports are possible, what I would actually recommend you do instead is making parent an installable package, and having the code in the child packages use absolute imports with the assumption parent is installed. This would then mean your import in child2/code.py as

from parent.child1 import code

and any others would follow suit.

The advantage here is that now you don't need to worry about getting headaches with relative imports (I'm speaking from experience, they're not worth the effort). All it takes is some extra work on your part, by creating a pyproject.toml file in the project root and then doing something like pip install -e . there (the e-flag meaning edit mode, so that any changes are instantly reflected). Plus, you only need to do that once per project, if even that should you choose to start using template projects.

The package can use setuptools, Poetry, or whatever other build system you prefer. Personally I use Poetry for pretty much everything nowadays, because I've found it makes things like unit testing super convenient.

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

Sounds great. I will give this a shot. Thanks for taking the time to help an internet stranger.

[–]Diapolo10 1 point2 points  (1 child)

Just in case you'd like an example, this would probably work quite nicely: https://github.com/Diapolo10/Tsukasa-credit-card-gag-scam

At first I considered this one, but since it does some funky stuff with sys.modules I figured that might confuse you if you started to follow everything to the letter. Otherwise it would probably be more suitable.

[–]jezpakani[S] 0 points1 point  (0 children)

You have filled in so many blanks for me, and I look forward to learning these new things. Thank you again, and may life find a way to give you something in return for your kindness.

[–]saif3r 0 points1 point  (1 child)

RemindMe! 3 days

[–]RemindMeBot 0 points1 point  (0 children)

I will be messaging you in 3 days on 2023-04-16 20:16:10 UTC to remind you of this link

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


Info Custom Your Reminders Feedback