Py2exe and file system

I have a Python application. It loads configuration files (and other files) such as:

_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
CONFIG_DIR = os.path.join(_path, 'conf')

It works great. However, when I package my application with py2exe, bad things happen:

  File "proj\config.pyc", line 8, in <module>
WindowsError: [Error 3] The system cannot find the path specified: 'C:\\proj\
\dist\\library.zip\\conf'

Obviously the wrong way ... What is a more reliable way to do this? I do not want to specify absolute paths in the program, because they can be placed in different folders. Should I just say "if it says that the name of the folder is" library.zip ", then go one more level down to the" dist "folder?

Note that I have fairly nested directory hierarchies ... for example, I have the gui.utils.images module stored in "gui / utils / images.py" and it uses its own path for example to access "gui /images/ok.png ". Right now, the py2exe version would try to access "proj / dist / library.zip / gui / images / ok.png" or something else that just wouldn't work.

+3
source share
5 answers

? , sys.path.append(".." + os.path.sep + "images") ok.png, open("ok.png", "rb"). library.zip, py2exe, , , .

+2

( ):

  • sys.frozen, py2exe , - getattr (sys, 'frozen', '')
  • , ,
  • , sys.executable, , .exe( python.exe, ). - os.path.dirname(sys.executable), , ..

:

frozen = getattr(sys, 'frozen', '')

if not frozen:
    # not frozen: in regular python interpreter
    approot = os.path.dirname(__file__)

elif frozen in ('dll', 'console_exe', 'windows_exe'):
    # py2exe:
    approot = os.path.dirname(sys.executable)

elif frozen in ('macosx_app',):
    # py2app:
    # Notes on how to find stuff on MAC, by an expert (Bob Ippolito):
    # http://mail.python.org/pipermail/pythonmac-sig/2004-November/012121.html
    approot = os.environ['RESOURCEPATH']

- ... py2app. , .

+10

os.path.dirname(os.path.abspath(sys.argv [0])) py2exe, python script ( , exe ) exe.

, , , (.py .exe).

+2

( Windows, Linux Mac). , Python script .

# Get if frozen
is_frozen = bool( getattr(sys, 'frozen', None) )

# Get path variable
path = sys.path if is_frozen else sys.argv

# Get nonempty first element or raise error
if path and path[0]:
    apppath = path[0]
elif is_frozen():
    raise RuntimeError('Cannot determine app path because sys.path[0] is empty.')
else:
    raise RuntimeError('Cannot determine app path in interpreter mode.')

# Make absolute (eliminate symbolic links)
apppath = os.path.abspath( os.path.realpath(apppath) )

# Split and return
appdir, appname = os.path.split(apppath)
+2

.
py2exe (, my_application.exe), dirname(__file__) , python script. , , my_application.exe, my_application.exe.
: my_application.exe , ( ), python . os.path.dirname __file__ my_application.exe.

Thus, this code gives you the root directory for your configuration files or images (in the case of a frozen application, the folder containing the py2exe executable file, otherwise, the folder with which your python script works):

dirname = os.path.dirname(__file__)
if dirname.endswith('.exe'):
    dirname = os.path.split(dirname)[0]

Obviously, you can use more general, portable methods to detect if the file is frozen, as other answers show instead of the method endswith.

0
source

Source: https://habr.com/ru/post/1719132/


All Articles