librelist archives

« back to archive

Help with configuring SQLAlchemy URI

Help with configuring SQLAlchemy URI

From:
Samrat Man Singh
Date:
2011-07-17 @ 12:16
Hi,
I'm trying to use SQLALchemy and when I use

SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/test.db',

the database initializes painlessly but when I try to do something like

SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/test.db' % os.path.dirname(__file__)

(like in Simblin- https://github.com/eugenkiss/Simblin) so that the database
file will stay in the application's directory.

But the latter one gives me an error-

sqlalchemy.exc.OperationalError: (OperationalError) unable to open database
file None None

Why am I getting this error and how do I fix it?

Samrat

Re: [flask] Help with configuring SQLAlchemy URI

From:
Jesaja Everling
Date:
2011-07-17 @ 12:56
Hi Samrat,

to be honest, I don't know why 'sqlite:///%s/test.db' %
os.path.dirname(__file__) doesn't work, looks ok to me.
But did you try just using sqlite:///test.db?
I think it should work.

Best Regards,

Jesaja Everling


On Sun, Jul 17, 2011 at 2:16 PM, Samrat Man Singh
<samratmansingh@gmail.com> wrote:
> Hi,
> I'm trying to use SQLALchemy and when I use
> SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/test.db',
> the database initializes painlessly but when I try to do something like
> SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/test.db' % os.path.dirname(__file__)
> (like in Simblin- https://github.com/eugenkiss/Simblin) so that the database
> file will stay in the application's directory.
> But the latter one gives me an error-
> sqlalchemy.exc.OperationalError: (OperationalError) unable to open database
> file None None
> Why am I getting this error and how do I fix it?
> Samrat
>
>
>
>

Re: [flask] Help with configuring SQLAlchemy URI

From:
Samrat Man Singh
Date:
2011-07-17 @ 13:08
Jesaja, Thanks for your suggestion; it works fine :)

But I'd still like to find out why 'sqlite:///%s/test.db' %
os.path.dirname(__file__) doesn't work(it worked just fine for Simblin). So,
if anyone else knows about this please help.

Samrat

On Sun, Jul 17, 2011 at 6:41 PM, Jesaja Everling <jeverling@gmail.com>wrote:

> Hi Samrat,
>
> to be honest, I don't know why 'sqlite:///%s/test.db' %
> os.path.dirname(__file__) doesn't work, looks ok to me.
> But did you try just using sqlite:///test.db?
> I think it should work.
>
> Best Regards,
>
> Jesaja Everling
>
>
> On Sun, Jul 17, 2011 at 2:16 PM, Samrat Man Singh
> <samratmansingh@gmail.com> wrote:
> > Hi,
> > I'm trying to use SQLALchemy and when I use
> > SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/test.db',
> > the database initializes painlessly but when I try to do something like
> > SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/test.db' %
> os.path.dirname(__file__)
> > (like in Simblin- https://github.com/eugenkiss/Simblin) so that the
> database
> > file will stay in the application's directory.
> > But the latter one gives me an error-
> > sqlalchemy.exc.OperationalError: (OperationalError) unable to open
> database
> > file None None
> > Why am I getting this error and how do I fix it?
> > Samrat
> >
> >
> >
> >
>

Re: [flask] Help with configuring SQLAlchemy URI

From:
Jonathan Chen
Date:
2011-07-17 @ 15:05
The only thing that comes to mind would be the operating system. It could
not like the idea of having mixed "slashes" if you are on windows. I could
be wrong.

~Jonathan C.


On Sun, Jul 17, 2011 at 6:08 AM, Samrat Man Singh
<samratmansingh@gmail.com>wrote:

> Jesaja, Thanks for your suggestion; it works fine :)
>
> But I'd still like to find out why 'sqlite:///%s/test.db' %
> os.path.dirname(__file__) doesn't work(it worked just fine for Simblin).
> So, if anyone else knows about this please help.
>
> Samrat
>
>
> On Sun, Jul 17, 2011 at 6:41 PM, Jesaja Everling <jeverling@gmail.com>wrote:
>
>> Hi Samrat,
>>
>> to be honest, I don't know why 'sqlite:///%s/test.db' %
>> os.path.dirname(__file__) doesn't work, looks ok to me.
>> But did you try just using sqlite:///test.db?
>> I think it should work.
>>
>> Best Regards,
>>
>> Jesaja Everling
>>
>>
>> On Sun, Jul 17, 2011 at 2:16 PM, Samrat Man Singh
>> <samratmansingh@gmail.com> wrote:
>> > Hi,
>> > I'm trying to use SQLALchemy and when I use
>> > SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/test.db',
>> > the database initializes painlessly but when I try to do something like
>> > SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/test.db' %
>> os.path.dirname(__file__)
>> > (like in Simblin- https://github.com/eugenkiss/Simblin) so that the
>> database
>> > file will stay in the application's directory.
>> > But the latter one gives me an error-
>> > sqlalchemy.exc.OperationalError: (OperationalError) unable to open
>> database
>> > file None None
>> > Why am I getting this error and how do I fix it?
>> > Samrat
>> >
>> >
>> >
>> >
>>
>
>

Re: [flask] Help with configuring SQLAlchemy URI

From:
Samrat Man Singh
Date:
2011-07-18 @ 02:48
I'm not on windows so that's certainly not the problem.
samrat

On 17 Jul 2011 20:52, "Jonathan Chen" <tamasiaina@gmail.com> wrote:
> The only thing that comes to mind would be the operating system. It could
> not like the idea of having mixed "slashes" if you are on windows. I could
> be wrong.
>
> ~Jonathan C.
>
>
> On Sun, Jul 17, 2011 at 6:08 AM, Samrat Man Singh
> <samratmansingh@gmail.com>wrote:
>
>> Jesaja, Thanks for your suggestion; it works fine :)
>>
>> But I'd still like to find out why 'sqlite:///%s/test.db' %
>> os.path.dirname(__file__) doesn't work(it worked just fine for Simblin).
>> So, if anyone else knows about this please help.
>>
>> Samrat
>>
>>
>> On Sun, Jul 17, 2011 at 6:41 PM, Jesaja Everling <jeverling@gmail.com
>wrote:
>>
>>> Hi Samrat,
>>>
>>> to be honest, I don't know why 'sqlite:///%s/test.db' %
>>> os.path.dirname(__file__) doesn't work, looks ok to me.
>>> But did you try just using sqlite:///test.db?
>>> I think it should work.
>>>
>>> Best Regards,
>>>
>>> Jesaja Everling
>>>
>>>
>>> On Sun, Jul 17, 2011 at 2:16 PM, Samrat Man Singh
>>> <samratmansingh@gmail.com> wrote:
>>> > Hi,
>>> > I'm trying to use SQLALchemy and when I use
>>> > SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/test.db',
>>> > the database initializes painlessly but when I try to do something
like
>>> > SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/test.db' %
>>> os.path.dirname(__file__)
>>> > (like in Simblin- https://github.com/eugenkiss/Simblin) so that the
>>> database
>>> > file will stay in the application's directory.
>>> > But the latter one gives me an error-
>>> > sqlalchemy.exc.OperationalError: (OperationalError) unable to open
>>> database
>>> > file None None
>>> > Why am I getting this error and how do I fix it?
>>> > Samrat
>>> >
>>> >
>>> >
>>> >
>>>
>>
>>

Re: [flask] Help with configuring SQLAlchemy URI

From:
Jesaja Everling
Date:
2011-07-18 @ 03:16
I think the only way we can find out what is wrong is by looking at
your value for __file__.
I would log it to a file, it will then probably be obvious why it doesn't work:

import logging
file_handler = logging.FileHandler('test.log')
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
app.logger.info('__file__: %s' % __file__)


Best Regards,

Jesaja Everling


On Mon, Jul 18, 2011 at 4:48 AM, Samrat Man Singh
<samratmansingh@gmail.com> wrote:
> I'm not on windows so that's certainly not the problem.
> samrat
>
> On 17 Jul 2011 20:52, "Jonathan Chen" <tamasiaina@gmail.com> wrote:
>> The only thing that comes to mind would be the operating system. It could
>> not like the idea of having mixed "slashes" if you are on windows. I could
>> be wrong.
>>
>> ~Jonathan C.
>>
>>
>> On Sun, Jul 17, 2011 at 6:08 AM, Samrat Man Singh
>> <samratmansingh@gmail.com>wrote:
>>
>>> Jesaja, Thanks for your suggestion; it works fine :)
>>>
>>> But I'd still like to find out why 'sqlite:///%s/test.db' %
>>> os.path.dirname(__file__) doesn't work(it worked just fine for Simblin).
>>> So, if anyone else knows about this please help.
>>>
>>> Samrat
>>>
>>>
>>> On Sun, Jul 17, 2011 at 6:41 PM, Jesaja Everling
>>> <jeverling@gmail.com>wrote:
>>>
>>>> Hi Samrat,
>>>>
>>>> to be honest, I don't know why 'sqlite:///%s/test.db' %
>>>> os.path.dirname(__file__) doesn't work, looks ok to me.
>>>> But did you try just using sqlite:///test.db?
>>>> I think it should work.
>>>>
>>>> Best Regards,
>>>>
>>>> Jesaja Everling
>>>>
>>>>
>>>> On Sun, Jul 17, 2011 at 2:16 PM, Samrat Man Singh
>>>> <samratmansingh@gmail.com> wrote:
>>>> > Hi,
>>>> > I'm trying to use SQLALchemy and when I use
>>>> > SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/test.db',
>>>> > the database initializes painlessly but when I try to do something
>>>> > like
>>>> > SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/test.db' %
>>>> os.path.dirname(__file__)
>>>> > (like in Simblin- https://github.com/eugenkiss/Simblin) so that the
>>>> database
>>>> > file will stay in the application's directory.
>>>> > But the latter one gives me an error-
>>>> > sqlalchemy.exc.OperationalError: (OperationalError) unable to open
>>>> database
>>>> > file None None
>>>> > Why am I getting this error and how do I fix it?
>>>> > Samrat
>>>> >
>>>> >
>>>> >
>>>> >
>>>>
>>>
>>>
>

Re: [flask] Help with configuring SQLAlchemy URI

From:
Samrat Man Singh
Date:
2011-07-18 @ 05:32
This is what I got:

INFO in blogengine [blogengine.py:26]:
__file__: blogengine.py

Shouldn't __file__ have been a 'file' and not a .py file. Maybe that's what
is causing the problem??

Samrat

On Mon, Jul 18, 2011 at 9:01 AM, Jesaja Everling <jeverling@gmail.com>wrote:

> I think the only way we can find out what is wrong is by looking at
> your value for __file__.
> I would log it to a file, it will then probably be obvious why it doesn't
> work:
>
> import logging
> file_handler = logging.FileHandler('test.log')
> file_handler.setLevel(logging.INFO)
> app.logger.addHandler(file_handler)
> app.logger.info('__file__: %s' % __file__)
>
>
> Best Regards,
>
> Jesaja Everling
>
>
> On Mon, Jul 18, 2011 at 4:48 AM, Samrat Man Singh
> <samratmansingh@gmail.com> wrote:
> > I'm not on windows so that's certainly not the problem.
> > samrat
> >
> > On 17 Jul 2011 20:52, "Jonathan Chen" <tamasiaina@gmail.com> wrote:
> >> The only thing that comes to mind would be the operating system. It
> could
> >> not like the idea of having mixed "slashes" if you are on windows. I
> could
> >> be wrong.
> >>
> >> ~Jonathan C.
> >>
> >>
> >> On Sun, Jul 17, 2011 at 6:08 AM, Samrat Man Singh
> >> <samratmansingh@gmail.com>wrote:
> >>
> >>> Jesaja, Thanks for your suggestion; it works fine :)
> >>>
> >>> But I'd still like to find out why 'sqlite:///%s/test.db' %
> >>> os.path.dirname(__file__) doesn't work(it worked just fine for
> Simblin).
> >>> So, if anyone else knows about this please help.
> >>>
> >>> Samrat
> >>>
> >>>
> >>> On Sun, Jul 17, 2011 at 6:41 PM, Jesaja Everling
> >>> <jeverling@gmail.com>wrote:
> >>>
> >>>> Hi Samrat,
> >>>>
> >>>> to be honest, I don't know why 'sqlite:///%s/test.db' %
> >>>> os.path.dirname(__file__) doesn't work, looks ok to me.
> >>>> But did you try just using sqlite:///test.db?
> >>>> I think it should work.
> >>>>
> >>>> Best Regards,
> >>>>
> >>>> Jesaja Everling
> >>>>
> >>>>
> >>>> On Sun, Jul 17, 2011 at 2:16 PM, Samrat Man Singh
> >>>> <samratmansingh@gmail.com> wrote:
> >>>> > Hi,
> >>>> > I'm trying to use SQLALchemy and when I use
> >>>> > SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/test.db',
> >>>> > the database initializes painlessly but when I try to do something
> >>>> > like
> >>>> > SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/test.db' %
> >>>> os.path.dirname(__file__)
> >>>> > (like in Simblin- https://github.com/eugenkiss/Simblin) so that the
> >>>> database
> >>>> > file will stay in the application's directory.
> >>>> > But the latter one gives me an error-
> >>>> > sqlalchemy.exc.OperationalError: (OperationalError) unable to open
> >>>> database
> >>>> > file None None
> >>>> > Why am I getting this error and how do I fix it?
> >>>> > Samrat
> >>>> >
> >>>> >
> >>>> >
> >>>> >
> >>>>
> >>>
> >>>
> >
>

Re: [flask] Help with configuring SQLAlchemy URI

From:
Simon Sapin
Date:
2011-07-18 @ 07:05
Hi,

 > SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/test.db' % 
os.path.dirname(__file__)
> __file__: blogengine.py

Let’s see step-by-step what’s going on here.

     >>> os.path.dirname('blogengine.py')
     ''
     >>> 'sqlite:///%s/test.db' % ''
     'sqlite:////test.db'

The problem is that the empty dirname makes the URI have 4 slashes. 
You’d need to check the SQLAlchemy docs for how many shlashes is 
correct, but maybe it searches for a file named '/test.db', at the root 
of your filesystem.

Try this:

     SQLALCHEMY_DATABASE_URI = ('sqlite:///' +
         os.path.join(os.path.dirname(__file__), 'test.db'))

os.path.join does the right thing:

     >>> os.path.dirname('blogengine.py')
     ''
     >>> os.path.join('', 'test.db')
     'test.db'
     >>> 'sqlite:///' + 'test.db'
     'sqlite:///test.db'

This time you get one slash less which, if I remember correctly, 
SQLAlchemy interprets as a name relative to the current directory.

os.path.join will also do the right thing if the dirname is non-empty, 
relative or absolute.

If relative filenames do not work, try os.path.abspath.

Regards,
-- 
Simon Sapin
http://exyr.org/

Re: [flask] Help with configuring SQLAlchemy URI

From:
Samrat Man Singh
Date:
2011-07-18 @ 07:30
Hi Simon,

SQLALCHEMY_DATABASE_URI = ('sqlite:///' +
os.path.join(os.path.dirname(__file__),'test.db'))
returned sqlite:///test.db
as you've mentioned. But do you have any idea why os.path.dirname(__file__)
is an empty string?

Samrat


On Mon, Jul 18, 2011 at 12:50 PM, Simon Sapin <simon.sapin@exyr.org> wrote:

> Hi,
>
>  > SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/test.db' %
> os.path.dirname(__file__)
> > __file__: blogengine.py
>
> Let’s see step-by-step what’s going on here.
>
>     >>> os.path.dirname('blogengine.py')
>     ''
>     >>> 'sqlite:///%s/test.db' % ''
>     'sqlite:////test.db'
>
> The problem is that the empty dirname makes the URI have 4 slashes.
> You’d need to check the SQLAlchemy docs for how many shlashes is
> correct, but maybe it searches for a file named '/test.db', at the root
> of your filesystem.
>
> Try this:
>
>     SQLALCHEMY_DATABASE_URI = ('sqlite:///' +
>         os.path.join(os.path.dirname(__file__), 'test.db'))
>
> os.path.join does the right thing:
>
>     >>> os.path.dirname('blogengine.py')
>     ''
>     >>> os.path.join('', 'test.db')
>     'test.db'
>     >>> 'sqlite:///' + 'test.db'
>     'sqlite:///test.db'
>
> This time you get one slash less which, if I remember correctly,
> SQLAlchemy interprets as a name relative to the current directory.
>
> os.path.join will also do the right thing if the dirname is non-empty,
> relative or absolute.
>
> If relative filenames do not work, try os.path.abspath.
>
> Regards,
> --
> Simon Sapin
> http://exyr.org/
>

回复: [flask] Help with configuring SQLAlchemy URI

From:
linnchord
Date:
2011-07-18 @ 07:46
os.path.dirname(__file__) 

just return the path that how you refer to the .py when you execute the script.

let's make a test

vim ~/test.py

input 

------------------------------------------
import os

print os.path.dirname(__file__)
------------------------------------------

when
python test.py 

print 
[none]


when
python ./test.py

print 
.



when
python ~/test.py

print 
/Users/[username]/test.py #on mac




-------------------------------
linnchord@gmail.com


在 2011年7月18日星期一,下午3:30,Samrat Man Singh 写道:

> Hi Simon,
> 
> SQLALCHEMY_DATABASE_URI = ('sqlite:///' + 
os.path.join(os.path.dirname(__file__),'test.db')) returned 
sqlite:///test.db
> as you've mentioned. But do you have any idea why 
os.path.dirname(__file__) is an empty string?
> 
> Samrat
> 
> 
> On Mon, Jul 18, 2011 at 12:50 PM, Simon Sapin <simon.sapin@exyr.org 
(mailto:simon.sapin@exyr.org)> wrote:
> >  Hi,
> > 
> > > SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/test.db' %
> >  os.path.dirname(__file__)
> > > __file__: blogengine.py
> > 
> >  Let’s see step-by-step what’s going on here.
> > 
> > >>> os.path.dirname('blogengine.py')
> >  ''
> > >>> 'sqlite:///%s/test.db' % ''
> >  'sqlite:////test.db'
> > 
> >  The problem is that the empty dirname makes the URI have 4 slashes.
> >  You’d need to check the SQLAlchemy docs for how many shlashes is
> >  correct, but maybe it searches for a file named '/test.db', at the root
> >  of your filesystem.
> > 
> >  Try this:
> > 
> >  SQLALCHEMY_DATABASE_URI = ('sqlite:///' +
> >  os.path.join(os.path.dirname(__file__), 'test.db'))
> > 
> >  os.path.join does the right thing:
> > 
> > >>> os.path.dirname('blogengine.py')
> >  ''
> > >>> os.path.join('', 'test.db')
> >  'test.db'
> > >>> 'sqlite:///' + 'test.db'
> >  'sqlite:///test.db'
> > 
> >  This time you get one slash less which, if I remember correctly,
> >  SQLAlchemy interprets as a name relative to the current directory.
> > 
> >  os.path.join will also do the right thing if the dirname is non-empty,
> >  relative or absolute.
> > 
> >  If relative filenames do not work, try os.path.abspath.
> > 
> >  Regards,
> > --
> >  Simon Sapin
> > http://exyr.org/
> 

Re: [flask] Help with configuring SQLAlchemy URI

From:
Peter Ward
Date:
2011-07-18 @ 08:57
Because os.path.dirname gets the directory component of a path.
And it does this without looking at the filesystem - so, it gets a directory
of '', which should be interpreted as the current working directory.

On 18 July 2011 17:30, Samrat Man Singh <samratmansingh@gmail.com> wrote:

> Hi Simon,
>
> SQLALCHEMY_DATABASE_URI = ('sqlite:///' +
> os.path.join(os.path.dirname(__file__),'test.db'))
> returned sqlite:///test.db
> as you've mentioned. But do you have any idea why os.path.dirname(__file__)
> is an empty string?
>
> Samrat
>
>
> On Mon, Jul 18, 2011 at 12:50 PM, Simon Sapin <simon.sapin@exyr.org>wrote:
>
>> Hi,
>>
>>  > SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/test.db' %
>> os.path.dirname(__file__)
>> > __file__: blogengine.py
>>
>> Let’s see step-by-step what’s going on here.
>>
>>     >>> os.path.dirname('blogengine.py')
>>     ''
>>     >>> 'sqlite:///%s/test.db' % ''
>>     'sqlite:////test.db'
>>
>> The problem is that the empty dirname makes the URI have 4 slashes.
>> You’d need to check the SQLAlchemy docs for how many shlashes is
>> correct, but maybe it searches for a file named '/test.db', at the root
>> of your filesystem.
>>
>> Try this:
>>
>>     SQLALCHEMY_DATABASE_URI = ('sqlite:///' +
>>         os.path.join(os.path.dirname(__file__), 'test.db'))
>>
>> os.path.join does the right thing:
>>
>>     >>> os.path.dirname('blogengine.py')
>>     ''
>>     >>> os.path.join('', 'test.db')
>>     'test.db'
>>     >>> 'sqlite:///' + 'test.db'
>>     'sqlite:///test.db'
>>
>> This time you get one slash less which, if I remember correctly,
>> SQLAlchemy interprets as a name relative to the current directory.
>>
>> os.path.join will also do the right thing if the dirname is non-empty,
>> relative or absolute.
>>
>> If relative filenames do not work, try os.path.abspath.
>>
>> Regards,
>> --
>> Simon Sapin
>> http://exyr.org/
>>
>
>


-- 
Peter Ward
http://flowblok.id.au/
BIT III, Sydney University

Re: [flask] Help with configuring SQLAlchemy URI

From:
Simon Sapin
Date:
2011-07-18 @ 08:08
 On Mon, 18 Jul 2011 13:15:34 +0545, Samrat Man Singh wrote:
> But do you have any idea
> why os.path.dirname(__file__) is an empty string?

 First, __file__ is a relative filename because the module was imported 
 with an entry in sys.path that was itself relative. As documented in 
 http://docs.python.org/library/sys.html#sys.path , an empty string in 
 sys.path is a relative name for the current directory.

 Second, os.path.dirname just takes a path and return all but the last 
 component. It does not care if the file exists at all. You could use 
 posixpath.dirname to work on URL fragment if you need to. (os.path is 
 posixpath on POSIX systems.)

     >>> os.path.dirname('a/b/c')
     'a/b'
     >>> os.path.dirname('a/b')
     'a'
     >>> os.path.dirname('a')
     ''

 Returning the empty string on a path with only one component is a bit 
 arbitrary, but I don’t have a better idea of what it should do. The 
 important thing is that it’s coherent with os.path.join.

 Regards,
-- 
 Simon Sapin

Re: [flask] Help with configuring SQLAlchemy URI

From:
Jesaja Everling
Date:
2011-07-18 @ 07:29
Samrat, os.path.dirname(__file__) should give you an absolute path.
That's the reason why it isn't working.
I don't know exactly why it doesn't do that, it must have something to
do with the way app.config.from_object(default_settings) works. But if
you use app.config.from_pyfile('default_settings.py') it works.
__file__ will be an absolute path.

Best Regards,

Jesaja Everling


On Mon, Jul 18, 2011 at 9:05 AM, Simon Sapin <simon.sapin@exyr.org> wrote:
> Hi,
>
>  > SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/test.db' %
> os.path.dirname(__file__)
>> __file__: blogengine.py
>
> Let’s see step-by-step what’s going on here.
>
>     >>> os.path.dirname('blogengine.py')
>     ''
>     >>> 'sqlite:///%s/test.db' % ''
>     'sqlite:////test.db'
>
> The problem is that the empty dirname makes the URI have 4 slashes.
> You’d need to check the SQLAlchemy docs for how many shlashes is
> correct, but maybe it searches for a file named '/test.db', at the root
> of your filesystem.
>
> Try this:
>
>     SQLALCHEMY_DATABASE_URI = ('sqlite:///' +
>         os.path.join(os.path.dirname(__file__), 'test.db'))
>
> os.path.join does the right thing:
>
>     >>> os.path.dirname('blogengine.py')
>     ''
>     >>> os.path.join('', 'test.db')
>     'test.db'
>     >>> 'sqlite:///' + 'test.db'
>     'sqlite:///test.db'
>
> This time you get one slash less which, if I remember correctly,
> SQLAlchemy interprets as a name relative to the current directory.
>
> os.path.join will also do the right thing if the dirname is non-empty,
> relative or absolute.
>
> If relative filenames do not work, try os.path.abspath.
>
> Regards,
> --
> Simon Sapin
> http://exyr.org/
>

Re: [flask] Help with configuring SQLAlchemy URI

From:
Armin Ronacher
Date:
2011-07-18 @ 08:17
Hi,

On 7/18/11 9:29 AM, Jesaja Everling wrote:
> Samrat, os.path.dirname(__file__) should give you an absolute path.
Nope. os.path.dirname(os.path.abspath(__file__)) however does.


Regards,
Armin

Re: [flask] Help with configuring SQLAlchemy URI

From:
Jesaja Everling
Date:
2011-07-18 @ 13:46
Hi Armin,

> Nope. os.path.dirname(os.path.abspath(__file__)) however does.
didn't know that, i thought __file__ would always be absolute, good to know!

Glad you got it working Samrat! :)


