码迷,mamicode.com
首页 > 编程语言 > 详细

如何看一个python的模块是否安装了

时间:2018-12-30 02:44:37      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:statement   wan   mina   and   iss   term   back   str   call   

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 trystatement) 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的模块是否安装了

标签:statement   wan   mina   and   iss   term   back   str   call   

原文地址:https://www.cnblogs.com/andy-0212/p/10198110.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!