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?
> 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__).
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?
>>> 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?
>>>> 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. 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
>>>>> 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
>> Hello Simone,
>> here is a little example, which maybe help you.
> 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
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
>>>>>> 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
>>> Hello Simone,
>>> here is a little example, which maybe help you.
>> 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
> 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
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:
>>>>>>> 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
>>>> Hello Simone,
>>>> here is a little example, which maybe help you.
>>> 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
>> 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
> 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:
>>>>>>>> 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
>>>>> Hello Simone,
>>>>> here is a little example, which maybe help you.
>>>> 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
>>> 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
>> 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: