找到您的库中的所有导入
项目描述
depfinder
安装
depfinder 在PyPI上。它针对Python 2.7和3.6-3.8进行了测试。
pip install depfinder
它也可以通过conda获得。
conda install -c conda-forge depfinder
它也可以通过github获得。
git clone git@github.com:ericdill/depfinder
cd depfinder
python setup.py install
使用depfinder
$ depfinder -h
usage: depfinder [-h] [-y] [-V] [--no-remap] [-v] [-q] [-k KEY] [--conda]
                [--pdb]
                file_or_directory
Tool for inspecting the dependencies of your python project.
positional arguments:
    file_or_directory  Valid options are a single python file, a single jupyter
                        (ipython) notebook or a directory of files that include
                        python files
optional arguments:
    -h, --help         show this help message and exit
    -y, --yaml         Output in syntactically valid yaml when true. Defaults to
                        False
    -V, --version      Print out the version of depfinder and exit
    --no-remap         Do not remap the names of the imported libraries to their
                        proper conda name
    -v, --verbose      Enable debug level logging info from depfinder
    -q, --quiet        Turn off all logging from depfinder
    -k KEY, --key KEY  Select some or all of the output keys. Valid options are
                        'required', 'optional', 'builtin', 'relative', 'all'.
                        Defaults to 'all'
    --conda            Format output so it can be passed as an argument to conda
                        install or conda create
    --pdb              Enable PDB debugging on exception
好,太好了。这是帮助输出。不是特别有用。当我们在depfinder的源代码上运行depfinder时,它的输出是什么样子的?
$ depfinder depfinder
{'builtin': ['__future__',
                'argparse',
                'ast',
                'collections',
                'copy',
                'errno',
                'json',
                'logging',
                'os',
                'pprint',
                're',
                'subprocess',
                'sys'],
    'relative': ['_version', 'main'],
    'required': ['pyyaml', 'stdlib-list']}
那么,这些是什么意思呢?嗯,builtin是标准库中构建的模块。 required是不来自标准库或depfinder包内的模块,而relative是depfinder源代码中从一个模块导入到另一个模块的模块。
另请参阅 这个笔记本
描述
自动查找库中所有唯一的导入,因为谁愿意手动去做呢?depfinder 使用了 ast (抽象语法树) 模块(以及 更多 ast 文档)来查找所有的 :pyast.Try 和 :pyast.ImportFrom 节点。这些 :pyast.Import 和 :pyast.ImportFrom 节点根据以下类别分组,按照优先级从高到低排序
- 
相对导入:导入来自同一库内的相对导入 
- 
内置:导入是内置在标准库中的,这是通过使用 stdlib-list 从官方 Python 文档中抓取内置函数来确定的 
- 
有疑问的:导入发生在以下任一组合中 - {py:class}ast.Try({py:class}ast.TryExcept在 py27)
- {py:class}ast.FunctionDef或 {py:class}ast.AsyncFunctionDef
- {py:class}ast.If、{py:class}ast.While、{py:class}ast.For或 {py:class}ast.AsyncFor。
- {py:class}ast.match_case。
 该模块可能在不导入这些的情况下导入,但它可能不会具有完整的功能。 
- {py:class}
- 
必需:导入发生在模块的顶层,当导入模块时将会执行。这些导入必须在环境中记录,否则模块将无法导入。 
测试
它依赖于 stdlib-list 和 pyyaml。我使用 stdlib-list 获取标准库中内置的库列表。这些需求可以通过 pip 安装
pip install -r requirements.txt
同时安装测试需求
pip install -r test-requirements.txt
然后您可以从 git 仓库的根目录运行测试
coverage run run_tests.py
发布
手动
- 创建一个带注解的标签并将其推送到 github。 git tag -a 标签名然后执行git push --tags
- git checkout 标签名
- python -m build --sdist --wheel . --outdir dist
- twine check dist/*
- twine upload dist/* --verbose
API
.. currentmodule:: depfinder.main
.. autofunction:: get_imported_libs
.. autofunction:: iterate_over_library
.. autofunction:: simple_import_search
IPython/Jupyter Notebook 支持
depfinder 支持 v4 Jupyter Notebook。
.. autofunction:: notebook_path_to_dependencies
          项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解更多关于 安装包 的信息。