简体   繁体   中英

How can I list all non-standard modules used by a Python program?

As some pundits say that the Python standard library is listed in the Python core documentation for your version, it is built-in by default, you don't have to install it separately from Python itself. For example, math is a standard module, you needn't install it with pip install math .

Non-standard modules are not built-in, you have to install them before you use them in a Python program. For example, lxml is a non-standard module. If not installed, a "no module named lxml " error pops up when you import lxml in the Python shell.

It is time to turn to the topic now, how can I list all non-standard modules used by a Python program?

test.py is a Python program, it is executed with python test.py . How many non-standard modules are called when python test.py is run?

pip freeze shows all installed modules, but some of them are not called by python test.py .

I was wondering the same thing, because I wanted to set up a conda environment for my project by a method other than trial-and-error. What I found worked was to make a bare environment with just python and pylint in it, using, eg,

conda create -n myenv python=3 pylint

and then from within that environment run:

pylint /path/to/module --disable=all --enable=import-error

This will nicely list, by file, all of the non-standard imports.

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