Python 获取可用模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3952513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:32:05  来源:igfitidea点击:

Get available modules

pythonlibraries

提问by Wizzard

With PHP you have the phpinfo()which lists installed modules and then from there look up what they do.

使用 PHP,您可以phpinfo()列出已安装的模块,然后从那里查找它们的作用。

Is there a way to see what packages/modules are installed to import?

有没有办法查看安装了哪些包/模块来导入?

采纳答案by ghostdog74

Type help()in the interpreter

键入help()的解释

then

然后

To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".                                     

help> modules 

回答by intuited

If you use ipython, which is an improved interactive Python shell (aka "REPL"), you can type import?(note the space at the end) followed by a press of the [TAB]key to get a list of importable modules.

如果您使用ipython,这是一个改进的交互式 Python shell(又名“ REPL”),您可以输入import?(注意末尾的空格)然后按[TAB]键以获取可导入模块的列表。

As noted in this SO post, you will have to reset its hash of modules after installing (certain?) new ones. You likely don't need to worry about this yet.

正如这篇 SO post 中所述,在安装(某些?)新模块后,您必须重置其模块的哈希值。你可能还不需要担心这个。

If you don't use ipython, and you haven't tried it, it might be worth checking out. It's a lot better than the basic Python shell, or pretty much any other REPL I've used.

如果您不使用ipython,并且您还没有尝试过,则可能值得一试。它比基本的 Python shell 或几乎我使用过的任何其他 REPL 都要好得多。

ipythonInstallation

ipython安装

If you're running linux, there is most likely an ipythonpackage that you can install through your system management tools. Others will want to follow these instructions.

如果您运行的是 linux,那么很可能ipython您可以通过系统管理工具安装一个软件包。其他人会希望遵循这些说明

If your installation route requires you to use easy_install, you may want to consider instead using pip. pipis a bit smarter than easy_installand does a better job of keeping track of file locations. This is very helpful if you end up wanting to uninstall ipython.

如果您的安装路线需要您使用easy_install,您可能需要考虑使用pip. pipeasy_install跟踪文件位置更聪明,并且在跟踪文件位置方面做得更好。如果您最终想要卸载ipython.

Listing packages

列出包

Note that the above tip only lists modules. For a list which also includes packages—which contain modules— you can do from?+ [TAB]. An explanation of the difference between packages and modules can be found in the Modules chapterof the helpful official Python tutorial.

请注意,上面的提示仅列出了modules。对于还包含包(包含模块)的列表,您可以执行from?+ [TAB]。可以在有用的官方 Python 教程的模块一章中找到对包和模块之间区别的解释。

#rtfm

#rtfm

As an added note, if you are very new to python, your time may be better spent browsing the standard library documentationthan by just selecting modules based on their name. Python's core documentation is well-written and well-organized. The organizational groups —File and Directory Access, Data Types, etc.— used in the library documentation's table of contents are not readily apparent from the module/package names, and are not really used elsewhere, but serve as a valuable learning aid.

作为补充说明,如果您对 Python 非常陌生,那么您最好花时间浏览标准库文档,而不是仅根据名称选择模块。Python 的核心文档编写良好且组织良好。库文档目录中使用的组织组——文件和目录访问数据类型等——从模块/包名称中并不容易看出,并且在其他地方没有真正使用,但可以作为有价值的学习帮助。

回答by pyfunc

As aaronasterling says, all .py or .pyc files on sys.path is a module because it can be imported. There are scripts that can let you find what external module is installed in site-packages.

正如 aaronasterling 所说,sys.path 上的所有 .py 或 .pyc 文件都是一个模块,因为它可以导入。有一些脚本可以让您找到安装在站点包中的外部模块。

Yolk is a Python command-line tool and library for obtaining information about packages installed by setuptools, easy_install and distutils and it can also query pypi packages.

Yolk 是一个 Python 命令行工具和库,用于获取有关 setuptools、easy_install 和 distutils 安装的包的信息,它还可以查询 pypi 包。

回答by rann

You can list available modules like so:

您可以像这样列出可用的模块:

python -c "for dist in __import__('pkg_resources').working_set:print dist.project_name.replace('Python', '')"

回答by Rajat Sewal

This was very useful. Here is a script version of this:

这非常有用。这是一个脚本版本:

# To list all installed packages just execfile THIS file
# execfile('list_all_pkgs.py')
for dist in __import__('pkg_resources').working_set:
   print dist.project_name.replace('Python', '')