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
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
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
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
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
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