Using external lxml library

Asked by Guillaume Mandil on 2011-11-18

I'm running Inkscape 0.48.2 r9819.
My computer is an iMac running OSX 10.6.8

I've install Python 2.6 through MacPorts.
  edmonde:~ gmandil$ which python
  /opt/local/bin/python
Just to be sure about the version :
  edmonde:~ gmandil$ python version
  /opt/local/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python: can't open file 'version': [Errno 2] No such file or directory

Then testing the lxml class :

edmonde:~ gmandil$ python
Python 2.6.7 (r267:88850, Nov 5 2011, 20:04:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from lxml import etree
>>> print("running with lxml.etree")
running with lxml.etree
>>> root = etree.Element("root")
>>> root.append( etree.Element("child1") )
>>> child2 = etree.SubElement(root, "child2")
>>> print(etree.tostring(root, pretty_print=True))
<root>
  <child1/>
  <child2/>
</root>

>>> exit()

But when I try to run some inkscape extension I get the boring message :
"La fantastique classe lxml pour libxml2 est nécessaire à inkex.py et par conséquent à cette extension. Veuillez en télécharger et installer la dernière version à partir du site http://cheeseshop.python.org/pypi/lxml/, ou l’installer directement avec votre gestionnaire de paquet avec une commande du type : sudo apt-get install python-lxml"

As I've read some issues (https://answers.launchpad.net/inkscape/+question/126652) about the python contained in the inkscape.app package, I've renamed the folder /Applications/Inkscape.app/Contents/Resources/python to /Applications/Inkscape.app/Contents/Resources/python.old

Does anyone can tell me how could i make inkscape use the Macports lxml version that is installed on my system ??

Thanks in advance
Guillaume

Question information

Language:
English Edit question
Status:
Solved
For:
Inkscape Edit question
Assignee:
No assignee Edit question
Solved by:
su_v
Solved:
2011-11-24
Last query:
2011-11-24
Last reply:
2011-11-21
su_v (suv-lp) said : #1

With Inkscape 0.48.x, Inkscape.app on Mac OS X defaults to the system python version and does not use python as installed in MacPorts.

To make Inkscape.app use python from MacPorts, you need to edit the launcher script:
 Inkscape.app/Contents/Resources/bin/inkscape

1) Comment out line 28:
  this line inserts '/usr/bin' at the start of $PATH, which makes inkscape-bin always launch the system python
2) Comment out line 38:
  this line sets PYTHONPATH to point to the modules bundled with Inkscape.app

Commenting out in a shell script is done by inserting a '#' (without the quotes) at the beginning of the line.

su_v (suv-lp) said : #2

> Just to be sure about the version :
> edmonde:~ gmandil$ python version
> /opt/local/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python: can't open file 'version': [Errno 2] No such file or directory

To test the python version properly, use:
$ python --version

Guillaume Mandil (gmandil-x) said : #3

Hi ~suv,

Thank you for the answer with nearly solved my problem.

With your help, I'm now able to see may extensions windows. But a problem remain. All the chatacters in these windows are remplaced by a square character like this one : ☐

Another thing that can help for assisting me is that before asking for help here, I've tried commands such as (executed as root) :

# export STATIC_DEPS=true
# easy_install --allow-hosts=lxml.de,*.python.org lxml

thanks again

Guillaume

PS : thank you also for the tip about the python version ;-).

Guillaume Mandil (gmandil-x) said : #4

For more efficient Help, here are the infos furnished by your debug.py extension found in the <https://bugs.launchpad.net/inkscape/+bug/482993> bug.

Default Encoding: ascii
----------------------------------------
Filesystem Encoding: utf-8
----------------------------------------
Platform: darwin
----------------------------------------
Python version: 2.7.2
Python version:
2.7.2 (default, Nov 5 2011, 20:09:20)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
----------------------------------------
Python executable:
 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python exec_prefix:
 /opt/local/Library/Frameworks/Python.framework/Versions/2.7
Python prefix:
 /opt/local/Library/Frameworks/Python.framework/Versions/2.7
----------------------------------------
lxml path: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml
LXML_VERSION: 2.3.0.0
LIBXML_VERSION: 2.7.8
LIBXML_COMPILED_VERSION: 2.7.8
LIBXSLT_VERSION: 1.1.26
LIBXSLT_COMPILED_VERSION: 1.1.26
----------------------------------------
numpy path: /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy
numpy version: 1.6.1
----------------------------------------
sys.path:
 /Users/gmandil/.config/inkscape/extensions
 /Applications/Inkscape.app/Contents/Resources/extensions
 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gtk-2.0
 /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info
