librelist archives

« back to archive

from_pyfile() with arbitrary file?

from_pyfile() with arbitrary file?

From:
Malphas Wats
Date:
2012-05-14 @ 09:37
Hi,

  I'm not sure if this is actually a Flask thing or a general Python thing.
I'm learning Python by building a website with Flask!

I've created my site as a new package as so:

  ~/projects/pyDimension/
    startDevServer.py
    - pyDimension/
        __init__.py
        ... modules ...
        site_config.cfg

I've gotten to the part where I want to deploy the site using mod_wsgi and
apache, which works fine, I did it this way:

  ~/public_html/pyDim_site/
        pyDim.wsgi

my apache config sets the start directory in public_html/pyDim_site and the
.wsgi file imports the ~/projects/pyDimension path

What I'd like to be able to do is move the site_config.cfg file into the
~/public_html/pyDim_site/ folder, so I could potentially use the same
package for multiple sites, each configured differently. I tried a few
different permutations of appending to the python path and starting in
different directories, but app.config.from_pyfile() only seems to look in
the package directory?

How can I make it look somewhere else without hard-coding it?

I did also try using an environment variable, but the Apache SetEnv
directive didn't seem to work?

Thanks for your time!

Re: [flask] from_pyfile() with arbitrary file?

From:
Simon Sapin
Date:
2012-05-14 @ 09:48
Le 14/05/2012 11:37, Malphas Wats a écrit :
>
> What I'd like to be able to do is move the site_config.cfg file into the
> ~/public_html/pyDim_site/ folder, so I could potentially use the same
> package for multiple sites, each configured differently. I tried a few
> different permutations of appending to the python path and starting in
> different directories, but app.config.from_pyfile() only seems to look
> in the package directory?
>
> How can I make it look somewhere else without hard-coding it?

Hi,

app.config.from_py() takes a filename and does not use the import 
mechanism at all (so sys.path is irrelevant.) If you pass a relative 
filename, it will be resolved from the application directory. Apparently 
you want to use an absolute path here.

Also note that Python does not resolve '~' in filenames automatically 
like your shell does. Either hard-code the full path (/home/foo/...) or 
use expanduser:

http://docs.python.org/library/os.path.html#os.path.expanduser

Regards,
-- 
Simon Sapin

Re: [flask] from_pyfile() with arbitrary file?

From:
Malphas Wats
Date:
2012-05-14 @ 09:57
On Mon, May 14, 2012 at 10:48 AM, Simon Sapin <simon.sapin@exyr.org> wrote:
>
> Le 14/05/2012 11:37, Malphas Wats a écrit :
> >
> > What I'd like to be able to do is move the site_config.cfg file into the
> > ~/public_html/pyDim_site/ folder, so I could potentially use the same
> > package for multiple sites, each configured differently. I tried a few
> > different permutations of appending to the python path and starting in
> > different directories, but app.config.from_pyfile() only seems to look
> > in the package directory?
> >
> > How can I make it look somewhere else without hard-coding it?
>
> Hi,
>
> app.config.from_py() takes a filename and does not use the import
> mechanism at all (so sys.path is irrelevant.) If you pass a relative
> filename, it will be resolved from the application directory. Apparently
> you want to use an absolute path here.
>
> Also note that Python does not resolve '~' in filenames automatically
> like your shell does. Either hard-code the full path (/home/foo/...) or
> use expanduser:
>
> http://docs.python.org/library/os.path.html#os.path.expanduser
>

Thanks Simon, I'd only used the ~ to save my typing (and avoid giving
away my username ;) )

is there a way to inject the path from within the .wsgi file? I
haven't quite worked out what the .wsgi file is, is it just a regular
python file? could I:

  Add a 'global' variable in __init__.py called SETTINGS_PATH

then in the .wsgi file, do something like:

  from pyDimension import srch as application
  application.SETTINGS_PATH = '/path/to/settings'

?

thanks again.

Re: [flask] from_pyfile() with arbitrary file?

From:
Simon Sapin
Date:
2012-05-14 @ 12:21
Le 14/05/2012 11:57, Malphas Wats a écrit :
> Thanks Simon, I'd only used the ~ to save my typing (and avoid giving
> away my username;)  )

You can either use expanduser or, if the file is "near" your code, this 
is common pattern:

os.path.join(os.path.dirname(__file__), 'foo', 'bar.cfg')

__file__ is the filename of the current source file.


> is there a way to inject the path from within the .wsgi file? I
> haven't quite worked out what the .wsgi file is, is it just a regular
> python file? could I:
>
>    Add a 'global' variable in __init__.py called SETTINGS_PATH
>
> then in the .wsgi file, do something like:
>
>    from pyDimension import srch as application
>    application.SETTINGS_PATH = '/path/to/settings'
>
> ?

By the time you can do application.foo = 'bar', the "application" module 
is done importing. So it won’t work if you create you Flask app at 
import-time: SETTINGS_PATH won’t be available yet. However you could 
just call app.config.from_pyfile() from the .wsgi file.

An other way is to create the app object within a factory function with 
the settings filename as a parameter:

http://flask.pocoo.org/docs/patterns/appfactories/

-- 
Simon Sapin