Package: pigar
pigarGenerating requirements.txt for Python project.Handling the difference between different Python versions.Jupyter notebook (*.ipynb) support.Including the import statements/magic fromexec/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 other toolsis recommended for improving your development flow.Installationpigarcan run on Python 3.7+.To install it withpip, use:[sudo] pip install pigarTo install it withconda, use:conda install -c conda-forge pigarTo get the newest code from GitHub:pip install git+https://github.com/damnever/pigar.git@[main or other branch] --upgradeUsagepigarcan consider most kinds of complicated situations(seeFAQ). For example,pigar v1haspy2_requirements.txtandpy3_requirements.txtfor 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, doesImport Error: xxxdrive you crazy?), such asbs4which may come frombeautifulsoup4orMySQLdbwhich could come frommysql-python, try searching for it:$ pigar search bs4 MySQLdbChecking 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 checkMore:TIP:pigaraccepts a prefix for a command, such aspigar gen,pigar c.pigar --helpEXPERIMENTAL FEATURESrequirement-annotationsSome packages may require optional packages/distributions to be installed depending on your usage. To makepigara little bit more useful, usepigar generate --enable-feature requirement-annotationsin conjunction with comments following the format below:importfoo# pigar: required-imports=import_name_bar,import_name_bazimportfoo# 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-bazIf 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'.FAQIs `pigar` a dependency management tool?No.I've thought about this many times, but there is too much dirty work to be done to makepigar's way reliable.I like the waypigardoes the job, but sadly,pigardoes a bad job of managing dependencies,pigaris 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 doespigargenerate different packages/distributions for the same import name in different environment?pigarcan not handle those situations gracefully, you may need to remove the duplicate packages in requirements.txt manually, or select one of them whenpigarasks you.
Install the required packages/distributions(remove others) in local environment should fix it as well.Related issues:#32,#68,#75.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, andpigarcan not handle it, you may need to fix it manually or use theEXPERIMENTAL FEATURES.Related issues:#33,#103Morepigardoes not use regular expressions in such a violent way. Instead, it uses AST, which is a better method for extracting imported names from arguments ofexec/eval/importlib, doctest of docstring, etc. However,pigarcan not solve all the tricky problems, seeFAQ.Also,pigarcan detect the difference between different Python versions. For example, you can findconcurrent.futuresfrom the Python 3.2 standard library, but you will need installfuturesin earlier versions of Python to getconcurrent.futures, this is not a hardcode.If you have any issues or suggestions,please submit an issue on GitHub.All contributions are appreciated!LICENSEThe BSD 3-Clause License