How to Get the Executable's Current Directory in Py2Exe

Get path to exe file on py2exe

_file = os.path.abspath(sys.argv[0])
path = os.path.dirname(_file)

Determining application path in a Python EXE generated by pyInstaller

I found a solution. You need to check if the application is running as a script or as a frozen exe:

import os
import sys

config_name = 'myapp.cfg'

# determine if application is a script file or frozen exe
if getattr(sys, 'frozen', False):
application_path = os.path.dirname(sys.executable)
elif __file__:
application_path = os.path.dirname(__file__)

config_path = os.path.join(application_path, config_name)

py2exe and the file system

What do you think about using relative paths for all of the included files? I guess it should be possible to use sys.path.append(".." + os.path.sep + "images") for your example about ok.png, then you could just open("ok.png", "rb"). Using relative paths should fix the issues with the library.zip file that's generated by py2exe, at least that's what it does for me.

How do I get the path of the current executed file in Python?

You can't directly determine the location of the main script being executed. After all, sometimes the script didn't come from a file at all. For example, it could come from the interactive interpreter or dynamically generated code stored only in memory.

However, you can reliably determine the location of a module, since modules are always loaded from a file. If you create a module with the following code and put it in the same directory as your main script, then the main script can import the module and use that to locate itself.

some_path/module_locator.py:

def we_are_frozen():
# All of the modules are built-in to the interpreter, e.g., by py2exe
return hasattr(sys, "frozen")

def module_path():
encoding = sys.getfilesystemencoding()
if we_are_frozen():
return os.path.dirname(unicode(sys.executable, encoding))
return os.path.dirname(unicode(__file__, encoding))

some_path/main.py:

import module_locator
my_path = module_locator.module_path()

If you have several main scripts in different directories, you may need more than one copy of module_locator.

Of course, if your main script is loaded by some other tool that doesn't let you import modules that are co-located with your script, then you're out of luck. In cases like that, the information you're after simply doesn't exist anywhere in your program. Your best bet would be to file a bug with the authors of the tool.

Is there a way to specify the build directory for py2exe

Any option that you can set on the command line you can set either through a setup.cfg file or in your setup.py file.

-d is a shortcut for --dist-dir which you can add to the py2xe dict in the dictionary passed to the options keyword param of setup as 'dist_dir':

from distutils.core import setup
import py2exe

# equivalent command line with options is:
# python setup.py py2exe --compressed --bundle-files=2 --dist-dir="my/dist/dir" --dll-excludes="w9xpopen.exe"
options = {'py2exe': {
'compressed':1,
'bundle_files': 2,
'dist_dir': "my/dist/dir"
'dll_excludes': ['w9xpopen.exe']
}}
setup(console=['myscript.py'], options=options)

You could also put setup.cfg next to your setup.py file:

[py2exe]
compressed=1
bundle_files=2
dist_dir=my/dist/dir
dll_excludes=w9xpopen.exe

The build directory (--build-base) is an option of the build command so you can add it to one of the config files (or the setup.py) as:

[build]
build_base=my/build/dir

Created an .exe with py2exe, but when I copy paste from dist folder to desktop, doesn't run properly

In the dist directory is there a set of .dll files needed to run the exe file.



Related Topics



Leave a reply



Submit