Best Regards,

Jesaja Everling


On Mon, Jul 18, 2011 at 10:17 AM, Armin Ronacher
<armin.ronacher@active-4.com> wrote:
> Hi,
>
> On 7/18/11 9:29 AM, Jesaja Everling wrote:
>> Samrat, os.path.dirname(__file__) should give you an absolute path.
> Nope. os.path.dirname(os.path.abspath(__file__)) however does.
>
>
> Regards,
> Armin
>

Re: [flask] Help with configuring SQLAlchemy URI

From:
Samrat Man Singh
Date:
2011-07-18 @ 07:46
Jesaja,

Using from_pyfile instead of from_object!!  Thanks :)

Samrat

On Mon, Jul 18, 2011 at 1:14 PM, Jesaja Everling <jeverling@gmail.com>wrote:

> Samrat, os.path.dirname(__file__) should give you an absolute path.
> That's the reason why it isn't working.
> I don't know exactly why it doesn't do that, it must have something to
> do with the way app.config.from_object(default_settings) works. But if
> you use app.config.from_pyfile('default_settings.py') it works.
> __file__ will be an absolute path.
>
> Best Regards,
>
> Jesaja Everling
>
>
> On Mon, Jul 18, 2011 at 9:05 AM, Simon Sapin <simon.sapin@exyr.org> wrote:
> > Hi,
> >
> >  > SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/test.db' %
> > os.path.dirname(__file__)
> >> __file__: blogengine.py
> >
> > Let’s see step-by-step what’s going on here.
> >
> >     >>> os.path.dirname('blogengine.py')
> >     ''
> >     >>> 'sqlite:///%s/test.db' % ''
> >     'sqlite:////test.db'
> >
> > The problem is that the empty dirname makes the URI have 4 slashes.
> > You’d need to check the SQLAlchemy docs for how many shlashes is
> > correct, but maybe it searches for a file named '/test.db', at the root
> > of your filesystem.
> >
> > Try this:
> >
> >     SQLALCHEMY_DATABASE_URI = ('sqlite:///' +
> >         os.path.join(os.path.dirname(__file__), 'test.db'))
> >
> > os.path.join does the right thing:
> >
> >     >>> os.path.dirname('blogengine.py')
> >     ''
> >     >>> os.path.join('', 'test.db')
> >     'test.db'
> >     >>> 'sqlite:///' + 'test.db'
> >     'sqlite:///test.db'
> >
> > This time you get one slash less which, if I remember correctly,
> > SQLAlchemy interprets as a name relative to the current directory.
> >
> > os.path.join will also do the right thing if the dirname is non-empty,
> > relative or absolute.
> >
> > If relative filenames do not work, try os.path.abspath.
> >
> > Regards,
> > --
> > Simon Sapin
> > http://exyr.org/
> >
>

Re: [flask] Help with configuring SQLAlchemy URI

From:
Peter Ward
Date:
2011-07-18 @ 06:05
That looks fine to me-perhaps you can give a complete minimal example of
your application so we can reproduce the problem?

Some other suggestions:
 * Try printing out the value of the SQLALCHEMY_DATABASE_URI, and see if
it's correct.
 * Is there already a test.db file in the directory, with silly permissions
on it so you can't open it?

Peter

On 18 July 2011 15:32, Samrat Man Singh <samratmansingh@gmail.com> wrote:

> This is what I got:
>
> INFO in blogengine [blogengine.py:26]:
> __file__: blogengine.py
>
> Shouldn't __file__ have been a 'file' and not a .py file. Maybe that's what
> is causing the problem??
>
> Samrat
>
> On Mon, Jul 18, 2011 at 9:01 AM, Jesaja Everling <jeverling@gmail.com>wrote:
>
>> I think the only way we can find out what is wrong is by looking at
>> your value for __file__.
>> I would log it to a file, it will then probably be obvious why it doesn't
>> work:
>>
>> import logging
>> file_handler = logging.FileHandler('test.log')
>> file_handler.setLevel(logging.INFO)
>> app.logger.addHandler(file_handler)
>> app.logger.info('__file__: %s' % __file__)
>>
>>
>> Best Regards,
>>
>> Jesaja Everling
>>
>>
>> On Mon, Jul 18, 2011 at 4:48 AM, Samrat Man Singh
>> <samratmansingh@gmail.com> wrote:
>> > I'm not on windows so that's certainly not the problem.
>> > samrat
>> >
>> > On 17 Jul 2011 20:52, "Jonathan Chen" <tamasiaina@gmail.com> wrote:
>> >> The only thing that comes to mind would be the operating system. It
>> could
>> >> not like the idea of having mixed "slashes" if you are on windows. I
>> could
>> >> be wrong.
>> >>
>> >> ~Jonathan C.
>> >>
>> >>
>> >> On Sun, Jul 17, 2011 at 6:08 AM, Samrat Man Singh
>> >> <samratmansingh@gmail.com>wrote:
>> >>
>> >>> Jesaja, Thanks for your suggestion; it works fine :)
>> >>>
>> >>> But I'd still like to find out why 'sqlite:///%s/test.db' %
>> >>> os.path.dirname(__file__) doesn't work(it worked just fine for
>> Simblin).
>> >>> So, if anyone else knows about this please help.
>> >>>
>> >>> Samrat
>> >>>
>> >>>
>> >>> On Sun, Jul 17, 2011 at 6:41 PM, Jesaja Everling
>> >>> <jeverling@gmail.com>wrote:
>> >>>
>> >>>> Hi Samrat,
>> >>>>
>> >>>> to be honest, I don't know why 'sqlite:///%s/test.db' %
>> >>>> os.path.dirname(__file__) doesn't work, looks ok to me.
>> >>>> But did you try just using sqlite:///test.db?
>> >>>> I think it should work.
>> >>>>
>> >>>> Best Regards,
>> >>>>
>> >>>> Jesaja Everling
>> >>>>
>> >>>>
>> >>>> On Sun, Jul 17, 2011 at 2:16 PM, Samrat Man Singh
>> >>>> <samratmansingh@gmail.com> wrote:
>> >>>> > Hi,
>> >>>> > I'm trying to use SQLALchemy and when I use
>> >>>> > SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/test.db',
>> >>>> > the database initializes painlessly but when I try to do something
>> >>>> > like
>> >>>> > SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/test.db' %
>> >>>> os.path.dirname(__file__)
>> >>>> > (like in Simblin- https://github.com/eugenkiss/Simblin) so that
>> the
>> >>>> database
>> >>>> > file will stay in the application's directory.
>> >>>> > But the latter one gives me an error-
>> >>>> > sqlalchemy.exc.OperationalError: (OperationalError) unable to open
>> >>>> database
>> >>>> > file None None
>> >>>> > Why am I getting this error and how do I fix it?
>> >>>> > Samrat
>> >>>> >
>> >>>> >
>> >>>> >
>> >>>> >
>> >>>>
>> >>>
>> >>>
>> >
>>
>
>


-- 
Peter Ward
http://flowblok.id.au/
BIT III, Sydney University

Re: [flask] Help with configuring SQLAlchemy URI

From:
Samrat Man Singh
Date:
2011-07-18 @ 06:56
Peter,
I tried this

import os

SECRET_KEY = 'abcdefg'
DEBUG = True

SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/blogengine.db' %
os.path.dirname(__file__)

print SQLALCHEMY_DATABASE_URI

and it gave me this- sqlite:////blogengine.db

The code above is my default_settings.py file. I haven't made much progress
with the application yet; still, here it is-

from flask import Flask
import default_settings
from flaskext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object(default_settings)
db = SQLAlchemy(app)

def initdb():
    db.create_all()
    print "Initialized new empty database in %s" %
app.config['SQLALCHEMY_DATABASE_URI']



On Mon, Jul 18, 2011 at 11:50 AM, Peter Ward <peteraward@gmail.com> wrote:

> That looks fine to me-perhaps you can give a complete minimal example of
> your application so we can reproduce the problem?
>
> Some other suggestions:
>  * Try printing out the value of the SQLALCHEMY_DATABASE_URI, and see if
> it's correct.
>  * Is there already a test.db file in the directory, with silly permissions
> on it so you can't open it?
>
> Peter
>
> On 18 July 2011 15:32, Samrat Man Singh <samratmansingh@gmail.com> wrote:
>
>> This is what I got:
>>
>> INFO in blogengine [blogengine.py:26]:
>> __file__: blogengine.py
>>
>> Shouldn't __file__ have been a 'file' and not a .py file. Maybe that's
>> what is causing the problem??
>>
>> Samrat
>>
>> On Mon, Jul 18, 2011 at 9:01 AM, Jesaja Everling <jeverling@gmail.com>wrote:
>>
>>> I think the only way we can find out what is wrong is by looking at
>>> your value for __file__.
>>> I would log it to a file, it will then probably be obvious why it doesn't
>>> work:
>>>
>>> import logging
>>> file_handler = logging.FileHandler('test.log')
>>> file_handler.setLevel(logging.INFO)
>>> app.logger.addHandler(file_handler)
>>> app.logger.info('__file__: %s' % __file__)
>>>
>>>
>>> Best Regards,
>>>
>>> Jesaja Everling
>>>
>>>
>>> On Mon, Jul 18, 2011 at 4:48 AM, Samrat Man Singh
>>> <samratmansingh@gmail.com> wrote:
>>> > I'm not on windows so that's certainly not the problem.
>>> > samrat
>>> >
>>> > On 17 Jul 2011 20:52, "Jonathan Chen" <tamasiaina@gmail.com> wrote:
>>> >> The only thing that comes to mind would be the operating system. It
>>> could
>>> >> not like the idea of having mixed "slashes" if you are on windows. I
>>> could
>>> >> be wrong.
>>> >>
>>> >> ~Jonathan C.
>>> >>
>>> >>
>>> >> On Sun, Jul 17, 2011 at 6:08 AM, Samrat Man Singh
>>> >> <samratmansingh@gmail.com>wrote:
>>> >>
>>> >>> Jesaja, Thanks for your suggestion; it works fine :)
>>> >>>
>>> >>> But I'd still like to find out why 'sqlite:///%s/test.db' %
>>> >>> os.path.dirname(__file__) doesn't work(it worked just fine for
>>> Simblin).
>>> >>> So, if anyone else knows about this please help.
>>> >>>
>>> >>> Samrat
>>> >>>
>>> >>>
>>> >>> On Sun, Jul 17, 2011 at 6:41 PM, Jesaja Everling
>>> >>> <jeverling@gmail.com>wrote:
>>> >>>
>>> >>>> Hi Samrat,
>>> >>>>
>>> >>>> to be honest, I don't know why 'sqlite:///%s/test.db' %
>>> >>>> os.path.dirname(__file__) doesn't work, looks ok to me.
>>> >>>> But did you try just using sqlite:///test.db?
>>> >>>> I think it should work.
>>> >>>>
>>> >>>> Best Regards,
>>> >>>>
>>> >>>> Jesaja Everling
>>> >>>>
>>> >>>>
>>> >>>> On Sun, Jul 17, 2011 at 2:16 PM, Samrat Man Singh
>>> >>>> <samratmansingh@gmail.com> wrote:
>>> >>>> > Hi,
>>> >>>> > I'm trying to use SQLALchemy and when I use
>>> >>>> > SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/test.db',
>>> >>>> > the database initializes painlessly but when I try to do something
>>> >>>> > like
>>> >>>> > SQLALCHEMY_DATABASE_URI = 'sqlite:///%s/test.db' %
>>> >>>> os.path.dirname(__file__)
>>> >>>> > (like in Simblin- https://github.com/eugenkiss/Simblin) so that
>>> the
>>> >>>> database
>>> >>>> > file will stay in the application's directory.
>>> >>>> > But the latter one gives me an error-
>>> >>>> > sqlalchemy.exc.OperationalError: (OperationalError) unable to open
>>> >>>> database
>>> >>>> > file None None
>>> >>>> > Why am I getting this error and how do I fix it?
>>> >>>> > Samrat
>>> >>>> >
>>> >>>> >
>>> >>>> >
>>> >>>> >
>>> >>>>
>>> >>>
>>> >>>
>>> >
>>>
>>
>>
>
>
> --
> Peter Ward
> http://flowblok.id.au/
> BIT III, Sydney University
>