如何修复 python/pycharm [macOS] 中的 Ghostscript 路径?

问题描述 投票:0回答:1

作为更大程序的一部分,我尝试运行以下代码,使用 Ghostscript 和 pyhton 的 PIL 库从基于 tkinter-canvas 的动画中保存 gif:

images = []  #initialize blank queue to later save each frame of gif


def frame_save():
    # workaround function to save individual frame of tkinter-based animation to queue to later render/save as gif
    win.postscript(file="image.eps", colormode='color')
    img = NewImage.open("image.eps")
    images.append(img)


def save_gif(len):
    # function to render and save image queue as gif
    now = datetime.now()
    now_str = now.strftime("%d/%m/%Y %H:%M:%S")
    filename = f'SOLDR-L{survive_low}H{survive_high}E{exp}_{now_str}'
    images[0].save(f'{filename}.gif', save_all=True, append_images=images[1:], optimize=False, duration=len*2, loop=0)

但是,我收到与上面最后一行代码和 Popen(*popenargs, **kwargs) 中的 Ghostscript 的 subprocess.py 相关的错误:

FileNotFoundError: [Errno 2] No such file or directory: 'gs'

从终端或pycharm的shell调用时,gs命令会运行,表明ghostscript已成功安装。在终端中运行“which gs”返回路径“/opt/homebrew/bin/gs”。我尝试将以下行添加到代码的开头来修复 PATH:

os.environ["PATH"] += ":/opt/homebrew/bin/gs"

但是,我仍然得到与以前相同的错误回溯。我还尝试了网上建议的一些终端命令方法来修复 PATH,但没有效果。我是编程初学者,对 macOS 终端命令等的了解非常有限,所以任何帮助将不胜感激。

谢谢!

python-3.x macos pycharm python-imaging-library
1个回答
0
投票

只是将我的评论作为答案,以便其他人可以看到它,而无需浏览评论......

您需要将路径添加到包含目录,而不是实际的可执行文件本身,尝试:

os.environ["PATH"] += ":/opt/homebrew/bin
© www.soinside.com 2019 - 2024. All rights reserved.