I'm writing a Django application that is using pip & virtualenv to manage its development environment.
One of the dependencies, pkgme , comes with many data files which are its "backends" and are configured in its setup.py with data_files=$FOO
(rather than package_data
).
When pkgme looks for its backends, it looks in os.path.join(sys.prefix, "share", "pkgme", "backends")
. This works great when pkgme has been installed normally, and seems to match the documentation but does not work when pkgme is installed as an egg.
There, the data files are installed under $VIRTUAL_ENV/lib/python2.7/site-packages/pkgme-0.1-py2.7.egg/share
rather than the expected $VIRTUAL_ENV/share
.
Which leaves me with two questions:
os.path.join
above to find the data files regardless of whether we are using an egg installation or a traditional system installation? If so, what? Note that I know about pkgutil.get_data
, but would rather not use it. I'm not interested in the contents of these data files, I want to know their location instead, so I can execute them.
My current plan is to do this:
package_data
instead of data_files
pkgme.__file__
rather than sys.prefix
Your current plan is essentially correct, or is at any rate a workable option.
When setuptools creates an egg, it checks whether code in the egg makes use of __file__
, and if so, it marks the egg as not being installable in compressed form. In this way, when the egg is installed by easy_install, it'll get extracted to an .egg/
directory instead of being left in an .egg
file.
If you want to support compressed/drop-in installation (ie, just dumping the egg in a directory without "installing" it), then you should use the pkg_resources.resource_filename()
(docs here) API instead of __file__
, but then your package will be dependent on setuptools or distribute in order to have that API available.
I ended up doing the following:
pkg_resources.resource_filename()
to find its own included backends sys.prefix
-based check for any backend that don't want to use Python The diff can be found here: http://bazaar.launchpad.net/~pkgme-committers/pkgme/trunk/revision/86
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.