Pergunta

If I have a script in my Python package, how can I get it to read other files from the package, since __file__ contains (for example) /usr/bin/myscript and not /usr/lib/python3.3/site-packages/mypackage/myscript?

(The files are just templates, not modules.)

Foi útil?

Solução 2

You could use the ResourceManager API from setuptools' pkg_resources:

import pkg_resources
my_stream = pkg_resources.resource_stream(__name__, "foo.dat")

will open my.package/my/package/foo.dat as the file-like object my_stream for example.

This will work for regular as well as zipped eggs. Note however that this requires setuptools to be installed. For a solution that only uses the standard library (which is to be preferred), see @abarnert's answer.

Outras dicas

Nothing based on a pathname will work, because your package, or your entire site-packages, could be, say, a zipfile, or the files may have been installed somewhere other than inside the package, etc.

The pkgutil module, in particular the get_data function, is the stdlib way to get the contents of a data file that's part of a package.

See Installing Package Data, and Installing Additional Files for more info on making sure the files actually end up there in the first place.

If you want to use setuptools instead of just the stdlib, pkg_resources can make things easier/more flexible.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top