如何看一个python的模块是否安装了
Posted andy_0212
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何看一个python的模块是否安装了相关的知识,希望对你有一定的参考价值。
pip list
pip freeze
pip show <module_name>
pip search <module_name>
How to know if a python module is installed or not in the system: You can do a very easy test in terminal,
$ python -c "import math"
$ echo $?
0 # math module exists in system
$ python -c "import numpy"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named numpy
$ echo $?
1 # numpy module does not exist in system
In case we do not want to unwantedly import a module in question (which would happen in a try
statement) we can make use of sys.modules
to test modules that are installed and were imported before.
In the python shell issue:
>>> import sys
Then test for installed modules:
>>> ‘numpy‘ in sys.modules
True
>>> ‘scipy‘ in sys.modules
False
以上是关于如何看一个python的模块是否安装了的主要内容,如果未能解决你的问题,请参考以下文章
如何使用模块化代码片段中的LeakCanary检测内存泄漏?
如何检查模块是不是安装在 Python 中,如果没有,则将其安装在代码中?
Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)(代码片段