----------------------------------------
os.environ: {'FONTCONFIG_PATH': '/Applications/Inkscape.app/Contents/Resources/etc/fonts', 'INKSCAPE_SHAREDIR': '/Applications/Inkscape.app/Contents/Resources', 'ASPELL_CONF': 'prefix /Applications/Inkscape.app/Contents/Resources;', 'VERSIONER_PYTHON_PREFER_32_BIT': 'yes', 'SHELL': '/bin/bash', 'GTK_EXE_PREFIX': '/Applications/Inkscape.app/Contents/Resources', 'LOGNAME': 'gmandil', 'USER': 'gmandil', 'HOME': '/Users/gmandil', 'PATH': '/usr/texbin:/opt/local/bin:/sw/bin/:/Library/Frameworks/Python.framework/Versions/Current/bin:/usr/local/bin:/Applications/Inkscape.app/Contents/Resources/bin:/usr/bin:/bin:/usr/sbin:/sbin', 'DISPLAY': '/tmp/launch-gQMhxa/org.macosforge.xquartz:0', 'LANG': 'fr_FR.UTF-8', 'Apple_PubSub_Socket_Render': '/tmp/launch-19ZNv0/Render', 'SHLVL': '0', 'GNOME_VFS_MODULE_CONFIG_PATH': '/Applications/Inkscape.app/Contents/Resources/etc/gnome-vfs-2.0/modules', 'GTK_DATA_PREFIX': '/Applications/Inkscape.app/Contents/Resources', 'MAGICK_CODER_FILTER_PATH': '/Applications/Inkscape.app/Contents/Resources/lib/ImageMagick-6.7.1/modules-Q16/filters', 'PYTHONPATH': '/Applications/Inkscape.app/Contents/Resources/extensions', 'SSH_AUTH_SOCK': '/tmp/launch-fAO9aX/Listeners', 'MAGICK_CONFIGURE_PATH': '/Applications/Inkscape.app/Contents/Resources/lib/ImageMagick-6.7.1/config:/Applications/Inkscape.app/Contents/Resources/share/ImageMagick-6.7.1/config', 'PANGO_RC_FILE': '/Users/gmandil/.inkscape-etc/pangorc', 'VERSION': '6', 'GTK2_RC_FILES': '/Users/gmandil/.inkscape-etc/gtkrc', 'INKSCAPE_LOCALEDIR': '/Applications/Inkscape.app/Contents/Resources/locale', 'TMPDIR': '/var/folders/9P/9PazJ4YeHZeVuy5-dRFJsU+++TQ/-Tmp-/', 'INKSCAPE_PLUGINDIR': '/Applications/Inkscape.app/Contents/Resources/lib/inkscape', 'GDK_PIXBUF_MODULE_FILE': '/Users/gmandil/.inkscape-etc/gdk-pixbuf.loaders', 'MAGICK_CODER_MODULE_PATH': '/Applications/Inkscape.app/Contents/Resources/lib/ImageMagick-6.7.1/modules-Q16/coders', 'GTK_IM_MODULE_FILE': '/Users/gmandil/.inkscape-etc/gtk.immodules', '__CF_USER_TEXT_ENCODING': '0x1F7:0:91\n', 'XDG_DATA_DIRS': '/Applications/Inkscape.app/Contents/Resources/share', 'PWD': '/Applications/Inkscape.app', 'GNOME_VFS_MODULE_PATH': '/Applications/Inkscape.app/Contents/Resources/lib/gnome-vfs-2.0/modules', 'COMMAND_MODE': 'legacy'}

su_v (suv-lp) said : #5

> With your help, I'm now able to see may extensions windows.
> But a problem remain. All the chatacters in these windows are
> remplaced by a square character like this one : ☐

This is with the extensions shipping with Inkscape, or with external custom extensions which use e.g. pygtk to open a dialog from within the spawned python process (e.g. textex and sozi)?

AFAICT the issue could be that py-*-gtk (used for the external dialog window) doesn't find the pango configuration files from MacPorts, or tries to reuse those used by Inkscape.app (which possibly do not match the version(s) used in your current MacPorts tree).

su_v (suv-lp) said : #6

There also seems to be a version mismatch:
in the shell, your default python version appears to be Python 2.6.7
> edmonde:~ gmandil$ python
> Python 2.6.7 (r267:88850, Nov 5 2011, 20:04:43)

however, Inkscape spawns python 2.7 and imports lxml and numpy modules installed for python 2.7:
> Python version:
> 2.7.2 (default, Nov 5 2011, 20:09:20)

- Which python version from MacPorts do you want to use, and which one is your current python version in MacPorts (use 'port select --show python', for additional info, see 'man port')?
- Does 'pygtk-demo' from the ports 'py26-gtk' and 'py27-gtk' work?
- You need to have gtk2 installed in MacPorts as dependency for py*-gtk: do you use the X11 based variant, or the Quartz-based variant in MacPorts?

su_v (suv-lp) said : #7

Nevermind - I can reproduce this if following my own instructions: they work well with normal extensions, but apparently not with those opening custom dialogs based on pygtk.

For now - since you already seem familiar with MacPorts, I'd recommend to install Inkscape itself in MacPorts ('port install inkscape'), and use the command line version if working with extensions like 'Sozi' or 'TeX Text' in Inkscape.

I'll further investigate what other workarounds or hacks might be used to make it work with the bundled application as well (without introducing library version conflicts and pango-related errors) - possibly placing a shell wrapper script named 'python' into 'Contents/Resources/bin' which unsets the environment explicitly set in inkscape's shell launcher script and only then execs MacPorts' python.

su_v (suv-lp) said : #8

A small change for you to test:

in 'Contents/Resources/inkscape', uncomment line 42 (remove the leading '#'):
line 42: # export DYLD_LIBRARY_PATH="$TOP/lib"

This will force py*-gtk from MacPorts to reuse the gtk/x11 libraries and its dependencies from inside the application bundle. It works for me with python 2.6 and python 2.7 (but I don't have a really up-to-date MacPorts tree).

Note: setting $DYLD_LIBRARY_PATH can have unintended consequences (other library version conflicts) and it would be better to find a solution without reactivating this environment variable.

su_v (suv-lp) said : #9

Sorry, I made a typo in my previous comment:

- in 'Contents/Resources/inkscape', uncomment line 42 (remove the leading '#'):
+ in 'Contents/Resources/bin/inkscape', uncomment line 42 (remove the leading '#'):

su_v (suv-lp) said : #10

> Note: setting $DYLD_LIBRARY_PATH can have unintended
> consequences (other library version conflicts) and it would
> be better to find a solution without reactivating this
> environment variable.

Possibly you get an error related to the version of poppler linked to by pygtk (poppler was recently upgraded in MacPorts). I'll keep you updated here if I found another workaround (which does not involve setting $DYLD_LIBRARY_PATH).

Best su_v (suv-lp) said : #11

Alternative solution:

1) revert 'Contents/Resources/bin/inkscape' to the original version (important)
2) save the content of <http://pastie.org/private/x1gbym3cybm8rrwgkwjq> as
   'Contents/Resources/bin/python'
3) make that shell script executable:
   $ chmod 755 "/path/to/Inkscape.app/Contents/Resources/bin/python"
4) edit 'Contents/Resources/bin/inkscape' and modify line 28:

current content of line28:
export PATH="/usr/bin:$PATH"
modified content of line 28:
export PATH="$CWD:$PATH"

Launch Inkscape and try to run an extension like 'sozi' which opens an external (pygtk-based) dialog.
(tested with Inkscape 0.48.2 and python 2.7 with all required dependencies for sozi installed in MacPorts for python 2.7)

What now happens:
When Inkscape (i.e. 'Contents/Resources/bin/inkscape-bin') spawns a python process, $PATH tells it to look in the current directory first for the python executable. 'inkscape-bin' executes the shell script wrapper 'Contents/Resources/bin/python' which unsets several specific environment settings used inside Inskcape.app, and then executes the default python from MacPorts with the same command line arguments passed to python by 'inkscape-bin'.

Note I: you can leave the python modules bundled with Inkscape in place - the external python from MacPorts will load its own modules (lxml, numpy) and ignore those from inside Inkscape.app, as can be verified with the debug extension.

Note II: this is a quick hack, which for example hard-codes the path of the executed python binary in the shell script wrapper. Possibly it could be improved - I'd be interested in feedback whether you got that working for you on your local system.

Guillaume Mandil (gmandil-x) said : #12

I Works ! Great !

Sorry for my late answer, I was quite busy these lasts days.
If I can help for more about this issue let me know...

Guillaume

PS : the explanation about the 2 different version of python was that the fisrt message was posted from my iMac and the second from my MacBook Pro...

Guillaume Mandil (gmandil-x) said : #13

Thanks ~suv, that solved my question.

su_v (suv-lp) said : #14

For anyone reaching this page via Google:
<http://www.lifl.fr/~riquetd/2012/03/13/installing-sozi-on-mac-os-x/> has written a nicely formatted summary of this hack.
(thanks for the credits, Damien ;-) )

su_v (suv-lp) said : #15

Update for Inkscape 0.48.5:

1) install dependencies in MacPorts (at least py27-pygtk, py27-lxml, py27-numpy)
2) create python wrapper script as described in comment #11
3) edit 'Contents/Resources/bin/inkscape' as below

current content of line 28:
export PATH="/usr/bin:$PATH"
modified content of line 28:
export PATH="$CWD:$PATH"

current content of line 39:
export PYTHONPATH="$TOP/python/site-packages/$ARCH/$PYTHON_VERS"
modified content of line 39:
#export PYTHONPATH="$TOP/python/site-packages/$ARCH/$PYTHON_VERS"

4) download Sozi:
http://sozi.baierouge.fr/pages/download.html
5) install Sozi into
$HOME/.config/inkscape/extensions/
5) restart Inkscape.app and enjoy using Sozi!

(Note: as side effect, this will disable python modules bundled with Inkscape.app - among them UniConvertor 1.1.5).

Ignacio Alvarez (ignalvarez) said : #16

Many thanks ~suv, it works perfectly (the problem was the line 39 of Contents/Resources/bin/inkscape).
[Ignacio]