gpt4 book ai didi

python - Django:为什么当我在 django 中通过 popen 使用 Ghostscript 时,会出现 "file not found"错误

转载 作者:行者123 更新时间:2023-12-01 05:56:56 24 4
gpt4 key购买 nike

我正在尝试使用 Ghostscript 从 Django 内部将 pdf 转换为 jpg,但是当我尝试使用 subprocess.popen 运行 Ghostscript 命令时,出现“找不到文件”错误。如果我通过命令行从 py 文件运行完全相同的脚本,它可以正常工作。

这是我的脚本:

import os
import subprocess

path = "/tmp/e15hntd/"

### this next line is what seems to cause the file not found error

sp = subprocess.Popen("gs -dSAFER -dNOPAUSE -dQUIET -dBATCH -sDEVICE=jpeg -sOUTPUTFILE=" + path + "/static.jpg " + path + "/source.pdf")

在寻找可能的解决方案后,我尝试了有问题的行的几个替代版本,例如:

sp = subprocess.Popen(['gs', '-dSAFER', '-dNOPAUSE', '-dQUIET', '-dBATCH', '-sDEVICE=jpeg', '-sOUTPUTFILE=' + path + "/static.jpg", path + "/source.pdf"])

或者:

sp = subprocess.call("gs -dSAFER -dNOPAUSE -dQUIET -dBATCH -sDEVICE=jpeg -sOUTPUTFILE=" + path + "/static.jpg " + path + "/source.pdf")

这是完整的错误:

[Errno 2]没有这样的文件或目录

回溯:

File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/options.py" in wrapper
366. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/sites.py" in inner
196. return view(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in _wrapper
25. return bound_func(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in bound_func
21. return func(self, *args2, **kwargs2)
File "/usr/local/lib/python2.6/dist-packages/django/db/transaction.py" in inner
209. return func(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/options.py" in change_view
1054. self.save_model(request, new_object, form, True)
File "/home/Django/Update/admin.py" in save_model
41. sp = subprocess.call("gs -dSAFER -dNOPAUSE -dQUIET -dBATCH -sDEVICE=jpeg -sOUTPUTFILE=" + unique_path + "/static.jpg " + unique_path + "/source.pdf" + unique_path + "/source.pdf")
File "/usr/lib/python2.6/subprocess.py" in call
480. return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.6/subprocess.py" in __init__
633. errread, errwrite)
File "/usr/lib/python2.6/subprocess.py" in _execute_child
1139. raise child_exception

最佳答案

您收到的错误意味着subprocess模块找不到它想要运行的可执行文件。

如果脚本从命令行运行,则 gs 可执行文件显然存在,因此看起来 gs 可执行文件不在 PATH 您的 Django 应用程序正在运行。如果您指定 gs 的完整路径(例如 /usr/bin/gs),会有帮助吗?

顺便说一句,如果我尝试让 subprocess 启动不存在的可执行文件,我会收到相同的错误消息:

Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call("nonexistent")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

编辑:如果您的进程使用命令行参数,则必须将可执行文件名称和参数指定为单独的列表项,如第二次尝试运行 gs:

Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call("ls /usr")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
>>> subprocess.call(["ls", "/usr"])
bin games include lib lib32 lib64 local sbin share src X11R6
0

第一个示例不起作用,因为我系统上的任何位置都没有名为 ls/usr 的可执行文件。

您能否尝试修改第二次尝试以使用 gs 的完整路径,例如:

sp = subprocess.Popen(['/usr/bin/gs', '-dSAFER', '-dNOPAUSE', '-dQUIET', '-dBATCH', '-sDEVICE=jpeg', '-sOUTPUTFILE=' + path + "/static.jpg", path + "/source.pdf"])

关于python - Django:为什么当我在 django 中通过 popen 使用 Ghostscript 时,会出现 "file not found"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12025743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com