在Python脚本中检查包是否安装的好方法是什么?我知道解释器很容易,但是我需要在脚本中完成它.
我想我可以检查系统中是否有安装过程中创建的目录,但我觉得有更好的方法.我正在尝试确保安装了Skype4Py软件包,如果没有,我会安装它.
我完成检查的想法
我正在使用django的调试工具栏,如果两个条件为真,我想将它添加到项目中:
settings.DEBUG 是 True做第一个并不难
# adding django debug toolbar
if DEBUG:
MIDDLEWARE_CLASSES += 'debug_toolbar.middleware.DebugToolbarMiddleware',
INSTALLED_APPS += 'debug_toolbar',
Run Code Online (Sandbox Code Playgroud)
但是如何检查模块是否存在?
我找到了这个解决方案:
try:
import debug_toolbar
except ImportError:
pass
Run Code Online (Sandbox Code Playgroud)
但由于导入发生在django的其他地方,我需要if/else逻辑来检查模块是否存在,所以我可以在settings.py中检查它
def module_exists(module_name):
# ??????
# adding django debug toolbar
if DEBUG and module_exists('debug_toolbar'):
MIDDLEWARE_CLASSES += 'debug_toolbar.middleware.DebugToolbarMiddleware',
INSTALLED_APPS += 'debug_toolbar',
Run Code Online (Sandbox Code Playgroud)
有办法吗?
try:
import MySQLdb
# some action
except ImportError as err:
# fallback code
Run Code Online (Sandbox Code Playgroud)
PyCharm给出了代码检查警告:
"除了ImportError"的try块中的'MySQLdb'也应该在除块之外定义
此检查检测应解决但不能解析的名称.由于动态调度和鸭子打字,这在有限但有用的情况下是可能的.顶级和类级别项比实例项更受支持.
好吧,我认为警告是合理的,因为fallback code假设没有安装'MySQLdb',而它可能是一些不同的错误,只是引发了ImportError.所以我使用了类似的东西:
try:
import MySQLdb
# some action
except ImportError as err:
if "MySQLdb" in repr(err):
# fallback code
else:
raise
Run Code Online (Sandbox Code Playgroud)
PyCharm警报仍然存在,但它可能只是一个PyCharm问题(谷歌显示此类检查的问题)
问题:
当你"除了ImportError"之外,真的值得检查名字吗?即使在简单的情况下(没有some action之后import MySQLdb)?
如果值得检查,上面的例子是正确的方法吗?如果不是 - 什么是正确的方法?
PS MySQLdb只是系统中可能缺少的模块的一个示例.
我正在尝试使用importlib库来验证在Python 3.5.2中执行脚本的计算机上是否安装了nmap库
我正在尝试使用importlib.util.find_spec("nmap")但收到以下错误.
>>> import importlib
>>> importlib.util.find_spec("nmap")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'importlib' has no attribute 'util'
Run Code Online (Sandbox Code Playgroud)
谁能告诉我哪里出错了?
编辑
我能够使用以下代码使函数工作.
#!/usr/bin/pythonw
import importlib
from importlib import util
#check to see if nmap module is installed
find_nmap = util.find_spec("nmap")
if find_nmap is None:
print("Error")
Run Code Online (Sandbox Code Playgroud) 因此,我有以下变量:
text1 = "some text here"
text2 = "other text here"
df = pd.DataFrame({"a": [1,2,3,4,5], "b": [6,7,8,9,10], "c": [11,12,13,14,15]})
Run Code Online (Sandbox Code Playgroud)
正如我所知,有可能使用xlsxwriter来执行此操作,这意味着我基本上必须遍历整个数据帧,以将每个条目写入excel工作簿中的不同单元格.这非常麻烦.
所以,我认为必须有一个更简单的方法来做到这一点; 这样的事情:
writer = pd.ExcelWriter("test.xlsx", engine="xlsxwriter")
writer.write(text1, startrow=0, startcol=0)
writer.write(text1, startrow=1, startcol=0)
df.to_excel(writer, startrow=4, startcol=0)
Run Code Online (Sandbox Code Playgroud)
有没有更简单的方法?
我已经厌倦了当你使用命名空间包并将你的代码库划分为单独的文件夹时,pylint无法导入文件的问题.因此,我开始深入研究astNG源代码,该代码已被确定为问题的根源(参见astng上的bugreport 8796).这个问题的核心似乎是在imp.find_module寻找进口的过程中使用蟒蛇.
什么情况是,进口的第一个(分)包- a中import a.b.c-被送到find_module一个None路径.无论返回什么路径,都会进入find_module查找循环中的下一个传递,您将尝试b在上一个示例中找到该路径.
来自logilab.common.modutils的伪代码:
path = None
while import_as_list:
try:
_, found_path, etc = find_module(import_as_list[0], path)
#exception handling and checking for a better version in the .egg files
path = [found_path]
import_as_list.pop(0)
Run Code Online (Sandbox Code Playgroud)
这就是被打破的:你只能得到第一个最好的命中find_module,可能有也可能没有你的子包.如果您没有找到子包,则无法退出并尝试下一个.
我尝试显式使用sys.path而不是None,这样结果可以从路径列表中删除并进行第二次尝试,但是python的模块查找器非常聪明,不必在路径中完全匹配,无论如何,使这种方法无法使用 - 尽我所知.
是否有一个替代find_modules将返回所有可能的匹配或采取排除列表?我也对完全不同的解决方案持开放态度.最好不要手工修补python,但这不是不可能 - 至少对于本地解决方案.
(注意事项:我正在运行python 2.6并且由于当前公司政策的原因无法升级,对p3k等的建议不会被标记为已被接受,除非它是唯一的答案.)
我想setup.py使用 写一个文件setuptools。我的包依赖于tensorflow,但是有两个不同的 pip 包可以满足要求,tensorflow和tensorflow-gpu。如果我只是输入tensorflowmy setup(..., install_requires=["tensorflow"]),那么如果用户tensorflow-gpu在其系统上安装了 pip 软件包,安装将会失败。
该imp模块不能用于检查(如本答案中:How to check if a python module isn’t importing it),因为模块的导入tensorflow名称与用户安装的pip 包无关。那么 setuptools(以及 distutils)如何检测安装了哪个 pip 包?我已经对源代码进行了一些挖掘,但找不到它检查的地方。
*注意,我不打算破解安装工具来接受其中任何一个。我只想知道它使用什么方法来检测包,因此我可以在 my 中使用相同的方法setup.py手动将 install_requires 参数设置为正确的版本。(即像这样:setup.py 中的替代依赖项(回退))
python ×7
importerror ×2
distutils ×1
django ×1
namespaces ×1
package ×1
pandas ×1
pycharm ×1
pylint ×1
python-2.7 ×1
python-3.x ×1
setuptools ×1
skype ×1
tensorflow ×1
xlsxwriter ×1