Correct idiom for determining path when frozen in 3.4

M

Mark Summerfield

Hi,

What is the correct idiom for getting the path to a top-level module in 3.3 and 3.4 when the module might be frozen?

At the moment I'm using this:

if getattr(sys, "frozen", False):
path = os.path.dirname(sys.executable)
else:
path = os.path.dirname(__file__)

Thanks!
 
D

Devin Jeanpierre

if getattr(sys, "frozen"): # ‘getattr’ will returnNone by default

No it won't.
Lastly, it's slightly more Pythonic to execute the normal path
unconditionally, and let it raise an exception if there's a problem::

try:
executable = sys.executable
except AttributeError:
executable = __file__
path = os.path.dirname(executable)

Sure, but sys.executable always exists. sys.frozen doesn't, and the
existence or nonexistence is apparently meaningful; so your code does
something different than the original problem statement.

Also, if that weren't the case, I'd really replace that try-except
with getattr(sys, 'executable', __file__)

-- Devin
 
M

Mark Summerfield

Hi,



What is the correct idiom for getting the path to a top-level module in 3.3 and 3.4 when the module might be frozen?



At the moment I'm using this:



if getattr(sys, "frozen", False):

path = os.path.dirname(sys.executable)

else:

path = os.path.dirname(__file__)



Thanks!

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

Thanks for your thoughtful replies.
 
Z

Zachary Ware

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
_fi.__spec__
ModuleSpec(name='_frozen_importlib', loader=<class
'<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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top