Generate and Install Required Python Packages: pip freeze > requirements.txt
and pip install -r requirements.txt
Python pip freeze
command is to “output installed packages in requirements format.”1
For example, after creating a new virtual environment by venv
module2 and in which installing package numpy
(by command pip install numpy
), we can find there are three packages in this virtual environment right now:
1
pip list
1
2
3
4
5
Package Version
---------- -------
numpy 2.0.0
pip 24.0
setuptools 65.5.0
Then, we can use pip freeze
to output packages name and version information in requirements format to PowerShell host:
1
pip freeze
1
numpy==2.0.0
As can be seen, only numpy
package information is printed. The reason is that pip freeze
will neglect some packages by default, and to make it do not, we can specify --all
option1:
--all
Do not skip these packages in the output: wheel, pip, setuptools, distribute
(environment variable: PIP_ALL
)
Take this case, we have:
1
pip freeze --all
1
2
3
numpy==2.0.0
pip==24.0
setuptools==65.5.0
By the way, I think the default option is more reasonable, because these packages look like ‘tool kit’, which are installed by default in many scenarios.
And we can generate corresponding requirements.txt
file with the aid of PowerShell redirection operator3:
1
pip freeze > requirements.txt
Conversely, to install packages in requirements.txt
, we can execute the following command14:
1
pip install -r requirements.txt
References