Built-in variable __file__ doesn't work

38 views
Skip to first unread message

Simosito

unread,
Jun 26, 2008, 3:03:49 AM6/26/08
to PyInstaller
Hello everybody!
In my app I had some problems with paths, thus I wrote that little
code:

#python code
path = os.path.dirname(__file__)
if path:
path = path + os.path.sep
else:
path =''
#code end

As you can see I use the built-in variable __file__.
When I run the "compiled" version (Linux) it doesn't start but says
that __file__ haven't been defined.

How can I fix this thing?
Shall I open a new ticket in the bug list?

Daniele Varrazzo

unread,
Jun 26, 2008, 6:13:11 PM6/26/08
to PyIns...@googlegroups.com
Simosito ha scritto:

It is a known behaviour.

If you detect you are running a frozen version of your program (which is when
hasattr(sys, 'frozen') is true) then you can use
os.path.dirname(sys.executable) to know the directory where your executable
is, instead of os.path.dirname(__file__).

-- Daniele

Simone Ramacci - Simosito.it

unread,
Jun 27, 2008, 1:25:07 AM6/27/08
to PyIns...@googlegroups.com
Daniele Varrazzo ha scritto:
Thank you for the fast reply.
I didn't understand well what you wrote.
Can you give me a little example?

Thank you,
Simone Ramacci

Masaru

unread,
Jun 27, 2008, 2:16:29 AM6/27/08
to pyins...@googlegroups.com

Hello Simone,

here is a little example, which maybe help you.

------<example>--------

import sys
import os

def main():
if hasattr(sys, 'frozen'):
this_file_path = sys.executable
else:
this_file_path = __file__
print "This file path is: %s" % this_file_path
print " Root directory: %s" % os.path.dirname(this_file_path)

if __name__ == '__main__':
main()

------</example>--------

--
Masaru

Simone Ramacci - Simosito.it

unread,
Jun 27, 2008, 3:07:57 AM6/27/08
to PyIns...@googlegroups.com
Masaru ha scritto:
Thank you.
But it seems that I cannot use this method in my app :-/

code:

if hasattr(sys, 'frozen'):
path = sys.executable + os.path.sep
print path
else:


path = os.path.dirname(__file__)
if path:
path = path + os.path.sep
else:
path =''

sys.path.append(path+'lang')
try:
module = __import__('astring')
except ImportError:
print "Astring not available. Falling back to default
import default as module

output when compiled:
#> ./MyApp
./MyApp/
Astring not available. Falling back to default
Traceback (most recent call last):
File "<string>", line 34, in <module>
File "/home/simone/Scrivania/pyinstaller-1.3/iu.py", line 334, in
importHook
ImportError: No module named default

I think this happens because sys.executable doesn't give an absolute path.
(but I'm not sure if I'm saying just rubbish or I'm right. The __file__
idea wasn't mine)

I hope you'll help me somehow,
anyway thank you,
simone ramacci


Masaru

unread,
Jun 27, 2008, 5:27:30 AM6/27/08
to pyins...@googlegroups.com
Simone Ramacci - Simosito.it schrieb:

Hello again,

I've tested on a linux system (ubuntu) the scripts and found out,
that both '__file__' and 'sys.executable' ways will not return the
full/absolute path like the python interpreter on windows will do
it.

Consequently you have to complete the relative path into the full
qualified one.
This you can do with "os.path.abspath(path)"

I've also detected a tiny mistake in you script, where you are taking
the "os.path.dirname" from your script if it's a default python file,
but the full name if it's a (frozen) binary.

Here a little example, how you can solve the problem:


if hasattr(sys, 'frozen'):
this = sys.executable
else:
this = __file__

path = ''
if this:
this = os.path.abspath(this)
path = os.path.dirname(this)

## For some debug informations ...
print "DEBUG: this ->", this
print "DEBUG: path ->", path

module_path = path + 'lang' # <--- are you sure, that this
# will result in a correct
# directory for sys.path?
sys.path.append(module_path)


try:
module = __import__('astring')
except ImportError:
print "Astring not available. Falling back to default
import default as module


Hope this will help you,
Masaru

Simone Ramacci - Simosito.it

unread,
Jun 27, 2008, 7:03:08 AM6/27/08
to PyIns...@googlegroups.com
I'm working under Ubuntu and if I do:

path = os.path.dirname(__file__)
if path:
path = path + os.path.sep
else:
path =''
print path

it prints the full path.

Anyway I added abspath, like you suggested both the lines.
My final code is:

if hasattr(sys, 'frozen'):
path = os.path.abspath(os.path.dirname(sys.executable)) + os.path.sep
#print path


else:
path = os.path.dirname(__file__)
if path:

path = os.path.abspath(path) + os.path.sep
else:
path =''
#print path

And IT WORKS!!

Thank you for your help and I hope I'll be helpful to you to one day.

Simone Ramacci

Giovanni Bajo

unread,
Jun 27, 2008, 8:02:46 AM6/27/08
to PyIns...@googlegroups.com

If you think it's useful, I could add this recipe to the PyInstaller wiki.
--
Giovanni Bajo
Develer S.r.l.
http://www.develer.com

Simone Ramacci - Simosito.it

unread,
Jun 27, 2008, 8:18:13 AM6/27/08
to PyIns...@googlegroups.com
Giovanni Bajo ha scritto:
I do. I think you should add it.

Masaru

unread,
Jun 27, 2008, 8:20:30 AM6/27/08
to pyins...@googlegroups.com
Giovanni Bajo schrieb:

I think it's an interesting *general* point for developing platform
independent with python and the script/module __file__ attribute.

Here we have a very painfull different between the linux and the windows
world.

I'm not up to date whether this is known and in progress of fixing in
the python development community, but should be not an explicit part of
pyinstaller (or an other additional library).

--
Masaru

Reply all
Reply to author
Forward
0 new messages