Google Groups Home
Help | Sign in
Built-in variable __file__ doesn't work
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Simosito  
View profile
 More options Jun 26, 3:03 am
From: Simosito <simos...@gmail.com>
Date: Thu, 26 Jun 2008 00:03:49 -0700 (PDT)
Local: Thurs, Jun 26 2008 3:03 am
Subject: Built-in variable __file__ doesn't work
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?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniele Varrazzo  
View profile
 More options Jun 26, 6:13 pm
From: Daniele Varrazzo <daniele.varra...@gmail.com>
Date: Thu, 26 Jun 2008 23:13:11 +0100
Local: Thurs, Jun 26 2008 6:13 pm
Subject: Re: [PyInstaller] Built-in variable __file__ doesn't work
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simone Ramacci - Simosito.it  
View profile
 More options Jun 27, 1:25 am
From: "Simone Ramacci - Simosito.it" <simos...@gmail.com>
Date: Fri, 27 Jun 2008 07:25:07 +0200
Local: Fri, Jun 27 2008 1:25 am
Subject: Re: [PyInstaller] Re: Built-in variable __file__ doesn't work
Daniele Varrazzo ha scritto:
Simosito ha scritto:
  
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?
    
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



  
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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Masaru  
View profile
 More options Jun 27, 2:16 am
From: Masaru <themas...@gmx.de>
Date: Fri, 27 Jun 2008 08:16:29 +0200
Local: Fri, Jun 27 2008 2:16 am
Subject: Re: Built-in variable __file__ doesn't work
Simone Ramacci - Simosito.it wrote:

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simone Ramacci - Simosito.it  
View profile
 More options Jun 27, 3:07 am
From: "Simone Ramacci - Simosito.it" <simos...@gmail.com>
Date: Fri, 27 Jun 2008 09:07:57 +0200
Local: Fri, Jun 27 2008 3:07 am
Subject: Re: [PyInstaller] Re: Built-in variable __file__ doesn't work
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Masaru  
View profile
 More options Jun 27, 5:27 am
From: Masaru <themas...@gmx.de>
Date: Fri, 27 Jun 2008 11:27:30 +0200
Local: Fri, Jun 27 2008 5:27 am
Subject: Re: Built-in variable __file__ doesn't work
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simone Ramacci - Simosito.it  
View profile
 More options Jun 27, 7:03 am
From: "Simone Ramacci - Simosito.it" <simos...@gmail.com>
Date: Fri, 27 Jun 2008 13:03:08 +0200
Local: Fri, Jun 27 2008 7:03 am
Subject: Re: [PyInstaller] Re: Built-in variable __file__ doesn't work
Masaru ha scritto:

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Giovanni Bajo  
View profile
 More options Jun 27, 8:02 am
From: Giovanni Bajo <ra...@develer.com>
Date: Fri, 27 Jun 2008 14:02:46 +0200
Local: Fri, Jun 27 2008 8:02 am
Subject: Re: [PyInstaller] Re: Built-in variable __file__ doesn't work
On 6/27/2008 1:03 PM, Simone Ramacci - Simosito.it wrote:

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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simone Ramacci - Simosito.it  
View profile
 More options Jun 27, 8:18 am
From: "Simone Ramacci - Simosito.it" <simos...@gmail.com>
Date: Fri, 27 Jun 2008 14:18:13 +0200
Local: Fri, Jun 27 2008 8:18 am
Subject: Re: [PyInstaller] Re: Built-in variable __file__ doesn't work
Giovanni Bajo ha scritto:

I do. I think you should add it.

    Reply to author    Forward  
You must