FAQ
Hello,

I use a data file that lies on disk in the same directory that the
module which makes use of it.

The data file is checked in the repository and gets installed by
the distutils ``package_data`` directive, so it is in place both
during development and after and install.

In my program, I need to find the absolute path to this data file.

I could think of 3 ways to do this:
1. using the __path__ property in the module
2. using __file__ property in the module
3. using __file__ and os.getcwd() in the module

The first option does not work, because __path__ is initialised after the
module is loaded (apparently).

Solution 2 works, but behaves differently when ran from python or
from a doctest file. The path in __file__ is absolute if the program
is ran directly by ``python``, and relative if it is ran by
``python -m doctest``.

Solution 3 works. However if there is a better way to do this, please let me
know.

I could not find anything documenting what the proper value for __file__
should be. Maybe my problem with solution 2 is a bug in doctest or runpy?

I put up a simple example exhibiting the problem.

Here are the results on my mac (using python from macport)
I get the same results using python 2.6, 2.7 and 3.1

$ python2.6 test.py
/Users/seb/Devel/arboris-python/src/pyfile
/Users/seb/Devel/arboris-python/src/pyfile/mod.pyc
/Users/seb/Devel/arboris-python/src/pyfile
/Users/seb/Devel/arboris-python/src/pyfile

$ python2.6 -m doctest test.txt
**********************************************************************
File "test.txt", line 5, in test.txt
Failed example:
print mod.__file__
Expected:
/Users/seb/Devel/arboris-python/src/pyfile/mod.pyc
Got:
mod.pyc
**********************************************************************
File "test.txt", line 7, in test.txt
Failed example:
print(mod.whereami)
Expected:
/Users/seb/Devel/arboris-python/src/pyfile
Got:
<BLANKLINE>
**********************************************************************
File "test.txt", line 9, in test.txt
Failed example:
print(mod.whereami2)
Expected:
/Users/seb/Devel/arboris-python/src/pyfile
Got:
/Users/seb/Devel/arboris-python/src/pyfile/
**********************************************************************
1 items had failures:
3 of 6 in test.txt
***Test Failed*** 3 failures.

Cheers
Sebastian
-------------- next part --------------
import os
print(os.getcwd())
/Users/seb/Devel/arboris-python/src/pyfile
import mod
print(mod.__file__)
/Users/seb/Devel/arboris-python/src/pyfile/mod.pyc
print(mod.whereami)
/Users/seb/Devel/arboris-python/src/pyfile
print(mod.whereami2)
/Users/seb/Devel/arboris-python/src/pyfile
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mod.py
Type: application/octet-stream
Size: 192 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20101001/4ac4f9cf/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.py
Type: application/octet-stream
Size: 100 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20101001/4ac4f9cf/attachment-0001.obj>

Search Discussions

  • Christian Heimes at Oct 1, 2010 at 11:47 am

    Am 01.10.2010 13:00, schrieb S?bastien Barth?lemy:
    Hello,

    I use a data file that lies on disk in the same directory that the
    module which makes use of it.

    The data file is checked in the repository and gets installed by
    the distutils ``package_data`` directive, so it is in place both
    during development and after and install.

    In my program, I need to find the absolute path to this data file.

    I could think of 3 ways to do this:
    1. using the __path__ property in the module
    2. using __file__ property in the module
    3. using __file__ and os.getcwd() in the module
    Use the abspath function of the os.path module:

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

    Christian
  • Arnaud Delobelle at Oct 1, 2010 at 4:43 pm

    S?bastien Barth?lemy <barthelemy at crans.org> writes:

    Hello, Hi!
    I use a data file that lies on disk in the same directory that the
    module which makes use of it.

    The data file is checked in the repository and gets installed by
    the distutils ``package_data`` directive, so it is in place both
    during development and after and install.

    In my program, I need to find the absolute path to this data file.

    I could think of 3 ways to do this:
    1. using the __path__ property in the module
    2. using __file__ property in the module
    3. using __file__ and os.getcwd() in the module
    I have used the following, but I don't know either if it is a good way:

    os.path.dirname(os.path.realpath(__file__))

    --
    Arnaud
  • Sébastien Barthélemy at Oct 1, 2010 at 7:00 pm
    Hi,

    Arnaud, Christian, thank you for your help.

    I'll use abspath, it's shorter.

    Any idea why it's sometimes absolute, sometimes not?
  • Antoine Pitrou at Oct 1, 2010 at 7:22 pm

    On Fri, 1 Oct 2010 21:00:02 +0200 S?bastien Barth?lemy wrote:
    Hi,

    Arnaud, Christian, thank you for your help.

    I'll use abspath, it's shorter.

    Any idea why it's sometimes absolute, sometimes not?
    AFAICT, that's because sys.path contains some absolute paths and some
    relative ones.

    Regards

    Antoine.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedOct 1, '10 at 11:00a
activeOct 1, '10 at 7:22p
posts5
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase