Pip freeze for only project requirements
PythonPipPython Problem Overview
When I run pip freeze > requirements.txt
it seems to include all installed packages. This appears to be the documented behavior.
I have, however, done something wrong as this now includes things like Django in projects that have no business with Django.
How do I get requirements for just this project? or in the future how do I install a package with pip
to be used for this project. I think I missed something about a virtualenv
.
Python Solutions
Solution 1 - Python
pipreqs can save the day for a specific project. Just
pip install pipreqs
#then
pipreqs path/to/project
Solution 2 - Python
I have tried both pipreqs
and pigar
and found pigar
is better because it also generates information about where it is used, it also has more options.
Solution 3 - Python
I use this command
EDIT: Thanks Addisson Klinke for suggestion
> pip freeze -r requirements.txt | grep -B100 "pip freeze" | grep -v "pip freeze"
pip freeze -r requirements.txt | sed '/freeze/,$ d'
When I ran pip freeze -r requirements.txt
the output is something like
APScheduler==3.2.0
Eve==0.6.4
Eve-Elastic==0.3.8
## The following requirements were added by pip freeze:
arrow==0.8.0
Cerberus==0.9.2
I have a requirements file like this
APScheduler
Eve
Eve-Elastic
So I get this output and sed
to remove the dependencies that I don`t want.
First output this to a file
pip freeze -q -r requirements.txt | sed '/freeze/,$ d' > requirements-froze.txt
That will output just the libs with version
APScheduler==3.2.0
Eve==0.6.4
Eve-Elastic==0.3.8
Then replace requirements file
mv requirements-froze.txt requirements.txt
Solution 4 - Python
I still suggest using the official pip freeze > requirements.txt
(documentation) compared to the two alternative features using pigar
and pipreqs
mentioned in the other answers because pip freeze
lists the effective package names.
Incomplete comparison among the three as per February 2022
Criteria \ Tool | pip freeze > requirements.txt | pigar | pipreqs |
---|---|---|---|
Name mismatch (1) | Package my-package==1.0.0 |
Module my_package == 1.0.0 |
Module my_package==1.0.0 |
Module overloading (2) | All packages my-package1==1.0.0 , my-package2==2.0.0 |
None | Top-level module (version shows 0.0.0) my==0.0.0 |
Showing only directly used packages | No | Yes | Yes |
Minimal contents | Yes | No | Yes |
(1) There can be a mismatch between module and package name such as my-package
(package name) vs my_package
(module name).
(2) There can be several packages using the same top level folder such as my-package1
and my-package2
(package names) which are installed under my/package1
and my/package2
, which are imported by Python's command import my.package1
and import my.package2
. Note that pipreqs notes version 0.0.0 for the not existing package my
.
I know these are very particular cases but I hope giving people this overview can help understanding limitations and avoid possible mistakes.
Solution 5 - Python
if you are using linux then do it with sed
pip freeze | sed 's/==.*$/''/' > requirements.txt
Solution 6 - Python
Here is a simplified version based on the previous comment: https://stackoverflow.com/a/40026169/4052041
mv requirements.txt requirements.txt.bak
pip freeze -q -r requirements.txt.bak | awk '/.*pip freeze.*/ {exit} {print}' > requirements.txt
Solution 7 - Python
pip install pipreqs,
pipreqs>requirements.txt
That works easily
Solution 8 - Python
I had the same issue with pip freeze
. In my case the problem was that I ran pip freeze
without activating my projects virtual environment. I activated the virtual environment and pip freeze > requirements.txt
worked fine.
So do make sure you activate your projects virtual environment by running <virtualenv folder name>\Scipts\activate
on Windows or source <virtualenv folder name>\bin\activate
on Linux.
If the virtualenv
has global access you should run pip freeze
with the -l
or --local
option, pip freeze -l
which according to the pip freeze docs
> -l, --local
If in a virtualenv that has global access, do not output globally-installed packages.
Solution 9 - Python
I just had the same issue, here's what I've found to solve the problem.
First create the venv in the directory of your project, then activate it.
For Linux/MacOS : python3 -m venv ./venv
source myvenv/bin/activate
For Windows : python3 -m venv .\venv
env\Scripts\activate.bat
Now pip freeze > requirements.txt
should only takes the library used in the project.
NB: If you have already begin your project you will have to reinstall all the library to have them in pip freeze.
Solution 10 - Python
I do came across same situation. After activating the virtual enviroment and doing
pip3 freeze > requirements.txt
still its collect all package install on my WSL ubuntu 22.04
After install "pipreqs" and typing pipreqs>requirments.txt My Wsl got stuck.
The best solution is pip3 freeze -l > requirments.txt
its only collected my local package used for my development.