Skip to content

How to create a lock file from installed packages (or from requirements.txt file) #13300

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

Open
czechnology opened this issue May 5, 2025 · 7 comments
Labels
question Asking for clarification or support

Comments

@czechnology
Copy link

Question

When I'm creating a lock file with uv lock, it will always lock the newest available versions. Is it possible to create a lock file which matches the versions installed in the venv? I have not been able to find a combination of options which would achieve this.
Alternatively, can I create a lock file from requirements.txt file?

I would need this for the process of migrating to uv: I would like to keep the pinned versions the same as before to keep the change minimal.

Platform

No response

Version

No response

@czechnology czechnology added the question Asking for clarification or support label May 5, 2025
@czechnology
Copy link
Author

In practice, for the first option I would install the packages based on the requirements.txt file and then lock.

rm -rf .venv
uv venv
uv pip install -r requirements.txt
uv lock --something?

@konstin
Copy link
Member

konstin commented May 5, 2025

You can use uv pip freeze to record the installed versions and use them as constraint dependencies for the first locking (you might need some manual editing for the transition from one environment to universal locking)

@czechnology
Copy link
Author

That is not what I'm going for - I already have the dependencies I want to keep available as requirements.txt files. I am trying to figure out how to make a uv.lock file with the same versions as those in the requirements files. One way I thought would be doable is to first install those versions into a virtualenv and then somehow create a uv.lock file which would reflect the installed versions instead of the newest ones available.

@charliermarsh
Copy link
Member

You can pass the requirements.txt as a constraint:

echo "anyio" > requirements.in
uv pip compile requirements.in -o requirements.txt
uv add -r requirements.in -c requirements.txt

@czechnology
Copy link
Author

Thanks, @charliermarsh, that is an interesting approach but gets me forward, thanks!

# Create a backup of pyproject.toml
cp pyproject.toml pyproject.backup

# Set the pinned requirements to be the constraints in pyproject.toml
uv add -r requirements.txt -c requirements.txt

# uv.lock is now created

# Now that we have a lock file, let's revert the original config file
mv pyproject.backup pyproject.toml

@zanieb
Copy link
Member

zanieb commented May 5, 2025

This is also covered in #12382, to an extent.

@czechnology
Copy link
Author

That will be a useful part of the docs, @zanieb, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking for clarification or support
Projects
None yet
Development

No branches or pull requests

4 participants