librelist archives

« back to archive

Root Path

Root Path

From:
Ishbir Singh
Date:
2011-09-27 @ 14:11
Hey there,

I had a quick question, what is the best way to get the root path of the
Flask application? This means the absolute path to the place where the
__init__.py file or similar file is stored. I need it because I have created
a resources folder in that directory and need to access it from my code.

-- 
Regards,
Ishbir Singh

Re: [flask] Root Path

From:
Jesse Panganiban
Date:
2011-09-27 @ 14:26
On Tuesday, 27 September, 2011 10:11 PM, Ishbir Singh wrote:
> Hey there,
>
> I had a quick question, what is the best way to get the root path of 
> the Flask application? This means the absolute path to the place where 
> the __init__.py file or similar file is stored. I need it because I 
> have created a resources folder in that directory and need to access 
> it from my code.
>
> -- 
> Regards,
> Ishbir Singh
>

Add this to your module:

import sys, os
cmd_folder = os.path.dirname(os.path.abspath(__file__))
if cmd_folder not in sys.path:
     sys.path.insert(0, cmd_folder)

Found this on Stack Overflow: 
http://stackoverflow.com/questions/279237/python-import-a-module-from-a-folder

Hope this helps.

Re: [flask] Root Path

From:
Ishbir Singh
Date:
2011-09-28 @ 13:11
Hey,

I am not trying to import a module, rather trying to load a font file from
the resources folder for use by PIL.

On 27 September 2011 19:56, Jesse Panganiban <me@jpanganiban.com> wrote:

> On Tuesday, 27 September, 2011 10:11 PM, Ishbir Singh wrote:
> > Hey there,
> >
> > I had a quick question, what is the best way to get the root path of
> > the Flask application? This means the absolute path to the place where
> > the __init__.py file or similar file is stored. I need it because I
> > have created a resources folder in that directory and need to access
> > it from my code.
> >
> > --
> > Regards,
> > Ishbir Singh
> >
>
> Add this to your module:
>
> import sys, os
> cmd_folder = os.path.dirname(os.path.abspath(__file__))
> if cmd_folder not in sys.path:
>     sys.path.insert(0, cmd_folder)
>
> Found this on Stack Overflow:
>
> http://stackoverflow.com/questions/279237/python-import-a-module-from-a-folder
>
> Hope this helps.
>



-- 
Regards,
Ishbir Singh

Re: [flask] Root Path

From:
Daniel Lepage
Date:
2011-09-28 @ 16:18
From within any python file, the code

os.path.dirname(os.path.abspath(__file__))

will give you the directory containing that filename. So if your
resources are in mymodule/res, you can add this to
mymodule/__init__.py:

import os
module_dir = os.path.dirname(os.path.abspath(__file__))
resource_dir = os.path.join(module_dir, "res/")

More generally, in any python code you can use a module's __file__
attribute to do the same:

import os
import mymodule
mymodule_dir = os.path.dirname(os.path.abspath(mymodule.__file__))
mymodule_resource_dir = os.path.join(module_dir, "res/")

-- 
Dan

On Wed, Sep 28, 2011 at 9:11 AM, Ishbir Singh <webmaster@ishbir.com> wrote:
> Hey,
>
> I am not trying to import a module, rather trying to load a font file from
> the resources folder for use by PIL.
>
> On 27 September 2011 19:56, Jesse Panganiban <me@jpanganiban.com> wrote:
>>
>> On Tuesday, 27 September, 2011 10:11 PM, Ishbir Singh wrote:
>> > Hey there,
>> >
>> > I had a quick question, what is the best way to get the root path of
>> > the Flask application? This means the absolute path to the place where
>> > the __init__.py file or similar file is stored. I need it because I
>> > have created a resources folder in that directory and need to access
>> > it from my code.
>> >
>> > --
>> > Regards,
>> > Ishbir Singh
>> >
>>
>> Add this to your module:
>>
>> import sys, os
>> cmd_folder = os.path.dirname(os.path.abspath(__file__))
>> if cmd_folder not in sys.path:
>>     sys.path.insert(0, cmd_folder)
>>
>> Found this on Stack Overflow:
>>
>> http://stackoverflow.com/questions/279237/python-import-a-module-from-a-folder
>>
>> Hope this helps.
>
>
>
> --
> Regards,
> Ishbir Singh
>