RE: Importing Products into my Custom Product....
I am asking this question again in hopes that maybe this time it will make more sense.... What I want to do is reuse code from one product to the next in a Python Class...... This is trivial in a ZClass, but ???? Anyway, I create a new Python Product "FOO" and I want to import the methods of another Python Product "Bar" ("FOO" and "BAR" are next to each other in the Products Directory), so I add "import Bar" into my __init__.py file for "FOO" When I do this, the ZServer complains that the module does not exist..... So, I try: "from Bar import Bar" and "from Bar.Bar import Bar" and "from Products.Bar import Bar" Again the ZServer complains that the module does not exist -even though they *do* exist right next to each other in the same "Products" Directory..... How do I get one product to use (reuse) the methods of another product that also exists in the "Products" Directory? WPH
On Sat, Sep 11, 2004 at 11:52:16AM -0700, Bill Hewitt wrote: [snip]
"import Bar" into my __init__.py file for "FOO"
When I do this, the ZServer complains that the module does not exist..... So, I try: "from Bar import Bar" and "from Bar.Bar import Bar" and "from Products.Bar import Bar"
Again the ZServer complains that the module does not exist -even though they *do* exist right next to each other in the same "Products" Directory.....
How do I get one product to use (reuse) the methods of another product that also exists in the "Products" Directory?
I don't know anything specific about Zope products, but it sounds like a standard import problem. You might print or inspect the contents of sys.path. For example: import sys print sys.path sys.path is a list of the places you can import things from. Then, if the path you need is not there, do the following before importing: sys.path.append("/a/path/to/the/instance/Products") or: sys.path.insert(0, "/a/path/to/the/instance/Products") Hope this helps. Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman
On Sat, Sep 11, 2004 at 12:54:46PM -0700, Dave Kuhlman wrote:
You might print or inspect the contents of sys.path. For example:
import sys print sys.path
sys.path is a list of the places you can import things from. Then, if the path you need is not there, do the following before importing:
sys.path.append("/a/path/to/the/instance/Products")
or:
sys.path.insert(0, "/a/path/to/the/instance/Products")
You should never need to do that. In Zope 2.7, if you really need to add additional directories to your Products path, you can set the "products" directive in etc/zope.conf, multiple times if needed: # Example: # # products /home/chrism/projects/myproducts But anyway, based on the original post, it is not a problem with the path. Zope is reporting import errors when trying to import one of his Products, and both are in the same Products directory, so his path is fine. It's got to be a problem with his code. -- Paul Winkler http://www.slinkp.com
On Sat, Sep 11, 2004 at 11:52:16AM -0700, Bill Hewitt wrote:
I am asking this question again in hopes that maybe this time it will make more sense....
What I want to do is reuse code from one product to the next in a Python Class...... This is trivial in a ZClass, but ????
Anyway, I create a new Python Product "FOO" and I want to import the methods of another Python Product "Bar" ("FOO" and "BAR" are next to each other in the Products Directory), so I add "import Bar" into my __init__.py file for "FO0"
Two points here: 1) You're mixing different capitalization styles. You might get away with that on Windows but it will bite you on any case-sensitive platform... 2) You don't need to import Bar in Foo's __init__.py, you should instead put your imports in whatever module of Foo actually needs to call methods from Bar. See below for how to do this.
When I do this, the ZServer complains that the module does not exist..... So, I try: "from Bar import Bar" and "from Bar.Bar import Bar" and "from Products.Bar import Bar"
The latter should work, so something must be wrong. Here's a working example of the CMFDefault product importing something from the CMFCore product: from Products.CMFCore.CMFCorePermissions import View This works because the following conditions are satisfied: 1) CMFCore is a proper package: i.e. it must have an __init__.py file. 2) CMFCore is placed in the Products directory. 3) CMFCore/CMFCorePermissions.py must exist. 4) CMFCorePermissions.py must have a module-level name View. -- Paul Winkler http://www.slinkp.com
participants (3)
-
Bill Hewitt -
Dave Kuhlman -
Paul Winkler