Advice on distutils and distribution policies
Magnus Lycka
lycka at carmen.se
Mon Nov 21 14:08:24 EST 2005
Mardy wrote:
> Hi,
> I've built a small project (http://eligante.sf.net) which I'm actually
> trying to package using distutils.
...
> However, I don't know if this directory layout is suitable for
> site-packages, since at a first glance it looks to me that datafiles might
> not be welcome under it. Is it so?
In general, it's a good idea to keep code and data separated:
- Different people might need to access code and data. People who
add content to a web site shouldn't be able to make a mess of the
software that controls that web site (or vice versa).
- Lifecycles are different, so different backup strategies might apply.
- Directory structures such as /usr under unix are typically fairly
static on production systems, and there might be assumptions that
required space doesn't grow over time, whereas data, kept under
/var is expected to change more over time. Also, if e.g. web content
is kept in a separate disk partition, filling that partition won't
mess up other parts of the computer system.
- It should be easy to upgrade the software without messing up data.
It's practical if it's safe to e.g. do
"rm -rf /usr/lib/python2.3/site-packages/eligante" followed by
a new "python setup.py install" in case a sys admin thinks that
his old install was broken.
For a CGI-based app on e.g. a linux box with Apache, I think it would
be typical to partition things like this:
In an apache cgi-bin directory: The main Python CGI script(s) that
are called by the web server. These might be scripts that are modified
as a part of the installation process to e.g. point out data files.
These should be short files. Import a module, set up configuration
and run something in the imported module.
Under /usr/lib/python2.x/site-packages/ you keep the bulk of your
software (as described above).
HTML and CSS files that are to be handled directly by the web browser
is placed under Apache's DOCUMENT_ROOT etc. E.g.
$DOCUMENT_ROOT/eligante.
Data files that are read and processed by you own programs (i.e. not
directly by the web server) van be anywhere under /var, e.g.
/var/eligante.
> In that case, where should I move the .html files, and how should I access
> them from inside python?
Asolute path defined by an environment variable?
I think Apache defines a DOCUMENT_ROOT variable. You
could decide that your HTML files etc should reside in
some particular place relative to DOCUMENT_ROOT, and
use something like...
import os
SUB_FOLDER = 'my_folder'
dir = os.path.join(os.environ['DOCUMENT_ROOT'],SUB_FOLDER)
...to locate your files.
As I wrote above, hardcoding in a main CGI-script is also
common practice.
More information about the Python-list
mailing list