#🔒 How to structure my python project

27 messages · Page 1 of 1 (latest)

surreal island
#

Hi everyone, it’s been a while since I last used python and I recently learned about uv, which I’m going to be using here.

I’m going to build a relatively large project, so I wan to make sure it’s properly structured.

For now, I’m thinking of putting the main code in a src and tools to run different parts of the code in a scripts folder.

Firstly, is this a good way to structure a python project? I like not having tons of the code mixed with README and other files in the root as that becomes a mess.

Secondly, I’m not sure how the imports work.

I’ve been told I need to run pip install —editable . And then import for example if I have src/cad/cube.py I can run in scripts/visualize_cube.py, from cad import cube (if I have a cube function in the cube file).

What I don’t get here is I’m not looking to create a library, so I don’t get why I have to run a pip install (and also I’m running uv so I shouldn’t be using pip).

How should I be setting everything up for imports if I use this kind of setup?

I’m looking for a clean way to organize my project.

Thanks a lot

cold belfryBOT
#

@surreal island

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.

tidal creek
#

have you read the official Python packaging documentation for this?

tired laurel
#

Sounds pretty similar to how I do it at our company. No scripts in root, have a src and scripts for entrypoints.

tidal creek
#

the interpreter has to find your code somehow, and an editable install is the most convenient way to achieve that, regardless of your current working directory

tired laurel
#

Src is just for the raw functions, no if name == main (also not for testing, that should be put in a separate tests folder)

#

And make a package in src you can install and later also import in other projects if they are more general tools

surreal island
surreal island
surreal island
tired laurel
#

There is no 1 standard, look at any big project, lots of differences

#

Its how ive found for myself works well

tidal creek
#

it's just the most convenient way to do everything

surreal island
#

Do you know of any big projects that aren’t libraries that I can check out? Because I checked libraries but I don’t know any non library python projects off the top of my head

tidal creek
#

rather than having one workflow for apps and another one for libraries

surreal island
#

Oh and one last thing later on I might have to end up using some c code as python extensions for high performance stuff. Here I’ll have to use a build backend right? What’s the best for this? Is setuptools still the best?

tidal creek
#

I like to use setuptools for that, although I'm moving from C to Rust now

surreal island
tidal creek
#

the master branch still has the C extension so you may find the comparison enlightening

#

if you do ever add an extension (regardless of the programming language), you'll need the project to be installable, and if you follow the standard workflow, you won't have to make radical changes to your project setup then

surreal island
#

Perfect so basically uv then setuptools as backend if I want c and yeah

#

And thanks a lot for sending the GitHub it helps a lot

cold belfryBOT
#
Python help channel closed for inactivity

This help channel has been closed. 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.