Problem accessing config files within a Python egg

前端 未结 2 812
滥情空心
滥情空心 2021-02-14 03:08

I have a Python project that has the following structure:

package1
  class.py
  class2.py
  ...
package2
  otherClass.py
  otherClass2.py
  ...
config
  dev_sett         


        
相关标签:
2条回答
  • 2021-02-14 03:52

    The problem is, the config files are not files anymore - they're packaged within the egg. It's not easy to find the answer in the docs, but it is there. From the setuptools developer's guide:

    Typically, existing programs manipulate a package's __file__ attribute in order to find the location of data files. However, this manipulation isn't compatible with PEP 302-based import hooks, including importing from zip files and Python Eggs.

    To access them, you need to follow the instructions for the Resource Management API.

    In my own code, I had this problem with a logging configuration file. I used the API successfully like this:

    from pkg_resources import resource_stream
    
    _log_config_file = 'logging.conf'
    _log_config_location = resource_stream(__name__, _log_config_file)
    logging.config.fileConfig(_log_config_location)
    _log = logging.getLogger('package.module')
    
    0 讨论(0)
  • 2021-02-14 03:57

    See Setuptools' discussion of accessing pacakged data files at runtime. You have to get at your configuration file a different way if you want the script to work inside an egg. Also, for that to work, you may need to make your config directory a Python package by tossing in an empty __init__.py file.

    0 讨论(0)
提交回复
热议问题