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

cannot import name properties #10

Closed
ldiqual opened this issue Aug 22, 2012 · 16 comments
Closed

cannot import name properties #10

ldiqual opened this issue Aug 22, 2012 · 16 comments

Comments

@ldiqual
Copy link

ldiqual commented Aug 22, 2012

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
Copy link
Member

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
Copy link
Author

ldiqual commented Aug 22, 2012

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
Copy link
Member

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
Copy link
Author

ldiqual commented Aug 22, 2012

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
Copy link
Member

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
Copy link
Author

ldiqual commented Aug 22, 2012

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
Copy link
Author

ldiqual commented Aug 22, 2012

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
Copy link
Member

'/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
Copy link
Member

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
Copy link
Member

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

@marianobianchi
Copy link

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
Copy link
Member

@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.

@marianobianchi
Copy link

I know it is closed but after one year i run into the same problem again. I installed pycparser and everything seems to work fine again.

@SimonSapin
Copy link
Member

@marianobianchi Was pycparser not already installed as a (recursive) dependency?

@marianobianchi
Copy link

I was having another problem and was installing all packages with "--no-deps" option in pip. That made me run into this problem again and found this old bug report. I thought it was a good idea to comment the solution that worked for me this time, but wasn't sure if this was the correct place to do it.

I have just installed using "pip install weasyprint" and it installed all the dependencies fine. So i think it's nothing to worry about...

@SimonSapin
Copy link
Member

Although the error message could definitely be better, I’m not too surprised that installing without dependencies doesn’t work.

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

No branches or pull requests

3 participants