开发者

How can I find additional files installed with my python package from an included script?

开发者 https://www.devze.com 2023-04-01 22:18 出处:网络
I\'ve built a python package that installs with a number of external files. I need to be able to read those files from a script that gets installed with the package. I can see that I can use __file__

I've built a python package that installs with a number of external files. I need to be able to read those files from a script that gets installed with the package. I can see that I can use __file__ inside my script, and it returns something like this:

/Library/Python/2.6/site-packages/M5-0.3.0-py2.6.egg/EGG-INFO/scripts/..
开发者_如何学Python

and so I can navigate to where the files were installed (top level of the .egg directory), but I'm looking for a more "standard" way to determine where my package is installed.


You might want something like the pkgutil.get_data function.

It will read in resources relative to __file__ if your package lives as a standard python package directory structure on the disk, and I think it will read resources out of a zipped egg as well (though I've not used it for that myself).


does os.getcwd() get you anywhere close? (it returns the 'current working directory', usually the one from which you lauched the main script)

what would be wrong with using __file__? If you are looking for any higher level function, it would probably use a similar technique.

import os
d = os.path.dirname(__file__[:__file__.rindex(os.extsep+'egg')])

gives you the directory; 'navigate' to it with

os.startfile(d)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号