pyce icon indicating copy to clipboard operation
pyce copied to clipboard

NameError: name '__file__' is not defined

Open varunsapre opened this issue 6 years ago • 4 comments

I have a module that requires the ' __ file __ ' variable for loading a bunch of files. When I run my program without any encryption this works flawlessly. However, after encrypting, the __ file __ attribute is not defined. After digging through 'dir()' a bit, I found that two attribute - ' __ file __ ' and ' __ cached __ ' get lost somewhere after encryption. Is anybody else facing such a problem? Is there a workaround for this?

varunsapre avatar Oct 01 '19 13:10 varunsapre

If it's a module from an installed package you can, and should, use importlib. See this answer https://stackoverflow.com/a/20885799/2181884

If its just a module as in a loose file, then it would depend on how you are calling it.

Den tis 1 okt. 2019 15:34Varun Sapre [email protected] skrev:

I have a module that requires the 'file' variable for loading a bunch of files. When I run my program without any encryption this works flawlessly. However, after encrypting, the file attribute is not defined. After digging through 'dir()' a bit, I found that two attribute - ' file' and 'cached' get lost somewhere after encryption. Is anybody else facing such a problem? Is there a workaround for this?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/soroco/pyce/issues/27?email_source=notifications&email_token=ADYQU5AKZJKLLOHRPZZRSQTQMNGVHA5CNFSM4I4JSJU2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HO3EAPQ, or mute the thread https://github.com/notifications/unsubscribe-auth/ADYQU5DEBQDUTQ4DNV7LONTQMNGVHANCNFSM4I4JSJUQ .

rickardcronholm avatar Oct 01 '19 19:10 rickardcronholm

Thanks @rickardcronholm! Using importlib worked fine for the problem I was facing. However, is __ file __ not getting defined expected behaviour? I understand why __ file __ does not work when served from a zip, is it the same reason here?

varunsapre avatar Oct 03 '19 13:10 varunsapre

Hey @varunsapre it is intentional as many things which assume __file__ no longer work with the encrypted version.

I will try and dig up the exact technical reason we didn't re-set that up in private communication and update you here.

theonewolf avatar Oct 06 '19 06:10 theonewolf

I ran into this issue recently. Looking into the https://docs.python.org/3/library/importlib.html#module-importlib.resources work-around suggested on the stackoverflow issue, but I'm curious why __file__ doesn't work.

jobevers avatar Apr 04 '22 18:04 jobevers