Using an external method into another external method
Let's say I have two external methods (foo1 and foo2) in two different files (myFile1 and myFile2 respectively) on my Extensions folder. I would like to import foo1 into the myFile2 where foo2 is. I have tried: from myFile2 import foo2 from Extensions.myFile2 import foo2 but it doesn't work. It says that Either myFile2 or Extensions doesn't exist. I don't want to put them in the same file because foo1 is called several times inside myFile1 and foo2 is called several times in myFile2. So, I would have to join both files into one and it wouldn't be an elegant solution. Does anybody have any idea? Thanks in advanced, Josef.
If you must do this put an empty file called __init__.py in the Extensions folder (you might need to restart). -- Andy McKay Agmweb Consulting http://www.agmweb.ca ----- Original Message ----- From: "Josef Meile" <jmeile@hotmail.com> To: <zope@zope.org> Sent: Thursday, September 05, 2002 2:49 PM Subject: [Zope] Using an external method into another external method
Let's say I have two external methods (foo1 and foo2) in two different files (myFile1 and myFile2 respectively) on my Extensions folder. I would like to import foo1 into the myFile2 where foo2 is.
I have tried:
from myFile2 import foo2 from Extensions.myFile2 import foo2
but it doesn't work. It says that Either myFile2 or Extensions doesn't exist.
I don't want to put them in the same file because foo1 is called several times inside myFile1 and foo2 is called several times in myFile2. So, I would have to join both files into one and it wouldn't be an elegant solution.
Does anybody have any idea?
Thanks in advanced, Josef.
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
If you must do this put an empty file called __init__.py in the Extensions folder (you might need to restart). Andy, thanks for the trick. It works.
About this answer:
Put Extensions directory in you sys.path I just found a document which says how to do it http://www.zope.org/Members/Amos/ZPublisher and it seems to be easy as well.
But I have a doubt about this one:
or PYTHONPATH enviroment. Where should one set the PYTHONPATH? in the start script? I didn't found it there, so I guess it's not set. Anyway, when one sets this variable, will it rewrite the default values? or will they be taken as well?
Thanks in advanced, Josef
[Josef Meile]
Let's say I have two external methods (foo1 and foo2) in two different files (myFile1 and myFile2 respectively) on my Extensions folder. I would like to import foo1 into the myFile2 where foo2 is.
I have tried:
from myFile2 import foo2 from Extensions.myFile2 import foo2
but it doesn't work. It says that Either myFile2 or Extensions doesn't exist.
To be able to import a method, its file must be in a Python "package". To accomplish this, you need two things: 1) The directory that contains your file or files must contain a file called "__init__.py". It acn be empty, or it can do some initialization for the package. Usually it is empty. 2) The containing directory, the one with the __init__.py, must be on the Python path. I am not sure whether the Extensions directory is or not, but I do not favor putting much code into it anyway. Most code, I think, should be in some other place and you just have a tiny function for the External method that invokes the real code (along with doing relevant imports). This approach has a number of benefits: - The Extensions directory only contains a minimum number of simple files. - You can set up arbitrary directory structures outside of the Zope installation. - You can easily run code that you wrote for non-Zope purposes withou copying it into Extensions. - You do not have to turn Extensions into a package. There are several ways to get something onto the Python path. The one I like is to create a .pth file. Each line in the *.pth file should contain some path you want to show up in the Python path. Put the file in the same directory as the Python executable (the one for Zope, I mean). Cheers, Tom P
participants (3)
-
Andy McKay -
Josef Meile -
Thomas B. Passin