56

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"
arshbot
  • 12,535
  • 14
  • 48
  • 71

11 Answers11

112

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

pipenv lock -r > requirements.txt
Aratz Manterola Lasa
  • 1,294
  • 1
  • 9
  • 4
  • 65
    This method is deprecated, we should use `pipenv requirements > requirements.txt` instead, see https://pipenv.pypa.io/en/latest/advanced/#generating-a-requirements-txt and [ARDVL's answer](https://stackoverflow.com/a/72684458/322283). – Marijn Jul 26 '22 at 05:26
76

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
Collin Heist
  • 1,962
  • 1
  • 10
  • 21
29

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.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
16

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
ARDVL
  • 179
  • 1
  • 6
12

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

$ pipenv run pip freeze  > requirements.txt 
bkawan
  • 1,171
  • 8
  • 14
  • It gave `FileNotFoundError: [Errno 2] No such file or directory` trying to look for pip file in the .venv directory for the respective project. Instead following worked for me : `pipenv run python -m pip freeze > requirements.txt` – Dhwanit Dec 03 '22 at 15:26
1

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

Joël Brigate
  • 237
  • 2
  • 13
Zach Bloomquist
  • 5,309
  • 29
  • 44
0

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)

0

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

pipenv lock --requirements
Chris Claude
  • 973
  • 12
  • 25
0

Use this as -r flag is deprecated

pipenv requirements > requirements.txt
akshay parkar
  • 85
  • 1
  • 12
0
pipenv run python -m pip freeze > requirements.txt
Dhwanit
  • 610
  • 1
  • 9
  • 17
-4

You can create a requirements.txt using this command :

pip3 freeze > requirements.txt
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
Alouani Younes
  • 948
  • 9
  • 17