-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Migrate to uv
as package manager for the generated project
#5434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
bd785eb
to
6bae8e7
Compare
Ready for testing 🎉 cookiecutter https://github.com/foarsitter/cookiecutter-django --checkout uv-generated-project |
8f3ca43
to
d12c2e7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did a first pass and noted a few things. Haven't reviewed the Dockerfiles yet
Hi, there. I played around with using your fork to update some existing projects and eventually got them working but ended up reverting them to their production (pip) versions so that I could revisit when I had more time. When working on an adapting an existing project, I got an error during the build, which was reproduced when I built a project from scratch using
I believe that it's caused by having the blank |
@steveputman thanks looking into this! During a build you cannot write to the host system. Your interpreter is inside the container together with uv so you can run uv:
Struggled a lot with this and it is not ideal. So hopefully someone has a better solution. |
@foarsitter Your solution is better than mine (I had uv installed on my host system so just ran Thanks for the help! Will let you know if anything else comes up. |
{{cookiecutter.project_slug}}/compose/production/django/Dockerfile
Outdated
Show resolved
Hide resolved
I think you need to run Of course we're also not sharing a virtualenv here, by my read/attempt to use this. It might be possible to just share a mount across all the services, but I'm having trouble getting that to work. |
Great work! This project really need to have a better tool than the plain pip |
The dependabot would need updating too. I dont know if it supports pyproject declarations |
I believe it does, I've found some discussions on the project, here's the link to the issue. |
This has been added now: dependabot/dependabot-core#10478 (comment) |
a git pull away from a merge 😿 |
Tried it. I wonder if the requirements currently in [project.optional-dependencies]
production = [
"gunicorn==23.0.0",
# ...
] Currently they appear in the main project dependencies, so they will be installed in the development virtualenv. They can then be installed by running uv docs about optional dependencies. (And thanks for doing the work - I look forward to it being merged :) ) |
@foobacca I see development as a superset of production. Everything from production needs to be replicated in development but production doesn't need a test framework for example. When the sentry_sdk is added as an extra dependency I cannot use capture_exception for example because the package is missing. Another example is testing the gunicorn command. I cannot do such thing when developing when I do not have the package. |
@foarsitter I see your point (though you can of course install the production dependencies if you want with I don't have a strong preference, but it does seem like a change from the master branch, where there is a separate I'll leave it to the maintainers. If they're happy with the change I have no objection. (And I'm quite capable of changing my project after running cookiecutter anyway 🙂 ) |
One other idea. I would expect the I can obviously edit it myself after running cookiecutter, so not a big deal if others prefer it at the bottom, but thought I'd mention it. |
The production deps are installed locally on the master branch too, so not so much of change - see #4838 for more context |
I agree. Dependencies should be at the top of the file |
We use https://github.com/tox-dev/pyproject-fmt for dictating the order of appearance, so you don't need to worry about it :) |
0de26a5
to
0f678fb
Compare
445bca4
to
c8c511e
Compare
2e8e2c5
to
9dba811
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think using bash shellscript in post gen probably won't work on windows unless it's running in a bash shell in WSL (
It maybe might be better to do it in pure python, although it's much more verbose.
I don't have Windows available at the moment, so I don't know if this works.
#!/usr/bin/env python3
import os
import subprocess
import shutil
import sys
def setup_dependencies():
print("Installing python dependencies using uv...")
{% if cookiecutter.use_docker == "y" %}
# Build the Docker service using Docker Compose
try:
subprocess.run(
["docker", "compose", "-f", "docker-compose.local.yml", "build", "django"],
check=True
)
except subprocess.CalledProcessError as e:
print(f"Error building Docker service: {e}", file=sys.stderr)
sys.exit(1)
# Use Docker to run the uv command
uv_cmd = ["docker", "compose", "-f", "docker-compose.local.yml", "run", "--rm", "django", "uv"]
{% else %}
# Use uv command directly
uv_cmd = ["uv"]
{% endif %}
# Install production dependencies
try:
subprocess.run(
uv_cmd + ["add", "-r", "requirements/production.txt"],
check=True
)
except subprocess.CalledProcessError as e:
print(f"Error installing production dependencies: {e}", file=sys.stderr)
sys.exit(1)
# Install local (development) dependencies
try:
subprocess.run(
uv_cmd + ["add", "--dev", "-r", "requirements/local.txt"],
check=True
)
except subprocess.CalledProcessError as e:
print(f"Error installing local dependencies: {e}", file=sys.stderr)
sys.exit(1)
# Remove the requirements directory
if os.path.exists("requirements"):
try:
shutil.rmtree("requirements")
except Exception as e:
print(f"Error removing 'requirements' folder: {e}", file=sys.stderr)
sys.exit(1)
print("Setup complete!")
if __name__ == "__main__":
setup_dependencies()
According to the cookiecutter docs .bat files are supported too, should that be a solution @luzfcb otherwise we should go with your version. |
bf4c149
to
498c323
Compare
@luzfcb adopted your version because the .bat file didn't work out of the box. I think we are ready! |
498c323
to
93bec37
Compare
@foarsitter I am trying to test the branch for an existing project. I created a blank project with the branch and then tried to merged the differences with mine. I am not sure I completely understood what has happened to package requirements. I copied the packages I had added to local.txt and base.txt to pyproject.toml, but it still gives a ModuleNotFound error. Where should the list of needed packages be when using uv? Thanks! |
Are you using docker? How did you add the requirements? Wich module cannot be found? |
Yes, using docker. I had added them to base.txt (they worked fine with pip) and when I saw the error I then added them to pyproject.toml too to no avail. The error is for django-money, but I have added all of these: Error is as follows: ModuleNotFoundError: No module named 'djmoney' (on the django container) |
You can add all your dependencies with uv as we do in the post gen hooks: https://github.com/cookiecutter/cookiecutter-django/pull/5434/files#diff-8601710441b0dde909812d3f5a1d83245fec1f1bf94b6e510d1b72cbd2fb86c1R525 First you need to remove the extending ( Then you need to execute the following commands: # project dependencies
docker compose -f docker-compose.local.yml add -r ./requirements/base.txt
docker compose -f docker-compose.local.yml add -r ./requirements/production.txt
# dev dependency group
docker compose -f docker-compose.local.yml add -r --dev ./requirements/production.txt This will modify you I think you didn't rebuild the image, and therefore, missing |
I get an error when trying to execute those commands (unknown docker command: "compose add") Anyway, I had manually added the dependencies (including django-money) to pyproject.toml and did a build I even tried running I might have missed merging something when bringing code over from the empty project I generated using this branch🤷🏻♂️ |
There is a mistake in the commands, it has to be Do you have a .venv in your directory? If you have files in the directory it will override your .venv in your container. Did you update the compose file according to this PR? |
Cool, I managed to run uv add Error is still there. I didn't have a .venv in my directory. django-money is inside the generated I merged all of the changes in django's Dockerfile. I didn't find any particular changes in the actual compose file. |
Do you have the following volumes listed in volumes:
- /app/.venv
- .:/app:z @acyment made some changes to the Dockerfile, can you try these? |
uv
as package manager for the generated project
Error still there. Made sure .venv is not there. Here are my local Dockerfile and compose files, in case you can spot what might be at fault. Thanks so much! Github won't let me attach a Dockerfile or a compose file, so pasting them here:
local.yml (I haven't renamed the compose files yet)
|
Your files seems fine, can you try these two things?
|
Just to clarify, when I need to add a new package dependency, how should I proceed from now on? My previous workflow was editing the corresponding requirements.txt file (base.txt / local.txt / production.txt) and then rebuilding the django image. Should I manually add it to pyproject.toml or rather run |
The project itself now uses
uv
and this is an attempt to bringuv
to the generated project too.uv run
for local development. In Docker the .venv python interpreter is added to the $PATH so packages can be used system wide.uv sync
is executed.pre-commit
that compiles a requirements.txttrap
totest_docker.sh
to cleanup after testing so the next time will no fail due to an existing postgres volume.Before we can merge this we need one last round to update all the versions to the latest ones in the requirement.txt files.