[Tutor] Data Directory under site-packages

Terry Carroll carroll at tjc.com
Thu Nov 11 03:45:44 CET 2010


On Wed, 10 Nov 2010, Greg Lindstrom wrote:

> I'm writing my first module that I intend to put under our company's
> "site-packages" directory for everyone to use in their programs.  The
> problem I'm having is that I want to place files in a data directory under
> the module directory (under site-packages) and I don't know how to set the
> path so I pick up the files.  If I use open('./data/myfile') I get the path
> of the file importing the module (which could be just about anywhere).  I've
> tried various combinations using os.path.abspath() and os.path.dirname() but
> have the same problem.  Is there a way I can use files in the subdirectory
> (I really do not want dozens more files in the main directory)?

I'm not sure I follow.

You want to put data, i.e., non-python code, in the import path?  That 
sounds unusual to me.

You can find the filename from which a module is imported with the 
module's __file__ attribute; and then os.path.dirname() can get you the 
directory.  So if you wanted to address a subdirectory named "data" in the 
same directory from which you imported a given module, or a file 
"myfile.txt" in that subdirectory, that's possible.

Using the sqlite module as an example on my system:

>>> import sqlite3
>>> sqlite3.__file__
'C:\\Python26\\lib\\sqlite3\\__init__.pyc'
>>> import os
>>> os.path.dirname(sqlite3.__file__)
'C:\\Python26\\lib\\sqlite3'
>>> os.path.join(os.path.dirname(sqlite3.__file__), "data")
'C:\\Python26\\lib\\sqlite3\\data'
>>> os.path.join(os.path.dirname(sqlite3.__file__), "data", "myfile.txt")
'C:\\Python26\\lib\\sqlite3\\data\\myfile.txt'


Is this the kind of thing you're thinking of?

Again, it's highly unusual to put non-code data in the import path; I've 
never heard of this being done before, and it makes me shiver in 
revulsion.  I'm not sure I can articulate exactly what bad effects it will 
have, apart from the inherent messiness of it, but I don't like it.



More information about the Tutor mailing list