- We'd like to create a file which stores all the packages we have manually installed.
- The questions arises whether we'd like to list all the dependencies or only those packages we have manually installed.
- As always it depends, so here are listed all the options you have.
- Take away is it is how kind of complicated this simple task really is in Python.
- Firstly, your project file must be a py file which is direct python file.
- Convert it to
.py
using the command line:jupyter nbconvert --to=python
- Convert it to
.py
using the GUI:File -> Download as -> Python (.py)
- Then you can use one of the folling options listed below.
pip freeze -> requirements.txt
- This will dump the current versions of all the installed modules on that system irrespective of there usage in the current project. That is likely to be a long list of you have not installed a single manually and you have a freshly created virtual environment.
- Further consider that this will only list modules that have been installed via
pip
, if you have installed a packaged viaconda
this may not be there.
pip3 freeze -> requirements.txt
- Same story as per option #2.
- Install
pipreqs
withpip install pipreqs
- Then try:
pipreqs /path/to/project
which will dump arequirements.txt
in the specified directory. - The difference here with respect to the other two is that, only those packages that are used in the specified folder will be saved.
- if you are using conda then try:
conda list -e > requirements.txt
- This approach seems to be more robust and more repetable, meaning that there is no chance the subpackages will become somehow incompatible.
- Here is the issue: if your project uses
pandas==1.3.2
which in turn usesnumpy==1.21.2
among other packages. But pipreqs itself does not write the sub-packages inrequirments.txt
. - Install
pip-tools
with:pip install pip-tools
- Then use this:
pipreqs --savepath=requirements.in && pip-compile --resolver=backtracking
- The new
requirements.txt
will have a structure similar to this:
numpy==1.24.2
# via
# datasets
# pandas
# pyarrow
# pytorch-lightning
# scikit-learn
# scipy
# torchmetrics
# transformers