Utilizing pipreqs to pull versioning information from a library
/opt/conda/lib/python3.9/site-packages/_distutils_hack/__init__.py:30: UserWarning: Setuptools is replacing distutils.
  warnings.warn("Setuptools is replacing distutils.")

pipreqs

pipreqs is a lightweight library that allows for searching through source code to find all imports used throughout the codebase.

We will utilize this for helping us write a clean requirements.txt, further than what it already does. Specifically the get_all_imports function

Integration

What follows is integration documentation for combining our two libraries

get_all_imports('dependency_checker')
['pipdeptree']
get_installed_dependencies('pipdeptree', depth_limit=1)
{'pip': '21.2.4'}
 

extract_project_dependencies[source]

extract_project_dependencies(folder_name:Union[str, Path], depth_limit:int=1, ignore_dependencies:Union[str, typing.List[str]]=[], ignore_libraries:Union[str, typing.List[str]]=[])

Looks at a project folder, and pulls out all dependencies utilized in it

Type Default Details
folder_name typing.Union[str, pathlib.Path] Name of a folder containing the source code for a python project
depth_limit int 1 The maximum recursive depth, when following a dependency's tree
ignore_dependencies typing.Union[str, typing.List[str]] None Libraries who's dependency versions we ignore and instead use the library version
ignore_libraries typing.Union[str, typing.List[str]] None List of explicit library names that we remove from the final version dictionary

generate_requirements_file[source]

generate_requirements_file(folder_name:"Name of a folder containing source code for a python project", depth_limit:"The maximum recursive depth, when following a dependency's tree"=1, ignore_dependencies:"Libraries who's dependency versions we ignore and instead use the library version"=[], ignore_libraries:"List of explicit library names that we remove from the final version dictionary"=[], requirements_file_name:"Name of requirements file"='requirements.txt', new_requirements_path:"Relative location to store requirements file"='.', override_existing_requirements:"Whether to override the requirements file (0/1)"=0)

Builds a requirements.txt file from a project folder to a directory

Usage example:

generate_requirements_file(
    folder_name='dependency_checker',
    depth_limit=1,
    requirements_file_name='test_requirements.txt',
    new_requirements_path='tests/''
)