简体   繁体   English

您如何看待导入模块中的导入失败?

[英]How can you see what import fails in an imported module?

I need to make a portable app to put on a device and I want to put only the modules needed so there will not be all the standard modules so I need to see what my app needs, but if that module misses some imports I cannot see that, because it gives an error without being explicit what fails in that module: 我需要制作一个可移植的应用程序以放置在设备上,并且只想放置所需的模块,因此不会有所有标准模块,因此我需要查看应用程序的需求,但是如果该模块缺少某些导入,我将看不到那是因为它给出了一个错误而没有明确指出该模块中失败的原因:

Traceback (most recent call last):
  File "./packaging.py", line 30, in <module>
    import simplejson
ImportError: No module named simplejson

Is there a way to see what import exactly fails in that module? 有没有办法查看该模块中的导入完全失败?

The error means, that the import import simplejson fails because there is “[n]o module named simplejson” . 该错误意味着,由于存在“ [n] o名为simplejson的模块” ,所以import import simplejson失败。 The solution is to simply install said module . 解决方法是简单地安装所述模块

you can catch the exceptions, to know what went wrong and handle them appropriately: 您可以捕获异常,以了解出了什么问题并适当地处理它们:

try:
   import simplejson
except ImportError:
   print "simplejson module not found"
   #or do something else here, may be install that module

可能是字符串/usr/lib/python2.7/dist-packages不在sys.path文件夹列表中,因此在尝试导入模块或软件包时不会搜索该字符串。

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

相关问题 导入的模块导入子模块失败 - Imported module fails to import sub-module 使用import时如何检查导入的内容* - How to check what gets imported when you use import * Python模块导入:在导入的模块中导入的模块如何处理 - Python module import : what about modules imported in the imported module 给定导入模块,如何确定导入路径? - Given an imported module, how can I determine the import path? Python函数看不到导入的模块 - Python function can't see an imported module 你如何找到python导入特定模块的位置? - How can you find where python imported a particular module from? 在python中,如何在不保留导入模块名称空间的情况下从其他模块导入所有类? - In python, how do you import all classes from another module without keeping the imported module's namespace? 您如何修补模拟从 class 导入到您正在测试的模块的导入? - How do you patch mock an import from a class that is being imported to a module you are testing? 你怎么能看到你在 Jupyter notebook 中导入的带有星号的 python 模块列表? - How can you see a list of python modules that YOU imported in Jupyter notebook with asterisk? Python-如何查看和修改导入路径(找不到FileUtil模块) - Python - how to see and modify Import Path (FileUtil module can not be found)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM