[Zope3-Users] Re: How to best include Zope3-based code in Zope3
unrelated Python projects?
Philipp von Weitershausen
philipp at weitershausen.de
Fri Sep 21 06:44:27 EDT 2007
Hermann Himmelbauer wrote:
> Hi,
> I have the problem that I cannot include parts of my Zope3 project in
> external, Zope3-unrelated projects due to import errors.
You have problem and cause mixed up. You get import errors due to the
fact that the Zope 3 libraries aren't on your PYTHONPATH.
> For instance, I currently build a LaTeX converter that automatically converts
> SQL-Alchemy table definitions into nicely formatted LaTeX that I can then
> include in my project documentation.
>
> This converter has to import my table definitions file, which however fails,
> as on the top of the file there are Zope3-specific imports and directives,
> e.g.:
>
> from zope.interface import implements
> import z3c.zalchemy
> metadata = z3c.zalchemy.metadata()
>
> My Python interpreter does not know anything about zope yet, as it's not in
> the PYTHONPATH, therefore the import of zope.interface fails. I assume, I
> should somehow put the Zope3 modules into the PYTHONPATH - but how would you
> recommend that? Should I perhaps link it from python/lib/site-packages? Or
> should I include the Zope3 top-level directory into PYTHONPATH?
$ export PYTHONPATH=/usr/local/Zope-3.3.x/lib/python
$ python myscript.py
I would not recommend modifying the global Python installation to
include Zope. For scripts, setting PYTHONPATH is simple enough. For
anything fancier, I suggest using the setuptools mechanism to depend on
the relevant Zope eggs (install_requires in setup.py) and installing it
in a controlled environment (virtual-python, virtualenv or zc.buildout)
> Moreover, what's even more complicated is the import of z3c.zalchemy, how
> should I solve that one?
$ export
PYTHONPATH=~/my/zope/instance/lib/python:/usr/local/Zope-3.3.x/lib/python
$ python myscript.py
(assuming that z3c.zalchemy is installed as a package in your Zope
instance).
> I thought about a construct like this:
>
> if ???started via debugzope / zopectl???:
> import z3c.zalchemy
> metadata = MetaData()
> else:
> from sqlalchemy import MetaData
> metadata = MetaData()
>
> But I don't know how to fomulate the if-clause above. Perhaps there's a better
> solution to this problem?
try:
from z3c.zalchemy import MetaData
except ImportError:
from sqlalchemy import MetaData
metadata = MetaData()
--
http://worldcookery.com -- Professional Zope documentation and training
More information about the Zope3-users
mailing list