Automatically Create Requirements.Txt

Automatically create requirements.txt

Use Pipenv or other tools is recommended for improving your development flow.

pip3 freeze > requirements.txt  # Python3
pip freeze > requirements.txt # Python2

If you do not use a virtual environment, pigar will be a good choice for you.

How to create a requirements.txt?

type following line in your command prompt

pip freeze > requirements.txt

Creating requirements.txt in GitLab

I think you mixed up some things. GitLab uses Git for version control of your files (your code). Therefore your repository should contain the files with your code. You can just put the files of your folder "MyProject" into the folder, where you cloned the repository to. Also add the requirements.txt the readme-file and so on.

The virtual environment is used to keep your system installation of Python clean and only have the necessary packages installed for each project. Among other things to avoid package requirement conflicts. The usage of an requirements.txt file is independet of the virtual environment, even if it is a sensible combination.

In general this means, your requirements.txt is always shared together with your code, because it lays within the same repository. When someone clones the repository, he can use the requirements.txt to install all the dependencies to his venv (or somewherer else) and then run your code without the nedd to install further python packages.

Your requirements.txt file has to contain columns, which look like this: numpy==1.21.4. Then you have to activate the environment with <your path to the venv folder>\venv\Scripts\activate and use python -m pip install -r requirements.txt to install the packages listed in your requirements.txt.

Is there a way to automatically add dependencies to requirements.txt as they are installed?

Since you mentioned Node.js specifically, the Python project that comes closest to what you're looking for is probably Pipenv.

Blurb from the Pipenv documentation:

Pipenv is a dependency manager for Python projects. If you're familiar with Node.js's npm or Ruby’s bundler, it is similar in spirit to those tools. While pip can install Python packages, Pipenv is recommended as it’s a higher-level tool that simplifies dependency management for common use cases.

It's quite a popular package among developers as the many stars on GitHub attest.

Alternatively, you can use a "virtual environment" in which you only install the external dependencies that your project needs. You can either use the venv module from the standard library or the Virtualenv package from PyPI, which offers certain additional features (that you may or may not need). With either of those, you can then use Python's (standard) package manager Pip to update the requirements file:

pip freeze >requirements.txt

This is the "semi-automatic" way, so to speak. Personally, I prefer to do this manually. That's because in a typical development environment ("virtual" or not), you also install packages that are only required for development tasks, such as running tests or building the documentation. They don't need to be installed along with your package on end-user machines, so shouldn't be in requirements.txt. Popular packaging tools such as Flit and Poetry manage these "extra dependencies" separately, as does Pip.

How to update a requirements file automatically when new packages are installed?

When using just plain pip to install packages, there is currently no way to make it automatically generate or update a requirements.txt file. It is still a manual process using pip freeze > requirements.txt.

If the purpose is to make sure the installed packages are tracked or registered properly (i.e. tracked in version control for a repository), then you will have to use other tools that "wrap" around pip's functionality.

You have two options.

Option 1: Use a package manager

There are a number of Python package managers that combines "install package" with "record installed packages somewhere".

  • pipenv
    • "It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages. It also generates the ever-important Pipfile.lock, which is used to produce deterministic builds.
    • Workflow (see Example Pipenv Workflow)
      $ pipenv install some-package

      $ cat Pipfile
      ...
      [packages]
      some-package = "*"

      # Commit modified Pipfile and Pipfile.lock
      $ git add Pipfile*

      # On some other copy of the repo, install stuff from Pipfile
      $ pipenv install
  • poetry
    • "poetry is a tool to handle dependency installation as well as building and packaging of Python packages. It only needs one file to do all of that: the new, standardized pyproject.toml. In other words, poetry uses pyproject.toml to replace setup.py, requirements.txt, setup.cfg, MANIFEST.in and the newly added Pipfile.*"
    • Workflow (see Basic Usage)
      $ poetry add requests

      $ cat pyproject.toml
      ...
      [tool.poetry.dependencies]
      requests = "*"

      # Commit modified pyproject.toml
      $ git add pyproject.toml

      # On some other copy of the repo, install stuff from Pipfile
      $ poetry install

Option 2: git pre-commit hook

This solution isn't going to happen "during installation of the package", but if the purpose is to make sure your tracked "requirements.txt" is synchronized with your virtual environment, then you can add a git pre-commit hook that:

  1. Generates a separate requirements_check.txt file
  2. Compares requirements_check.txt to your requirements.txt
  3. Aborts your commit if there are differences

Example .git/hooks/pre-commit:

#!/usr/local/bin/bash

pip freeze > requirements_check.txt
cmp --silent requirements_check.txt requirements.txt

if [ $? -gt 0 ];
then
echo "There are packages in the env not in requirements.txt"
echo "Aborting commit"
rm requirements_check.txt
exit 1
fi

rm requirements_check.txt
exit 0

Output:

$ git status
...
nothing to commit, working tree clean

$ pip install pydantic

$ git add .
$ git commit
The output of pip freeze is different from requirements.txt
Aborting commit

$ pip freeze > requirements.txt
$ git add .
$ git commit -m "Update requirements.txt"
[master 313b685] Update requirements.txt
1 file changed, 1 insertion(+)


Related Topics



Leave a reply



Submit