How to freeze a requirement with pipenv?

72.3k Views Asked by At

For example we have some pipfile (below) and I'd like to freeze the django version. We don't have a requirements.txt and we only use pipenv. How can I freeze the django version?

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
django = "*"

[dev-packages]
black = "*"

[requires]
python_version = "3.6"
11

There are 11 best solutions below

1
On BEST ANSWER

Pipenv do natively implement freezing requirements.txt. It is as simple as:

pipenv lock -r > requirements.txt
0
On

first, you ensure that your virtual environment is active then you open the terminal and run the command pip3 freeze > reqirements.txt (pip3) pip3 freeze > reqirements.txt (pip3)

1
On
pipenv run python -m pip freeze > requirements.txt
3
On

It's as simple as changing django = "*" to django = "your-preferred-version". So if you wanted to freeze it to 2.1, the latest release at the time of this writing, you could do this:

[packages]
django="2.1"

The pipfile Git repo has some good examples of different ways to specify version strings: https://github.com/pypa/pipfile#pipfile

Note that when you generate a lockfile from your pipfile, that lockfile is actually the file that's supposed to "freeze" your dependency to a specific version. That way, you don't have to concern yourself with which version works with your code, since by distributing the lockfile everyone else must use the same dependency versions as you. The developers of pipenv intended for developers to use it like this

1
On

Assuming you have your virtual environment activated, you have three simple approaches. I will list them from less verbose to more verbose.

pip

$ pip freeze > requirements.txt

pip3

$ pip3 freeze > requirements.txt

If a virtual environment is active, pip is most certainly equivalent to pip3.

pipenv run

$ pipenv run pip freeze > requirements.txt
$ pipenv run pip3 freeze > requirements.txt

pipenv run spawns a command installed into the virtual environment, so these commands are equivalent to the ones run without pipenv run. Once again, it is assumed that your virtual environment is active.

1
On

By using run You can run given command from virtualenv, with any arguments forwarded

$ pipenv run pip freeze  > requirements.txt 
2
On

As of v2022.8.13 of pipenv, the "old" lock -r functionality has been removed.

Going forward, this should be accomplished with:

pipenv requirements > requirements.txt
1
On

This is the way that I was prompted by pipenv to generate a requirements.txt file from the project's Pipfile:

pipenv lock --requirements
0
On

Recent pipenv versions (e.g. version 2022.6.7) are using the requirements subcommand and pipenv lock -r is deprecated.

To freeze default dependencies

pipenv requirements > requirements.txt

to freeze development dependencies as well

pipenv requirements --dev > dev-requirements.txt
0
On

You can create a requirements.txt using this command :

pip3 freeze > requirements.txt
0
On

Use this as -r flag is deprecated

pipenv requirements > requirements.txt