为什么我的 Python 代码可以在 Jupyter Notebook 中运行,但不能作为脚本运行?

Why does my Python code work in Jupyter Notebook but not as a script?

我正在尝试使用 Wand 打开 PDF 作为图像。如果我在 Jupyter Notebook 中 运行 下面的代码,它工作正常。如果我 运行 命令提示符中的代码作为脚本,我会收到一条错误消息。出于某种原因,模块在 Jupyter Notebook 之外 运行 时不会加载。有没有人有什么建议?

Windows

Python 3.7.2(64 位)

from wand.image import Image

with Image(filename="C:/test.pdf", resolution=300) as img:
    print(type(img))

这是错误信息:

Traceback (most recent call last):
  File "C:\Untitled.py", line 7, in <module>
    from wand.image import Image
  File "C:\Users\spencer.rand\AppData\Local\Programs\Python\Python37\lib\site-packages\wand\image.py", line 18, in <module>
    from . import compat
  File "C:\Users\spencer.rand\AppData\Local\Programs\Python\Python37\lib\site-packages\wand\compat.py", line 25, in <module>
    abc = collections.abc if PY3 else collections
  File "C:\Users\spencer.rand\AppData\Local\Programs\Python\Python37\lib\collections\__init__.py", line 55, in __getattr__
    raise AttributeError(f'module {__name__!r} has no attribute {name!r}')
AttributeError: module 'collections' has no attribute 'abc'

魔杖和 collections 似乎有问题,已于 2 周前修复:https://github.com/emcconville/wand/pull/398

尝试更新您的魔杖安装:pip install --upgrade wand