Can I do this in a python products ??
Hello, everyone, Since this is my first post on this lis, I will introduce myself shortly. I'm a developper in a small enterprise which has a huge internet project, since I am a great user of linux and of python I naturally tough about doing all my job in Zope. At first I played with dtml-scripting and so on. But now I'd like to make a little product and when I'll be fluent in this kind of programming, I will migrate the whole site to Product-like programming. Now, my question : I do this in the initialization of my product : fd = open('the_file') line = fd.readline() while line : process_line(line) line = fd.readline() The file 'the_file' is in the base directory of the product but I keep getting 'No such file'. I then decided to put this file on the Zope Directory where my product will be localized but the same problem arose. Is it possible to have some kind of a rc-file for a product the need to be parsed (this file residing either in the product directory or in the zope directory). Maybe I'm all wrong and that's not the way I ought to think in Zope ... -- Évrard Nicolas SOLIREM SA - Projet Smartainers 1, rue de l'épervier Tel: +32 4 248 08 85 B-4040 Herstal Fax: +32 4 248 05 78
[Nicolas Évrard] Now, my question : I do this in the initialization of my product : fd = open('the_file') line = fd.readline() while line : process_line(line) line = fd.readline() The file 'the_file' is in the base directory of the product but I keep getting 'No such file'. I then decided to put this file on the Zope Directory where my product will be localized but the same problem arose. Is it possible to have some kind of a rc-file for a product the need to be parsed (this file residing either in the product directory or in the zope directory). [Tom P] You should always make sure a module can find out the right location for a file rather than relying on a guess or default location. When I use external scripts, I generally use a path relative to the module or package that contains the program. That way I can move the code to another installation on another directory tree or drive and still have things work. There are several possibilities. Here are some: 1) Use an environmental variable. This means you have to remember to set the variable before running Zope. 2) Put your code into a package (put it into a directory on the python path and put an __init__.py file into that directory). When you import the package, you can get the complete path from the package's __file__attribute. For example, if you have a package called "altlas": import atlas,os.path PATH=os.path.dirname(atlas.__file__) 3) A variation on 2) is to create a variable in the package's __init__.py file: import os.path BASEPATH=os.path.dirname(__file__) Now when you import the package, the BASEPATH variable is available. To do this, you may need to put your code into subdirectories or subpackages under the package. 4) You could also try making the path be relative to that of Zope by using os.cwd() or sys.argv[0], but that might be less robust. Cheers, Tom P
* Thomas B. Passin <tpassin@mitretek.org> [10:59 03/05/02] :
There are several possibilities. Here are some:
1) Use an environmental variable. This means you have to remember to set the variable before running Zope.
Seems a bit like a quick hack no ??
2) Put your code into a package (put it into a directory on the python path and put an __init__.py file into that directory). When you import the package, you can get the complete path from the package's __file__attribute. For example, if you have a package called "altlas":
import atlas,os.path PATH=os.path.dirname(atlas.__file__)
3) A variation on 2) is to create a variable in the package's __init__.py file:
import os.path BASEPATH=os.path.dirname(__file__)
Now when you import the package, the BASEPATH variable is available. To do this, you may need to put your code into subdirectories or subpackages under the package.
These two solutions seem cool, i'll try them. but I've found this function by browsing in the source the ZopeTutorialProduct : import App.Common App.Common.package_home seems to return the home of the package (duh !!)
4) You could also try making the path be relative to that of Zope by using os.cwd() or sys.argv[0], but that might be less robust.
Humm as you say this is less robust. So I won't even try it. -- Évrard Nicolas SOLIREM SA - Projet Smartainers 1, rue de l'épervier Tel: +32 4 248 08 85 B-4040 Herstal Fax: +32 4 248 05 78
participants (2)
-
Nicolas Évrard -
Thomas B. Passin