#๐Ÿ”’ Unsure of how to approach project spread across multiple files.

29 messages ยท Page 1 of 1 (latest)

hidden lodge
#

I currently have a larger scale project than I normally attempt, in that I'm at the point where it is more convenient to split up my code base into multiple files.

I currently have my file system arrayed as such:

src/
  main.py
  tools/
    __init__.py
    widgets.py

I would like to add a new file about.py to the src directory such that:

src/
  about.py
  main.py
  tools/
    __init__.py
    widgets.py

And where about.py is accessable by both main.py and tools/__init__.py.

How would I best go about doing this, and how would I organise the entry point to the application?

Response from @velvet quartz:

Best to use a unique project name for your package
So you have
project-name/src/project_name/__init__.py
And
project-name/src/project_name/__main__.py
And then you can run it with python -m project_name
Then relative imports will work

@velvet quartz I'm sorry I didn't respond to this, I somehow forgot I made this post. I have a few more questions about this project. Some further help would be greatly appreciated!

  • I've been told elsewhere that it's bad practice to use relative imports from a parent directory, is that the case or should I still be fine?

  • And, were I to build this with PyInstaller or similar, how would I go about that considering the entry point is a directory?

winter valleyBOT
#

@hidden lodge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

languid smelt
#

didn't you repost this

velvet quartz
#

Relative imports are fine inside a package, they don't work outside a top level package

#

Which is why it's best to only have one top level package

hidden lodge
# languid smelt didn't you repost this

It didn't get answered the first time, the second someone did answer but I completely forgot I made the post, and I'm still a little unsure of a couple things.

hidden lodge
velvet quartz
#

Yeah

#

Eg you'd only have one directory and no files in src

#

And you'd use python -m project_name to run your package

#

Using the __main__.py as an entry

hidden lodge
#

ok

#

in which case the working directory would still be src/ or would it be src/project_name/? i.e. where would I need to put files that are read by __main__.py?

velvet quartz
#

Use importlib.resources

#

And put files somewhere under src/project_name

hidden lodge
velvet quartz
#

I'd also put an __init__.py in your assets folder

#

So you can do

import json
import importlib.resources
from . import assets

example = importlib.resources.files(assets) / "example.json"
with example.open('rb') as f:
    data= json.load(f)
hidden lodge
#

ok

#

I also just tested putting the assets folder in src/ as so:

src/
  assets/
    example.json
  project_name/
    __init__.py
    __main.py

and was able to read example.json from __main__.py. Is using importlib.resources preferable or did I just come across a fluke?

velvet quartz
#

You just fluked it

#

You should use importlib.resources

hidden lodge
#

Ok, and lastly, how would I go about building the project with something like pyinstaller?

velvet quartz
hidden lodge
#

Ok, thank you very much for giving me your time, after I didn't respond the first time!

#

!close

winter valleyBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.