Musical Umbrella

My Personal Blog

Auto Generate requirements.txt from Your Python Code

Posted at — Feb 19, 2020


If you are working with data scientists, he might not make requirements.txt when he write 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 envrironment without requirements.txt(or other dependencies list).

Of course, if he who write the program could make it, it would be the easiest solution. But when it is impossible, there are the following solution: you are programmer, so you can use the program. It’s 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, and so on. Especially, you can convert from your code: that is, it can be generated requirements.txt from import statements.


curl -L | python3

If you need more detail, see installation documentation.

How To Use

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

└── my_package

and is like 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__':

In this situation, all you need is running this.

dephell deps convert --from=imports --to=requirements.txt

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

$ cat requirements.txt