External Method importing modules
Dear List, I have an external method and now I developed I new module in a separated file and then I am trying to make my external method to import it but I am failing here. Where should place my module file in order to be found by my external method? I tried to place it in the same folder of my external method, but no success. Any help would be very appreciated. Many thanks in advance. Cheers, Alan -- Alan Wilter S. da Silva, D.Sc. - CCPN Research Associate Department of Biochemistry, University of Cambridge. 80 Tennis Court Road, Cambridge CB2 1GA, UK.
--On 13. Februar 2007 12:53:25 +0000 Alan <alanwilter@gmail.com> wrote:
Dear List,
I have an external method and now I developed I new module in a separated file and then I am trying to make my external method to import it but I am failing here. Where should place my module file in order to be found by my external method? I tried to place it in the same folder of my external method, but no success.
Since the module must be importable, it must be available in some location as defined through sys.path or $PYTHONPATH. Check the Python docs for additional info on that topic. -aj
Thanks Andreas, I am trying with: if sys.path.count(myPath) == 0: sys.path.insert(0, myPath) where myPath should be '[Zope installation folder]/instance/import'. So my question now is: is there a variable inside Zope which can returns the 'import' path? Many thanks in advance. Cheers, Alan On 13/02/07, Andreas Jung <lists@zopyx.com> wrote:
--On 13. Februar 2007 12:53:25 +0000 Alan <alanwilter@gmail.com> wrote:
Dear List,
I have an external method and now I developed I new module in a separated file and then I am trying to make my external method to import it but I am failing here. Where should place my module file in order to be found by my external method? I tried to place it in the same folder of my external method, but no success.
Since the module must be importable, it must be available in some location as defined through sys.path or $PYTHONPATH. Check the Python docs for additional info on that topic.
-aj
-- Alan Wilter S. da Silva, D.Sc. - CCPN Research Associate Department of Biochemistry, University of Cambridge. 80 Tennis Court Road, Cambridge CB2 1GA, UK.
--On 13. Februar 2007 13:10:17 +0000 Alan <alanwilter@gmail.com> wrote:
Thanks Andreas, I am trying with:
if sys.path.count(myPath) == 0: sys.path.insert(0, myPath)
What's the sense of this code? sys.path is usually *never* empty. You should really look at the Python docs and learn where Python is looking by default for modules. Is is perfectly documented -> python.org -> documentation. In addition you would like to check for the existence of an item in sys.path using if myPath in sys.path: -aj
----- Original Message ----- From: "Alan" <alanwilter@gmail.com> To: "Andreas Jung" <lists@zopyx.com> Cc: <zope@zope.org> Sent: Tuesday, February 13, 2007 8:10 AM Subject: Re: [Zope] External Method importing modules
Thanks Andreas, I am trying with:
if sys.path.count(myPath) == 0: sys.path.insert(0, myPath)
where myPath should be '[Zope installation folder]/instance/import'. So my question now is: is there a variable inside Zope which can returns the 'import' path?
You can easily check what's in your sys.path by having your external method return it. Typically, the Extensions directory (where your external methods live) are not in the default path and need to be added to sys.path.(warning: be careful on how you do this or everytime your external method gets called it will add to the sys.path and will eventually cause you grief!). Jonathan
Dear Andreas and Jonathan, first a mistake: I want to access 'Extensions' folder as you mentioned Jonathan, not 'import'. Andreas, I've done that, I can see several path there like .../instance/Products/Sprout/src, .../lib/python/Zope2/Startup etc., but none for .../instance/Extensions. I was hoping that I could get the path where 'Extensions' is because I work with several zope installations in several platforms and I would like to make my module independent of a variable inside its code that should be manually adjusted for every situation. And yes, I am aware about adding the path only once (that's why there's a 'if') and then I avoid recursive insertions. Cheers, Alan On 13/02/07, Jonathan <dev101@magma.ca> wrote:
----- Original Message ----- From: "Alan" <alanwilter@gmail.com> To: "Andreas Jung" <lists@zopyx.com> Cc: <zope@zope.org> Sent: Tuesday, February 13, 2007 8:10 AM Subject: Re: [Zope] External Method importing modules
Thanks Andreas, I am trying with:
if sys.path.count(myPath) == 0: sys.path.insert(0, myPath)
where myPath should be '[Zope installation folder]/instance/import'. So my question now is: is there a variable inside Zope which can returns the 'import' path?
You can easily check what's in your sys.path by having your external method return it.
Typically, the Extensions directory (where your external methods live) are not in the default path and need to be added to sys.path.(warning: be careful on how you do this or everytime your external method gets called it will add to the sys.path and will eventually cause you grief!).
Jonathan
-- Alan Wilter S. da Silva, D.Sc. - CCPN Research Associate Department of Biochemistry, University of Cambridge. 80 Tennis Court Road, Cambridge CB2 1GA, UK.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Alan wrote:
Dear Andreas and Jonathan, first a mistake: I want to access 'Extensions' folder as you mentioned Jonathan, not 'import'.
Andreas, I've done that, I can see several path there like .../instance/Products/Sprout/src, .../lib/python/Zope2/Startup etc., but none for .../instance/Extensions.
I was hoping that I could get the path where 'Extensions' is because I work with several zope installations in several platforms and I would like to make my module independent of a variable inside its code that should be manually adjusted for every situation.
And yes, I am aware about adding the path only once (that's why there's a 'if') and then I avoid recursive insertions.
I don't know if it helps, but $INSTANCE_HOME/lib/python is going to be on your path by default. Personally, I would put the module-to-be-imported there, rather than trying to hack sys.path. Tres. - -- =================================================================== Tres Seaver +1 540-429-0999 tseaver@palladion.com Palladion Software "Excellence by Design" http://palladion.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFF0fxD+gerLs4ltQ4RAhTVAKC6UCFmX/WPv1B2snREMTeoGEET4gCfaPIZ O+ErLeWJIDZ5YIMkF7dtIF8= =zSoJ -----END PGP SIGNATURE-----
Alan wrote at 2007-2-13 13:10 +0000:
if sys.path.count(myPath) == 0: sys.path.insert(0, myPath)
where myPath should be '[Zope installation folder]/instance/import'. So my question now is: is there a variable inside Zope which can returns the 'import' path?
Formerly, the "ExternalMethod" documentation suggested to put such modules into "<SOFTWARE_HOME>/Shared/<your_company>/...". Nowadays (since Zope 2.7, I think), "<INSTANCE_HOME>/lib/python" is a better place (with any structure you would like to use). This folder is put on "PYTHONPATH" automatically. No need to fiddle with it (I have already searched hours for problems caused by runtime "sys.path" manipulations). -- Dieter
participants (5)
-
Alan -
Andreas Jung -
Dieter Maurer -
Jonathan -
Tres Seaver