Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

cannot import name properties #10

Closed
ldiqual opened this Issue · 12 comments

3 participants

@ldiqual

I'm trying to use WeasyPrint in my virtualenv. I followed the Homebrew steps from the installation instructions, but I get an error while trying to generate a simple PDF:

Traceback (most recent call last):
  File "/Users/ldiqual/Documents/Projects/app-django/app_env/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/ldiqual/Documents/Projects/app-django/app_env/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 77, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/ldiqual/Documents/Projects/app-django/backend/views/admin/actions.py", line 87, in handle_admin_actions
    return generate_contract(request, vendor)
  File "/Users/ldiqual/Documents/Projects/app-django/backend/utils/contract_generator.py", line 28, in generate_contract
    pdf = render_to_pdf(request, "backend/steps/contract/contract.html", context_dict)
  File "/Users/ldiqual/Documents/Projects/app-django/backend/utils/common.py", line 61, in render_to_pdf
    weasyprint.HTML(string=html).write_pdf(target=result)
  File "/Users/ldiqual/Documents/Projects/app-django/app_env/lib/python2.7/site-packages/weasyprint/__init__.py", line 98, in __init__
    from .html import find_base_url
  File "/Users/ldiqual/Documents/Projects/app-django/app_env/lib/python2.7/site-packages/weasyprint/html.py", line 22, in <module>
    from .formatting_structure import boxes
  File "/Users/ldiqual/Documents/Projects/app-django/app_env/lib/python2.7/site-packages/weasyprint/formatting_structure/boxes.py", line 63, in <module>
    from ..css.computed_values import ZERO_PIXELS
  File "/Users/ldiqual/Documents/Projects/app-django/app_env/lib/python2.7/site-packages/weasyprint/css/__init__.py", line 51, in <module>
    from . import properties
ImportError: cannot import name properties

The error comes from weasyprint/css/__init__.py:

import tinycss
import cssselect
import lxml.etree

from . import properties # <=== This line
from . import computed_values
from .validation import preprocess_declarations
@SimonSapin
Owner

The properties.py file has been there since forever, so the problem is most likely elsewhere. Is this the whole output? Are you using the same Python as the one Homebrew installed pygtk in? Did you create the virtualenv with --system-site-packages? Can you import pango from a Python shell? What about import lxml.etree?

@ldiqual

Is this the whole output?

No, but everything else is related to Django. I noticed something else: when I load my django view for the first time (after starting the dev server), I get a ImportError: No module named gi.repository. Then, when I reload the page, it is replaced by ImportError: cannot import name properties.

Are you using the same Python as the one Homebrew installed pygtk in?

I don't think so, as $ which python outputs /usr/bin/python (v2.7.2). Do I have to use the Homebrew's python formula ?
I also followed the last step from Homebrew:

For non-Homebrew Python, you need to amend your PYTHONPATH like so:
  export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH

Did you create the virtualenv with --system-site-packages? Can you import pango from a Python shell? What about import lxml.etree?

Yes, yes, and yes. I double checked that.

@SimonSapin
Owner

when I load my django view for the first time (after starting the dev server), I get a ImportError: No module named gi.repository

This is it. WeasyPrint fails to import Pango, either from gi.repository.Pango through PyGObject3-introspection or from just pango with PyGTK.

I really don’t know how Homebrew handles Python and Python libs. How do you start Django? Can you import pango with the same Python that Django uses?

@ldiqual

Ok, you pointed out the right issue (I was importing cairo in my last test). Using ./manage.py shell, I get the same python environment than Django:

$ import pango
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named pango

$ import pygtk
# Seems to work

In /usr/local/lib/python2.7/site-packages (my PYTHONPATH):

$ find . -iname "*pango*"
./gtk-2.0/pango.so
./gtk-2.0/pangocairo.so

Is a pango.py needed ?

@SimonSapin
Owner

There is no pango.py, pango.so imports as a Python module. /usr/local/lib/python2.7/site-packages/gtk-2.0 should be in sys.path. On my system this happens because /usr/lib/python2.7/site-packages/pygtk.pth contains a path to the gtk-2.0 directory. Do you have something similar, maybe in another .pth file? If not try adding it. (But it should have been there…)

@ldiqual

I have the same pygtk.pth file containing gtk-2.0. Plus, sys.path seems to be correct:

>>> import sys
>>> sys.path
[..., '/usr/local/lib/python2.7/site-packages', ...]

$ ls /usr/local/lib/python2.7/site-packages
...
gtk-2.0
pygtk.pth
pygtk.py
pygtk.pyc
pygtk.pyo
...

Any thoughts ?

@ldiqual

I finally got it working ! Analyzing this PyGTK OSX package, I noticed that the postinstall script was adding the install path to /Library/Python/2.7/site-packages/gtkredirect.pth. So I modified it this way:

# in /Library/Python/2.7/site-packages/gtkredirect.pth
import site;
site.addsitedir('/usr/local/lib/python2.7/site-packages')

I think that brew just assume python to be installed with a brew package.
Thanks for your help Simon, you led me on the right way :)

Could you please add this step to the installation tutorial ?

@SimonSapin
Owner

'/usr/local/lib/python2.7/site-packages/gtk-2.0' needs to be in sys.path, no just '/usr/local/lib/python2.7/site-packages'. Did the .pth file exist before you set it to the above?

I’m not sure what happened on your system, but in any case WeasyPrint’s documentation is not the place to fix Homebrew’s PyGTK formula. I see that it has a warning about "non-Homebrew Python":
https://github.com/mxcl/homebrew/blob/master/Library/Formula/pygtk.rb#L24

Does that help, as an alternative to your fix?

@SimonSapin
Owner

If you undo these changes to your PyGTK install and got back to the default, does 35540be fix the issue? (It’s in git master.)

@SimonSapin
Owner

Closing, as I believe that 35540be fixed the issue.
Related: http://faq.pygtk.org/index.py?req=show&file=faq02.004.htp

@SimonSapin SimonSapin closed this
@marianobianchi

I had this problem today while i was upgrading my django code on a server. The problem was that i didn't have pango1.0-dev installed on my system. I installed that library and everything works well now.

@SimonSapin
Owner

@marianobianchi I think yours was not exactly the same problem, since we’re not using PyGTK anymore. But thanks for sharing, I’ll remember to check the dependencies when debugging similar problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.