cassidylaidlaw / python-boilerplate

4 stars 1 forks source link

Different Python versions + don't use requirements.txt #1

Open orrp opened 7 months ago

orrp commented 7 months ago

Useful and awesome repo, thanks! I just used it to start a new codebase and I have two quick suggestions.

  1. Add a remark in the README on how to adapt it for other Python versions. I'm using Python 3.11, so I had to change line 2 in mypy.ini and line 3 in pyproject.toml.
  2. Nice-to-have: requirements.txt is for listing package versions for a particular deployment, not for distributing a package. For package distribution (i.e., installing the package on an unspecified machine), we should probably use setuptools. This is usually done via a setup.py file, but since we are already using a pyproject.toml we can simply update it. Here's how we could do this in the context of this repo:
# pyproject.toml
# This part replaces setup.py (either way, we should not rely on requirements.txt)
[build-system]
requires = ["setuptools"]

[project]
name = "my_package"
version = "0.0.1"
description = "My Package"
authors = [
     {name = "Orr Paradise", email = "orrp@eecs.berkeley.edu"},
]
dynamic = ["readme"]
dependencies = [
    "torch",
    "ray",
]

[tool.setuptools.dynamic]
readme = {file=["README.md"], content-type="text/markdown"}

# Remainder boilerplate
[tool.black]
line-length = 88
target-version = ['py311']
include = '\.pyi?$'
exclude = ''

[tool.pytest.ini_options]
timeout = 60
testpaths = ["tests"]
markers = []

[tool.isort]
profile = "black"
known_first_party = ["my_package"]
known_third_party = ["ray", "torch"]
orrp commented 7 months ago

If these additions aren't useful for your use cases feel free to close this issue and I'll just keep these changes in my own fork, no worries! Thanks once again :)

cassidylaidlaw commented 6 months ago

Sorry for not responding to this sooner! These look like good changes. I do usually eventually end up putting a setup.py in place when I'm ready to publish a project but it's nice to have it from the beginning. Do you recommend not using requirements.txt and doing pip install -e . or something?

orrp commented 6 months ago

No worries!

I would use the existing pyproject.toml, which supersedes setup.py (see the dependencies line in the sample above). I never have requirements.txt in my projects, unless I explicitly need a flow for quickly setting up a package on, say, a specific AWS configuration.

cassidylaidlaw commented 6 months ago

Makes sense! How do you install dependencies locally during development? And do you have thoughts on where to put development-only dependencies like black, mypy, etc.?

orrp commented 6 months ago

I use pip install -e .. Sometimes I end up having to use a src layout, which is a tad annoying but I tolerate it.

ATM I use your boilerplate and I just install your dev-dependencies via pip install -r requirements_dev.txt 😅. Seems like a better solution would be to specify them via extra_requires= and then use pip install -e .[dev] (StackOverflow).

IDK how to do this in pyproject.toml (rather than setup.py, but some googling leads me to believe one it'll be something like:

[project.optional-dependencies]
dev = [
    "black",
    "flake8",
    # etc.
]

I'm in the middle of some other stuff rn so can't try it out, lmk if it works! Or I can try it later.

cassidylaidlaw commented 6 months ago

Ok, I moved requirements to pyproject.toml and added a section about changing the Python version. Thanks for the ideas!