Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OS Error when importing #4

Closed
jmbaker94 opened this issue Feb 5, 2019 · 5 comments · Fixed by #10
Closed

OS Error when importing #4

jmbaker94 opened this issue Feb 5, 2019 · 5 comments · Fixed by #10
Assignees

Comments

@jmbaker94
Copy link

I just installed this package. When importing as in your example I get the following error:

OSError                                   Traceback (most recent call last)
<ipython-input-1-18b5d834a3c4> in <module>()
----> 1 import drawSvg as draw

~/anaconda3/lib/python3.6/site-packages/drawSvg/__init__.py in <module>()
     46 
     47 from .defs import *
---> 48 from .raster import Raster
     49 from .drawing import Drawing
     50 from .elements import *

~/anaconda3/lib/python3.6/site-packages/drawSvg/raster.py in <module>()
      3 
      4 try:
----> 5     import cairosvg
      6 except ImportError:
      7     import warnings

~/anaconda3/lib/python3.6/site-packages/cairosvg/__init__.py in <module>()
     39 
     40 # VERSION is used in the "url" module imported by "surface"
---> 41 from . import surface  # noqa
     42 
     43 

~/anaconda3/lib/python3.6/site-packages/cairosvg/surface.py in <module>()
     22 import io
     23 
---> 24 import cairocffi as cairo
     25 
     26 from .colors import color

~/anaconda3/lib/python3.6/site-packages/cairocffi/__init__.py in <module>()
     39 
     40 
---> 41 cairo = dlopen(ffi, 'cairo', 'cairo-2', 'cairo-gobject-2')
     42 
     43 

~/anaconda3/lib/python3.6/site-packages/cairocffi/__init__.py in dlopen(ffi, *names)
     36             except OSError:
     37                 pass
---> 38     raise OSError("dlopen() failed to load a library: %s" % ' / '.join(names))
     39 
     40 

OSError: dlopen() failed to load a library: cairo / cairo-2 / cairo-gobject-2

I've also attempted to fix by explicitly pip installing cairosvg with no change in the error.

@cduck
Copy link
Owner

cduck commented Feb 5, 2019

This is mainly a problem with CairoSVG. I'm not sure why it's failing to load.

drawSvg should have just disabled the image rasterization functionality when Cairo failed to load. I've updated the library do this.

@cduck cduck closed this as completed Feb 5, 2019
@jpach01
Copy link

jpach01 commented Oct 7, 2019

TL;DR: CairoSVG not installed is not always the case when the error message shows when rendering and the warning after importing drawSVG:

https://github.com/cduck/drawSvg/blob/5d6177a51c430e644bb4b714216977a1af92e29e/drawSvg/raster.py#L9

RuntimeWarning: CairoSVG will need to be installed to rasterize images: Install with `pip3 install cairosvg`
  warnings.warn(msg, RuntimeWarning)

The error message provided when an OSError/ImportError occurs is misleading. It happened to me after installing CairoSVG for about 10 times without being able to fix it. I went on and I posted an issue in CairoSVG's repository (which I link here) and they were able to explain that CairoSVG's installation was not the source of the problem I was having when I tried to render my svg files. I encourage you to read the issue I've linked and read the replies because they're able to explain much better what the problem is that I am.
Essentially, because Cairo itself is not something that gets installed with Python or as a python package, but rather, a different thing that you can have Python interacting with, I didn't realise that that was the problem until I read the response to my issue. They suggested creating an issue for this here so that's why I'm writing this. Basically, the messages when an ImportError and an OSError need to be distinct and more helpful, plus (I don't really know how the following works) retain the stack trace of errors from higher levels if possible.

@cduck
Copy link
Owner

cduck commented Oct 7, 2019

Thanks for pointing out the misleading error message. It looks like there should be separate except blocks for ImportError and OSError each with different error messages. Also, I think the readme should say something about how to optionally install Cairo and CairoSVG.

I'll make sure this is fixed by the next release. Do you want to take a shot at fixing this and submit a pull request?

@cduck cduck reopened this Oct 7, 2019
@bonicim
Copy link
Contributor

bonicim commented Oct 10, 2019

What happened?

This is my first time using drawSvg. I followed the project description to get started but ran into the same error message that jpach01 saw.

Steps to Reproduce

Operating system: macOS Mojave (10.14.6)
Python Version: 3.7.4 (default, July 8, 2019)
I'm using a virtual environment.
I've installed cairosvg: pip install cairosvg
Note: cairo was not installed

  1. Enter jupyter notebook on terminal
  2. Open jupyter url link in browser, open a new notebook and run the following:
import drawSvg as draw

d = draw.Drawing(200, 100, origin='center')
  1. The following error message appears:
Python-Virtual-Environments/ipython_env/lib/python3.7/site-packages/drawSvg/raster.py:11: RuntimeWarning: CairoSVG will need to be installed to rasterize images: Install with `pip3 install cairosvg`
  warnings.warn(msg, RuntimeWarning)

Detailed Description

After reading jpach01's issue on CairoSvg, I installed cairo on my macOS using brew install cairo. I then tried drawing the example image on the project description and it worked. I agree with cduck on updating the README. I'm going to take a shot at trying to fix this.

@cduck
Copy link
Owner

cduck commented Oct 14, 2019

This is fixed in version 1.3.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants