2

Let's say that I do have same data files that are included in my python package.

I understand that the usage of __file__ is not recommended by distils because you will not be able to get a valid path if your module is inside an egg file.

Now, which method should I use in oder to be able to read these files, being inside eggs or outside them?

sorin
  • 161,544
  • 178
  • 535
  • 806
  • 1
    http://stackoverflow.com/questions/5897666/how-do-i-use-data-in-package-data-from-source-code – jterrace Dec 02 '11 at 23:25
  • So - the answer to the question linked above is the answwer to this question, and can't be made better :-) I will call for closing this question – jsbueno Dec 03 '11 at 02:35
  • While I consider the other question essential for solving this one, this has a quite different target, in fact this has to work even if you do not have bundle, while the other is specific to them. Probably I will write the answer myself this weekend :) – sorin Dec 09 '11 at 18:55
  • @jsbueno as stated by Jeff ;) having two questions with the same answer is not enough for considering them duplicate. The only reason for closing as duplicate would be if the questions are too similar, almost identical. – sorin Jun 12 '12 at 09:49

1 Answers1

2

Using the other question linked in the comments, I got a solution that works pretty well for Python 2.6+. In particular, in my own module, I have both code and data files like so:

mymod/
|
| __init__.py
| code.py
| data.dat

To get access to data.dat in code.py, I did this:

import pkgutil
def get_data():
    return pkgutil.get_data('', 'data.dat')
sorin
  • 161,544
  • 178
  • 535
  • 806
Igor Serebryany
  • 3,307
  • 3
  • 29
  • 41