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

时间:2021-12-31 07:01:13

I tried to install beautifulsoup. I get such an error:
<-- snip -->

我试着安装beautifulsoup。我收到这样的错误:< - 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%

3 个解决方案

#1


You can test if a module is installed like so:

您可以测试模块是否安装如下:

$ python
>>> import modulename

#2


I'd try the following:

我尝试以下方法:

  1. Open Python shell
  2. 打开Python shell

  3. Type import <moduleName>
  4. 输入import

  5. Parse the results (no error, ImportError or some module loading error)
  6. 解析结果(没有错误,ImportError或某些模块加载错误)

  7. Try to invoke any function from imported module (to see if module is correctly initialized).
  8. 尝试从导入的模块调用任何函数(以查看模块是否已正确初始化)。

Do you need something more sophisticated?

你需要更复杂的东西吗?

#3


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." print "\033[32mPython-prettytable Module [Available]\033[0m"

尝试:从prettytable导入PrettyTable,除了: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”除了:print“PrettyTable Python模块安装失败。”打印“\ 033 [32mPython-prettytable模块[可用] \ 033 [0m”

#1


You can test if a module is installed like so:

您可以测试模块是否安装如下:

$ python
>>> import modulename

#2


I'd try the following:

我尝试以下方法:

  1. Open Python shell
  2. 打开Python shell

  3. Type import <moduleName>
  4. 输入import

  5. Parse the results (no error, ImportError or some module loading error)
  6. 解析结果(没有错误,ImportError或某些模块加载错误)

  7. Try to invoke any function from imported module (to see if module is correctly initialized).
  8. 尝试从导入的模块调用任何函数(以查看模块是否已正确初始化)。

Do you need something more sophisticated?

你需要更复杂的东西吗?

#3


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." print "\033[32mPython-prettytable Module [Available]\033[0m"

尝试:从prettytable导入PrettyTable,除了: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”除了:print“PrettyTable Python模块安装失败。”打印“\ 033 [32mPython-prettytable模块[可用] \ 033 [0m”