简体   繁体   English

runserver 命令的 PyCharm 问题

[英]PyCharm issue with runserver command

I have a script, part of a Django app, that makes thumbnails from pdfs.我有一个脚本,它是 Django 应用程序的一部分,可以从 pdf 生成缩略图。 I'm using the excellent wand package to do it: Here are the Wand docs我正在使用优秀的魔杖包来做到这一点:这里是魔杖文档

It runs fine if I do ./manage.py runserver from the command line, but if I run from PyCharm, it breaks.如果我./manage.py runserver执行./manage.py runserver ,它运行良好,但如果我从 PyCharm 运行,它会中断。

When I step through the code, the problem is that the code used to open the blob always returns an empty wand.image object.当我逐步执行代码时,问题是用于打开 blob 的代码总是返回一个空的wand.image对象。 It's the right class ( wand.image ) but it's an empty one.这是正确的类( wand.image ),但它是一个空类。 The object I pass it is a pdf, but the blob conversion, which produces no error at all, is empty.我传递给它的对象是一个 pdf,但是完全没有错误的 blob 转换是空的。

The error occurs in the next line ( single_image = all_pages.sequence[0] ) because all_pages is empty, so the index is out of range.错误发生在下一行( single_image = all_pages.sequence[0] ),因为all_pages为空,因此索引超出范围。

Again, if I launch the server from the command line, it works, but if I launch from PyCharm, it breaks.同样,如果我从命令行启动服务器,它可以工作,但如果我从 PyCharm 启动,它就会中断。

I'm using virtualenv.我正在使用 virtualenv。

Here's the code I'm running:这是我正在运行的代码:

from wand.image import Image as WandImage
from wand.color import Color


def convert_to_thumb(pdf_path, slug):
    with open(pdf_path) as f:
        image_binary = f.read()
    all_pages = WandImage(blob=image_binary) #<-- Here image_binary is a pdf
    single_image = all_pages.sequence[0]     #<-- BOOM! all_pages is a wand.image, but it's empty. Gives an Index error
    with WandImage(single_image) as i:
        i.format = 'png'
        i.background_color = Color('white')  
        i.alpha_channel = 'remove'  
        i.transform(resize='x100')
        save_name = slug + '_pdf_preview.png'
        i.save(filename='/foo/bar/' + save_name)

        return i

EDIT: Here's some Debug info编辑:这是一些调试信息

When I run from CLI and use pdb.set_trace() to check the value of all_pages I get this当我从CLI和使用运行pdb.set_trace()来检查的价值all_pages我得到这个

(Pdb) p all_pages
<wand.image.Image: 3da0549 'PDF' (612x792)>

But when I do the same thing from the PyCharm console, I get:但是当我从 PyCharm 控制台做同样的事情时,我得到:

(Pdb) >? p all_pages
<wand.image.Image: (empty)>

The value of image_binary appears to be identical in both cases. image_binary的值在两种情况下似乎都相同。 I diffed them.我区别了他们。

Furthermore, the value of libmagick (the ImageMagick install) is /usr/local/lib/libMagickWand.dylib in both cases.此外,在这两种情况下, libmagick (ImageMagick 安装)的值都是/usr/local/lib/libMagickWand.dylib

EDIT: This is interesting.编辑:这很有趣。 If I launch PyCharm from the system Terminal, it works fine.如果我从系统终端启动 PyCharm,它工作正常。

EDIT: Added relevant run configuration settings编辑:添加了相关的运行配置设置

<configuration default="false" name="foo_bar_app" type="Python.DjangoServer" factoryName="Django server">
  <option name="INTERPRETER_OPTIONS" value="" />
  <option name="PARENT_ENVS" value="true" />
  <envs>
    <env name="PYTHONUNBUFFERED" value="1" />
    <env name="FOO_USERID" value="foobar" />
    <env name="DJANGO_SETTINGS_MODULE" value="foo_bar.settings" />
  </envs>
  <option name="SDK_HOME" value="$USER_HOME$/venv/foo_bar/bin/python" />
  <option name="WORKING_DIRECTORY" value="" />
  <option name="IS_MODULE_SDK" value="false" />
  <option name="ADD_CONTENT_ROOTS" value="true" />
  <option name="ADD_SOURCE_ROOTS" value="true" />
  <module name="foo_bar_app" />
  <option name="launchJavascriptDebuger" value="false" />
  <option name="port" value="8000" />
  <option name="host" value="" />
  <option name="additionalOptions" value="" />
  <option name="browserUrl" value="" />
  <option name="runTestServer" value="false" />
  <option name="runNoReload" value="false" />
  <option name="useCustomRunCommand" value="false" />
  <option name="customRunCommand" value="" />
  <RunnerSettings RunnerId="PyDebugRunner" />
  <RunnerSettings RunnerId="PythonCover" />
  <RunnerSettings RunnerId="PythonRunner" />
  <ConfigurationWrapper RunnerId="PyDebugRunner" />
  <ConfigurationWrapper RunnerId="PythonCover" />
  <ConfigurationWrapper RunnerId="PythonRunner" />
  <method />
</configuration>

EDIT: This is interesting.编辑:这很有趣。 If I launch PyCharm from the system Terminal, it works fine.如果我从系统终端启动 PyCharm,它工作正常。

Sounds like you are running PyCharm with different user permissions, when launching from the terminal.从终端启动时,听起来您正在以不同的用户权限运行 PyCharm。

i got this problem few month ago.几个月前我遇到了这个问题。 The only way i found to fix it was to launch everything in admin mode.我发现修复它的唯一方法是在管理员模式下启动所有内容。 It's probably a permissions denied problem from system.这可能是系统的权限被拒绝问题。

If you are referencing an image with a relative path this may be the expected behavior--an empty image--as the working directory will be different between terminal instances. 如果使用相对路径引用图像,则可能是预期的行为-空图像-因为终端实例之间的工作目录会有所不同。 Replace your file paths with absolute paths in your code. 用代码中的绝对路径替换文件路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM