DEV Community

515hikaru
515hikaru

Posted on • Edited on • Originally published at 515hikaru.dev

Auto Generate requirements.txt from Your Python Code

Introduction

If you are working with a data scientist, he might not make requirements.txt when he writes code in "notebook". And you often need to run the program on your PC, even though the program only ran on his machine. In this case, you may have to create python virtual environment without requirements.txt(or other dependencies list).

Of course, if he who writes the program could make it, it would be the easiest solution. But when it is impossible, there is the following solution: you are a programmer, so you can use the program. It's DepHell.

DepHell

dephell/dephell: Python project management. Manage packages: convert between formats, lock, install, resolve, isolate, test, build graph, show outdated, audit. Manage venvs, build package, bump version.

DepHell has many features. One of them is converting between formats, e.g. requirements.txt, Pipfile, setup.py and so on. Especially, you can convert from your code: that is, it can be generated requirements.txt from import statements.

Installation

curl -L dephell.org/install | python3
Enter fullscreen mode Exit fullscreen mode

If you need more detail, see installation documentation.

How To Use

I'll show a simple example. If you have a python package like this:

.
└── my_package
    ├── __init__.py
    └── main.py
Enter fullscreen mode Exit fullscreen mode

and main.py is like the following.

import numpy as np
import pandas as pd
from sklearn.svm import SVC
import matplotlib.pyplot as plt
import seaborn as sns

def main():
    ...  # write something

if __name__ == '__main__':
    main()
Enter fullscreen mode Exit fullscreen mode

In this situation, all you need is running this.

dephell deps convert --from=imports --to=requirements.txt
Enter fullscreen mode Exit fullscreen mode

You will have requirements.txt. It will have all the packages you need.

$ cat requirements.txt 
matplotlib
numpy
pandas
scikit-learn
seaborn
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
sidkhullar profile image
sid-khullar

This sounds good! Does it also work with an entry-point python program and work its way through all the import calls, and include their dependencies in the requirements.txt, or does it only work with a single, stand-alone python file?