2

Similar to how Node.js automatically adds dependencies to package-lock.json, is there a way I can automatically add requirements to my requirements.txt file for Python?

john-hen
  • 4,410
  • 2
  • 23
  • 40
Revircs
  • 1,312
  • 3
  • 12
  • 23

3 Answers3

2

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.

john-hen
  • 4,410
  • 2
  • 23
  • 40
1

If you are using Linux you can create an alias like this:

alias req='pip3 freeze > ~/requerments.txt'

And then when you want to install new package use this command:

pip3 install <package> | req
furas
  • 134,197
  • 12
  • 106
  • 148
Pw Wolf
  • 342
  • 1
  • 11
0

I think, to-requirements.txt is what you need:

pip install to-requirements.txt
requirements-txt setup

After that installed packages will be appended to requirements.txt. And uninstalled packages will be removed. It might require root access if you install it on system-wide Python interpreter. Add sudo if it failes.

voilalex
  • 2,041
  • 2
  • 13
  • 18