I'm just starting out with Python, and have found out that I can import various libraries. How do I find out what libraries exist on my Mac that I can import? How do I find out what functions they include?
我刚刚开始使用Python,并且发现我可以导入各种库。如何找出我可以导入的Mac上存在哪些库?我如何找出它们包含哪些功能?
I seem to remember using some web server type thing to browse through local help files, but I may have imagined that!
我似乎记得使用一些Web服务器类型的东西浏览本地帮助文件,但我可能已经想到了!
6 个解决方案
#1
For the web server, you can run the pydoc
module that is included in the python distribution as a script:
对于Web服务器,您可以将python发行版中包含的pydoc模块作为脚本运行:
python /path/to/pydoc.py -p 1234
where 1234
is the port you want the server to run at. You can then visit http://localhost:1234/
and browse the documentation.
其中1234是您希望服务器运行的端口。然后,您可以访问http:// localhost:1234 /并浏览文档。
#2
From the Python REPL (the command-line interpreter / Read-Eval-Print-Loop), type help("modules")
to see a list of all your available libs.
从Python REPL(命令行解释器/ Read-Eval-Print-Loop)中,键入help(“modules”)以查看所有可用库的列表。
Then to see functions within a module, do help("posix")
, for example. If you haven't import
ed the library yet, you have to put quotes around the library's name.
然后,要查看模块中的函数,请执行帮助(“posix”)。如果尚未导入库,则必须在库的名称周围加上引号。
#3
You can install another library: yolk.
您可以安装另一个库:yolk。
yolk is a python package manager and will show you everything you have added via pypi. But it will also show you site-packages added through whatever local package manager you run.
yolk是一个python包管理器,它会显示你通过pypi添加的所有内容。但它也会向您显示通过您运行的任何本地包管理器添加的站点包。
#4
Every standard python distribution has these libraries, which cover most of what you will need in a project.
每个标准的python发行版都有这些库,它们涵盖了项目中所需的大部分内容。
In case you need to find out if a library exists at runtime, you do it like this
如果您需要在运行时查明库是否存在,您可以这样做
try:
import ObscureModule
except ImportError:
print "you need to install ObscureModule"
sys.exit(1) # or something like that
#5
On Leopard, depending on the python package you're using and the version number, the modules can be found in /Library/Python:
在Leopard上,根据您使用的python包和版本号,可以在/ Library / Python中找到这些模块:
/Library/Python/2.5/site-packages
or in /Library/Frameworks
或者在/ Library / Frameworks中
/Library/Frameworks/Python.framework/Versions/Current/lib/python2.6/site-packages
(it could also be 3.0 or whatever version)... I guess it is quite the same with Tiger
(它也可能是3.0或任何版本)......我猜这与Tiger完全一样
#6
just run the Python interpeter and type the command import "lib_name" if it gives an error, you don't have the lib installed...else you are good to go
只运行Python interpeter并输入命令import“lib_name”如果它出错,你没有安装lib ...否则你很高兴去
#1
For the web server, you can run the pydoc
module that is included in the python distribution as a script:
对于Web服务器,您可以将python发行版中包含的pydoc模块作为脚本运行:
python /path/to/pydoc.py -p 1234
where 1234
is the port you want the server to run at. You can then visit http://localhost:1234/
and browse the documentation.
其中1234是您希望服务器运行的端口。然后,您可以访问http:// localhost:1234 /并浏览文档。
#2
From the Python REPL (the command-line interpreter / Read-Eval-Print-Loop), type help("modules")
to see a list of all your available libs.
从Python REPL(命令行解释器/ Read-Eval-Print-Loop)中,键入help(“modules”)以查看所有可用库的列表。
Then to see functions within a module, do help("posix")
, for example. If you haven't import
ed the library yet, you have to put quotes around the library's name.
然后,要查看模块中的函数,请执行帮助(“posix”)。如果尚未导入库,则必须在库的名称周围加上引号。
#3
You can install another library: yolk.
您可以安装另一个库:yolk。
yolk is a python package manager and will show you everything you have added via pypi. But it will also show you site-packages added through whatever local package manager you run.
yolk是一个python包管理器,它会显示你通过pypi添加的所有内容。但它也会向您显示通过您运行的任何本地包管理器添加的站点包。
#4
Every standard python distribution has these libraries, which cover most of what you will need in a project.
每个标准的python发行版都有这些库,它们涵盖了项目中所需的大部分内容。
In case you need to find out if a library exists at runtime, you do it like this
如果您需要在运行时查明库是否存在,您可以这样做
try:
import ObscureModule
except ImportError:
print "you need to install ObscureModule"
sys.exit(1) # or something like that
#5
On Leopard, depending on the python package you're using and the version number, the modules can be found in /Library/Python:
在Leopard上,根据您使用的python包和版本号,可以在/ Library / Python中找到这些模块:
/Library/Python/2.5/site-packages
or in /Library/Frameworks
或者在/ Library / Frameworks中
/Library/Frameworks/Python.framework/Versions/Current/lib/python2.6/site-packages
(it could also be 3.0 or whatever version)... I guess it is quite the same with Tiger
(它也可能是3.0或任何版本)......我猜这与Tiger完全一样
#6
just run the Python interpeter and type the command import "lib_name" if it gives an error, you don't have the lib installed...else you are good to go
只运行Python interpeter并输入命令import“lib_name”如果它出错,你没有安装lib ...否则你很高兴去