I'm building a Django management command which is creating website screenshots using Paul Hammond's webkit2png script (http://www.paulhammond.org/webkit2png/) and stores them into my DB.
我正在构建一个Django管理命令,该命令使用Paul Hammond的webkit2png脚本(http://www.paulhammond.org/webkit2png/)创建网站截图,并将它们存储到我的DB中。
For this command I'm using the 'call' command from 'subprocess'. How do I execute this command in specific directory (temp/ under django project in this case)? My current code looks like this but it doesn't find the script to execute which is stored in my virtualenv site-packages folder:
对于这个命令,我使用“子进程”的“调用”命令。如何在特定目录(在本例中为django项目下的temp/)中执行此命令?我当前的代码看起来是这样的,但是它并没有找到要执行的脚本,它存储在我的virtualenv站点-packages文件夹中:
import os
from django.core.management.base import NoArgsCommand
from django.conf import settings
from subprocess import call
# Models
from reviews.models import Shop
class Command(NoArgsCommand):
def handle_noargs(self, **options):
# Shops
shops = Shop.objects.all()
path = os.path.join(settings.SITE_ROOT, '../env/lib/python2.6/site-packages')
for shop in shops:
print shop
command = "cd temp; python %s/webkit2png.py -F %s" % (path, shop.url)
call([command])
# Read the screenshot file and insert to model's ImageField
2 个解决方案
#1
2
You need to use the cwd
parameter to call
. Also I'd recommend normalizing the path before using it.
您需要使用cwd参数调用。我还建议在使用路径之前对路径进行规范化。
path = os.path.normpath(os.path.join(settings.SITE_ROOT,
'../env/lib/python2.6/site-packages'))
for shop in shops:
print shop
call(["python", path + "/webkit2png.py", "-F", shop.url], cwd="temp")
# Read the screenshot file and insert to model's ImageField
call
takes the same arguments as Popen. You might find some more things there that help out as well. Also, it's best to split your command-line tokens up as separate strings in the list passed to call
and leave the shell
parameter at its default False
. This way, you don't have to worry about shell escaping, quoting, or whatever messing up your parameters.
调用使用与Popen相同的参数。你可能会在那里找到更多的帮助。此外,最好将命令行令牌分割为用于调用的列表中的独立字符串,并将shell参数保留为默认的False。通过这种方式,您不必担心shell转义、引用或其他什么会打乱参数。
#2
0
This does not answer your question directly but : why do you need to use subprocess to call a python script ? You could just look at the "__main__
" code in the webkit2png.py file, import
it and use it.
这并不能直接回答您的问题,但是:为什么需要使用subprocess来调用python脚本?您可以查看webkit2png中的“__main__”代码。py文件,导入并使用它。
#1
2
You need to use the cwd
parameter to call
. Also I'd recommend normalizing the path before using it.
您需要使用cwd参数调用。我还建议在使用路径之前对路径进行规范化。
path = os.path.normpath(os.path.join(settings.SITE_ROOT,
'../env/lib/python2.6/site-packages'))
for shop in shops:
print shop
call(["python", path + "/webkit2png.py", "-F", shop.url], cwd="temp")
# Read the screenshot file and insert to model's ImageField
call
takes the same arguments as Popen. You might find some more things there that help out as well. Also, it's best to split your command-line tokens up as separate strings in the list passed to call
and leave the shell
parameter at its default False
. This way, you don't have to worry about shell escaping, quoting, or whatever messing up your parameters.
调用使用与Popen相同的参数。你可能会在那里找到更多的帮助。此外,最好将命令行令牌分割为用于调用的列表中的独立字符串,并将shell参数保留为默认的False。通过这种方式,您不必担心shell转义、引用或其他什么会打乱参数。
#2
0
This does not answer your question directly but : why do you need to use subprocess to call a python script ? You could just look at the "__main__
" code in the webkit2png.py file, import
it and use it.
这并不能直接回答您的问题,但是:为什么需要使用subprocess来调用python脚本?您可以查看webkit2png中的“__main__”代码。py文件,导入并使用它。