简体   繁体   中英

How do I identify imported packages in python code

it frequently happens that I get single python files or whole projects where I need to identify which modules are imported - mostly for license clearing purpose.

How can I do this in an - as complete as possible - way? I am aware of the pipreqs tool but it somehow does not list all modules in the final report, although it seems to identify all of them -> according to the debug logs.

I'm fine with investing time into developing a tool - so a strategy on how to do this would be great.

Thank you

Here is one solution, if you are just interested in licenses of your dependencies:

If you get access to a project, I reckon there must be something like a requirements.txt , Pipfile , pyproject.toml (Poetry) or similar which contains all the required packages. Or you should have access to the running environment, where all packages are installed.

With that assumption, you could use pip-licenses to identify the licences of all installed dependencies. Usage is quite simple:

$ pip install pip-licenses
$ pip-licenses --with-urls
Name    Version  License  URL
Django  2.0.2    BSD      https://www.djangoproject.com/
pytz    2017.3   MIT      http://pythonhosted.org/pytz

I often had the problem, that the license wasn't exposed in the setup.py file. Therefore I had to visit the project page itself to get the license.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM