Correct idiom for determining path when frozen in 3.4

Zachary Ware zachary.ware+pylist at gmail.com
Mon Mar 17 12:03:10 EDT 2014


On Mon, Mar 17, 2014 at 10:31 AM, Mark Summerfield <list at qtrac.plus.com> wrote:
> My code was adapted from this:
> http://cx-freeze.readthedocs.org/en/latest/faq.html#using-data-files
>
> When you freeze a Python program with cx_Freeze, sys.freeze exists; but otherwise it doesn't.
>
> I develop some programs which I freeze for distribution but during development I test them as-is so I need to be able to distinguish.
>
> Also, from 3.4, __file__ won't exist in frozen modules:
> http://docs.python.org/3.4/whatsnew/3.4.html#changes-in-the-python-api

Have a look at __spec__, specifically __spec__.origin:

>>> import sys
>>> _fi = sys.modules['_frozen_importlib']
>>> _fi
<module '_frozen_importlib' (frozen)>
>>> _fi.__spec__
ModuleSpec(name='_frozen_importlib', loader=<class
'_frozen_importlib.FrozenImporter'>, origin='<frozen>')
>>> _fi.__spec__.origin
'<frozen>'

I've not looked up in the importlib docs to confirm, but that should
mean that frozen modules should have a __spec__.origin that contains
"frozen".

HTH,
-- 
Zach



More information about the Python-list mailing list