简体   繁体   English

如何测试我是否已成功安装Python模块?

[英]How can I test that I have a Python module successfully installed?

I tried to install beautifulsoup. 我试图安装beautifulsoup。 I get such an error: 我收到这样的错误:
<-- snip --> <-片段->

raise MissingSectionHeaderError(fpname, lineno, line)
ConfigParser.MissingSectionHeaderError: File contains no section headers.
file: /Users/Sam/.pydistutils.cfg, line: 1
'install_lib = ~/Library/Python/$py_version_short/site-packages\n'

I get an similar error as I run my Python code in vim by: :!python % 我在vim中运行Python代码时遇到类似的错误::!python%

You can test if a module is installed like so: 您可以像这样测试是否安装了模块:

$ python
>>> import modulename

I'd try the following: 我会尝试以下方法:

  1. Open Python shell 打开Python Shell
  2. Type import <moduleName> import <moduleName>
  3. Parse the results (no error, ImportError or some module loading error) 解析结果(无错误,ImportError或某些模块加载错误)
  4. Try to invoke any function from imported module (to see if module is correctly initialized). 尝试从导入的模块调用任何函数(以查看模块是否正确初始化)。

Do you need something more sophisticated? 您需要更复杂的东西吗?

try: from prettytable import PrettyTable except: try: print "\\033[31mPython-prettytable Module [Not Available]\\033[0m" os.system('sudo apt-get install python-setuptools') os.system('sudo easy_install prettyTable') print "\\033[33mPython-prettytable Module [Installed]\\033[0m" except: print "PrettyTable Python Module Installation failed." 尝试:从prettytable导入PrettyTable除外:尝试:打印“ \\ 033 [31mPython-prettytable模块[不可用] \\ 033 [0m”] os.system('sudo apt-get install python-setuptools')os.system('sudo easy_install prettyTable')打印“ \\ 033 [33mPython-prettytable模块[已安装] \\ 033 [0m””,但:打印“ PrettyTable Python模块安装失败。” print "\\033[32mPython-prettytable Module [Available]\\033[0m" 打印“ \\ 033 [32mPython-prettytable模块[可用] \\ 033 [0m”

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

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