importing an existing python module into Zope python script?
This *must* be a FAQ, but I haven't found the answer anywhere despite all day looking... I've got an existing python module with a large amount of code in it which I want to use both inside Zope and outside in existing python programs. The module is still under development so will change frequently. Two burning questions: a) How do I get access to the module within Zope ? b) What do I then need to do to keep the versions in sync? As far as I can see, I have at least 3 alternatives to question (a). i) Copy it all into Zope, each function as a separate Zope python script (Ugh! and this then makes keeping it in sync outside Zope pretty darned difficult...) ii) Keep the module as-is and add an External Method in Zope for each function that will be called from a Zope script. Pretty awful but do-able. But then I think I need to do something else in the Zope Management Interface each time I change the module? Is that right? iii) Rewrite it all as a Zope Product? There must be an easier answer that has eluded me to date.... Any answers appreciated... Rachel
On Wed, Jan 28, 2004 at 04:38:56PM +0000, Rachel Willmer wrote:
This *must* be a FAQ, but I haven't found the answer anywhere despite all day looking...
I've got an existing python module with a large amount of code in it which I want to use both inside Zope and outside in existing python programs. The module is still under development so will change frequently.
Two burning questions:
a) How do I get access to the module within Zope ?
b) What do I then need to do to keep the versions in sync?
As far as I can see, I have at least 3 alternatives to question (a).
i) Copy it all into Zope, each function as a separate Zope python script (Ugh! and this then makes keeping it in sync outside Zope pretty darned difficult...)
ii) Keep the module as-is and add an External Method in Zope for each function that will be called from a Zope script. Pretty awful but do-able. But then I think I need to do something else in the Zope Management Interface each time I change the module? Is that right?
iii) Rewrite it all as a Zope Product?
iv) write a Zope Product that wraps your class(es). That's probably the most common approach. If the API is good, you won't have to modify the wrapper much, if ever. If your existing module changes on disk, you will have to refresh the product or restart zope before it will see the changes. -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's FLUORO-SOLID WHORE! (random hero from isometric.spaceninja.com)
Rachel Willmer wrote at 2004-1-28 16:38 +0000:
This *must* be a FAQ, but I haven't found the answer anywhere despite all day looking...
I've got an existing python module with a large amount of code in it which I want to use both inside Zope and outside in existing python programs. The module is still under development so will change frequently.
Apparently, you did not read the "README" file in "Products/PythonScripts". Watch out for "allow_module". -- Dieter
OK, so I'm now convinced that the way forward is to use a Product wrapper around the python module. (Thanks to Dieter, Sean, Andrew and Paul for responding...) But, I'm still struggling with getting it to work :-( Using the lib/python/PythonScripts/README as a reference: a) I create a directory lib/python/GlobalModules b) I create __init__.py which looks like this... from Products.PythonScripts.Utility import allow_module allow_module('base64') c) I restart the Zope server in development mode, and configure the GlobalModules product to auto-refresh. d) I then create a Python Script file which looks like this: from Products.GlobalModules import base64 def testing() : print base64.encodestring("hello") return printed e) I call the script and get Error Type: NameError Error Value: global name 'base64' is not defined What else do I need to do to get this to work? or, what am I doing wrong? (Note, I'm using a simple example here with a builtin function, because I thought that would make it clearer, but what I really want to do is use a module of my own) Thanks for any more pointers. Rachel
On Jan 30, 2004, at 10:54 AM, Rachel Willmer wrote:
d) I then create a Python Script file which looks like this:
from Products.GlobalModules import base64 def testing() : print base64.encodestring("hello") return printed
e) I call the script and get
Error Type: NameError Error Value: global name 'base64' is not defined
What else do I need to do to get this to work? or, what am I doing wrong?
Just import base64 as you would in any other Python script: import base64 No reference to the Product needed. ___/ / __/ / ____/ Ed Leafe Linux Love: unzip;strip;touch;finger;mount;fsck;more;yes;umount;sleep
Ed Leafe wrote:
Just import base64 as you would in any other Python script:
import base64
No reference to the Product needed.
Thanks for your email. I've tried that as well, same error. Any other thoughts? Rachel
On Jan 30, 2004, at 1:19 PM, Rachel Willmer wrote:
Just import base64 as you would in any other Python script: import base64 No reference to the Product needed.
Thanks for your email. I've tried that as well, same error.
Any other thoughts?
Sorry; no. My product's __init__.py simple has: from Products.PythonScripts.Utility import allow_module allow_module("re") ... and my scripts that use regexps start with: import re ... and they work fine. ___/ / __/ / ____/ Ed Leafe http://leafe.com/ http://opentech.leafe.com
Rachel Willmer wrote at 2004-1-30 15:54 +0000:
OK, so I'm now convinced that the way forward is to use a Product wrapper around the python module. (Thanks to Dieter, Sean, Andrew and Paul for responding...)
But, I'm still struggling with getting it to work :-(
Using the lib/python/PythonScripts/README as a reference:
a) I create a directory lib/python/GlobalModules
b) I create __init__.py which looks like this...
from Products.PythonScripts.Utility import allow_module allow_module('base64')
c) I restart the Zope server in development mode, and configure the GlobalModules product to auto-refresh.
What is the "GlobalModules" product? Products usually do not live in "lib/python" but in a "Products" folder. Only products can be refreshed.
d) I then create a Python Script file which looks like this:
from Products.GlobalModules import base64 def testing() : print base64.encodestring("hello") return printed
e) I call the script and get
Error Type: NameError Error Value: global name 'base64' is not defined
Your code above cannot produce this error... Whenever you see such an error, you must look at the traceback (via the "error_log" object in your Zope "Root Folder"). It tells you precisely where the error was raised. In your case, it will show you that this is not in the code you have shown above (or you did not show us all the relevant code -- e.g. a call for "testing"). -- Dieter
Dieter Maurer wrote:
What is the "GlobalModules" product?
Products usually do not live in "lib/python" but in a "Products" folder.
Only products can be refreshed.
GlobalModules is the name of the example product used in the README file you referred me to above. It is in the lib/python/Products directory (not lib/python)(sorry, careless typing on my part) Right, thanks for all your help. I've got this working now... It was a combination of careless typing, browser not refreshing when I thought it was, zope not refreshing when I thought it was, and just staring at the problem too hard for too long... I'd like to write this up to add to the documentation to help the next newbie that hits this problem, should I just add something into the Zope Book (can I?) or is there a more formal entry submission process? Thanks again for your help Rachel
participants (4)
-
Dieter Maurer -
Ed Leafe -
Paul Winkler -
Rachel Willmer