[Zope] Using an external method into another external method
Thomas B. Passin
tpassin@mitretek.org
Thu, 5 Sep 2002 18:58:42 -0400
[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