10

I have a script, part of a Django app, that makes thumbnails from pdfs. 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.

When I step through the code, the problem is that the code used to open the blob always returns an empty wand.image object. It's the right class (wand.image) but it's an empty one. The object I pass it is a pdf, but the blob conversion, which produces no error at all, is empty.

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.

Again, if I launch the server from the command line, it works, but if I launch from PyCharm, it breaks.

I'm using 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

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

But when I do the same thing from the PyCharm console, I get:

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

The value of image_binary appears to be identical in both cases. I diffed them.

Furthermore, the value of libmagick (the ImageMagick install) is /usr/local/lib/libMagickWand.dylib in both cases.

EDIT: This is interesting. If I launch PyCharm from the system Terminal, it works fine.

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>
Rob L
  • 3,230
  • 2
  • 15
  • 36
  • How is it being called? – GRAYgoose124 Jan 26 '17 at 21:35
  • From `views.py`: `pdf_thumb = convert_to_thumb(pdf_path, pub.slug)` – Rob L Jan 26 '17 at 21:36
  • 3
    I would suggest to compare environment variables in PyCharm and cli – Marat Jan 26 '17 at 21:46
  • @Marat, yeah, I tried that. Unless there are environment variables hidden somewhere in PyCharm (i.e., not in the 'edit configurations' panel), then that's not it. The environment variables are the same. – Rob L Jan 26 '17 at 21:50
  • @RobL can you please share your run config in PyCharm and config of Django Framework Integration page in Pycharm settings? – s_mart Jan 26 '17 at 23:06
  • @s_mart I assume you mean run config from the .idea file? I'm not sure what you mean from the "Django Framework Integration page." I'll have a look when I get back to work tomorrow morning (UTC -5) – Rob L Jan 27 '17 at 00:03
  • Added some more info to the original question, Thanks!!! – Rob L Jan 27 '17 at 17:51
  • Have you made sure that you've defined the working directory in the server configuration window? – Daniel van Flymen Feb 02 '17 at 02:48
  • @DanielvanFlymen yes. Way past that. – Rob L Feb 02 '17 at 14:40
  • 1
    I would suggest inspecting a couple of values in pdb, e.g. `sys.executable`, `os.environ`, `wand.image.__file__`. You can also try loading the image differently, e.g. WandImage(filename=pdf_path) and see if that raises a more conclusive error. – Daniel Hepper Feb 03 '17 at 09:37
  • `WandImage(filename=pdf_path)` doesn't raise an error (they appear to be caught by `Wand`). the `sys`, `os` and `wand` parameters are precisely the same in both cases. – Rob L Feb 03 '17 at 20:40

2 Answers2

1

EDIT: This is interesting. If I launch PyCharm from the system Terminal, it works fine.

Sounds like you are running PyCharm with different user permissions, when launching from the terminal.

0

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.

Valentin Garreau
  • 581
  • 1
  • 4
  • 27