Automatically create requirements.txt

You can use the pip freeze command to automatically generate a requirements.txt file in Python. This command lists all of the packages and their versions that are currently installed in your virtual environment. Here's an example of how to use the command in a Python script:

import subprocess

# Create a requirements.txt file
with open("requirements.txt", "w") as f:
    subprocess.call(["pip", "freeze"], stdout=f)

This script opens a file called "requirements.txt" in write mode, and then calls the pip freeze command, passing the file object as the stdout argument. This writes the output of the pip freeze command to the file, creating a requirements.txt file containing a list of all the packages and their versions currently installed in your environment.

You may also use pipenv instead of pip if you're using pipenv.

import subprocess

# Create a requirements.txt file
with open("requirements.txt", "w") as f:
    subprocess.call(["pipenv", "lock", "-r"], stdout=f)

This will output the dependency tree of your project.