A tool to generate requirements.txt for Python project.
Project description
pigar
- Generating requirements.txt for Python project.
- Handling the difference between different Python versions.
- Jupyter notebook (
*.ipynb) support. - Including the import statements/magic from
exec/eval/importlib, doctest of docstring, etc.
- Searching ditributions(packages) by the top level import/module names.
- Checking the latest versions of requirements.
NOTE: Pipenv or uv or other tools is recommended for improving your development flow.
Installation
pigar can run on Python 3.7+.
To install it with pip, use:
[sudo] pip install pigar
To install it with conda, use:
conda install -c conda-forge pigar
To get the newest code from GitHub:
pip install git+https://github.com/damnever/pigar.git@[main or other branch] --upgrade
Usage
-
pigarcan consider most kinds of complicated situations(see FAQ). For example,pigar v1has py2_requirements.txt and py3_requirements.txt for different Python versions.# Generate requirements.txt for current directory. $ pigar generate # Generating requirements.txt for given directory in given file. $ pigar gen -f ../dev-requirements.txt ../pigar gen --with-referenced-commentscan list all files which referenced the package/distribution(the line numbers for Jupyter notebook may be a bit confusing), for example:# project/foo.py: 2,3 # project/bar/baz.py: 2,7,8,9 foobar == 3.3.3If the requirements.txt is overwritten,
pigarwill show the difference between the old and the new, use--dont-show-differencesto disable it.NOTE,
pigarwill search the packages/distributions in local environment first, then it will do further analysis and search missing packages/distributions on PyPI.See also: EXPERIMENTAL FEATURES.
-
If you do not know the import name that belongs to a specific distribution (more generally, does
Import Error: xxxdrive you crazy?), such asbs4which may come frombeautifulsoup4orMySQLdbwhich could come frommysql-python, try searching for it:$ pigar search bs4 MySQLdb -
Checking for the latest version:
# Specify a requirements file. $ pigar check -f ./requirements.txt # Or, you can let pigar searching all *requirements.txt in the current directory # level by itself. $ pigar check -
More:
TIP:
pigaraccepts a prefix for a command, such aspigar gen,pigar c.pigar --help
EXPERIMENTAL FEATURES
-
requirement-annotationsSome packages may require optional packages/distributions to be installed depending on your usage. To make
pigara little bit more useful, usepigar generate --enable-feature requirement-annotationsin conjunction with comments following the format below:import foo # pigar: required-imports=import_name_bar,import_name_baz import foo # pigar: required-packages=package-name-bar,package-name-baz # Extra comments are allowed. foo(features=['bar', 'baz']) # pigar: required-distributions=package-name-bar,package-name-baz
If you find the comment intrusive, you can extract those comments into a separate Python file and delete it as needed, for example, 'pigar_annotations.py'.
FAQ
Is `pigar` a dependency management tool?
No. I've thought about this many times, but there is too much dirty work to be done to make pigar's way reliable.
I like the way pigar does the job, but sadly, pigar does a bad job of managing dependencies, pigar is more like a tool to assist an old project to migrate to a new development workflow.
(1) Why does `pigar` show multiple packages/distributions for the same import name?
(2) Why does pigar generate different packages/distributions for the same import name in different environment?
pigar generate different packages/distributions for the same import name in different environment?pigar can not handle those situations gracefully, you may need to remove the duplicate packages in requirements.txt manually, or select one of them when pigar asks you.
Install the required packages/distributions(remove others) in local environment should fix it as well.
Why can't `pigar` find the packages/distributions that have not been explicit import?
Some frameworks may use some magic to import the modules for users automatically, and pigar can not handle it, you may need to fix it manually or use the EXPERIMENTAL FEATURES.
More
pigar does not use regular expressions in such a violent way. Instead, it uses AST, which is a better method for extracting imported names from arguments of exec/eval/importlib, doctest of docstring, etc. However, pigar can not solve all the tricky problems, see FAQ.
Also, pigar can detect the difference between different Python versions. For example, you can find concurrent.futures from the Python 3.2 standard library, but you will need install futures in earlier versions of Python to get concurrent.futures, this is not a hardcode.
If you have any issues or suggestions, please submit an issue on GitHub. All contributions are appreciated!
LICENSE
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pigar-2.2.0.tar.gz.
File metadata
- Download URL: pigar-2.2.0.tar.gz
- Upload date:
- Size: 39.6 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
870f43fc6da703fd9c62ed1a7f4a81ceb4e0336cce60be4c16bd7c3ebfe9f95b
|
|
| MD5 |
b1eedf85d31626a2f8c16975b364b7f0
|
|
| BLAKE2b-256 |
bd05dfc58d57dee50ed061c5472e700c4f21d7c57e102ad448208497d7de08db
|
File details
Details for the file pigar-2.2.0-py3-none-any.whl.
File metadata
- Download URL: pigar-2.2.0-py3-none-any.whl
- Upload date:
- Size: 39.9 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
73ba65974dc63e7bb68f81f753642b61d93cbd1aafc2a902aaaaa834a85d07d5
|
|
| MD5 |
e84721d3dd484ea6dfd4d49ad462b73d
|
|
| BLAKE2b-256 |
e03c7c804834720b807f3ed0654636d923da19c520a4eebe6c50d70cefc2cb66
|