简体   繁体   English

如何列出 Python 程序使用的所有非标准模块?

[英]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.正如一些权威人士所说,Python 标准库在您的版本的 Python 核心文档中列出,默认情况下它是内置的,您不必将其与 Python 本身分开安装。 For example, math is a standard module, you needn't install it with pip install math .例如, math是一个标准模块,您不需要使用pip install math安装它。

Non-standard modules are not built-in, you have to install them before you use them in a Python program.非标准模块不是内置的,您必须在 Python 程序中使用它们之前安装它们。 For example, lxml is a non-standard module.例如, lxml是一个非标准模块。 If not installed, a "no module named lxml " error pops up when you import lxml in the Python shell.如果未安装,在 Python shell 中import lxml时会弹出“没有名为lxml的模块”错误。

It is time to turn to the topic now, how can I list all non-standard modules used by a Python program?现在是时候转向主题了,如何列出 Python 程序使用的所有非标准模块?

test.py is a Python program, it is executed with python test.py . test.py是一个 Python 程序,它使用python test.py执行。 How many non-standard modules are called when python test.py is run? python test.py运行时调用了多少个非标准模块?

pip freeze shows all installed modules, but some of them are not called by python test.py . pip freeze显示所有已安装的模块,但其中一些没有被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.我想知道同样的事情,因为我想通过试错法以外的方法为我的项目设置一个 conda 环境。 What I found worked was to make a bare environment with just python and pylint in it, using, eg,我发现的工作是创建一个只有 python 和 pylint 的裸环境,使用例如,

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.这将按文件很好地列出所有非标准导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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