Ben
Ben

Reputation: 1142

running a python script as administrator

i'm writing an installer using py2exe which needs to run in admin to have permission to perform various file operations. i've modified some sample code from the user_access_controls directory that comes with py2exe to create the setup file. creating/running the generated exe works fine when i run it on my own computer. however, when i try to run the exe on a computer that doesn't have python installed, i get an error saying that the import modules (shutil and os in this case) do not exist. it was my impression that py2exe automatically wraps all the file dependencies into the exe but i guess that this is not the case. py2exe does generate a zip file called library that contains all the python modules but apparently they are not used by the generated exe. basically my question is how do i get the imports to be included in the exe generated by py2exe. perhaps modification need to be made to my setup.py file - the code for this is as follows:

from distutils.core import setup
import py2exe

# The targets to build
# create a target that says nothing about UAC - On Python 2.6+, this
# should be identical to "asInvoker" below.  However, for 2.5 and
# earlier it will force the app into compatibility mode (as no
# manifest will exist at all in the target.)
t1 = dict(script="findpath.py",
          dest_base="findpath",
          uac_info="requireAdministrator")
console = [t1]

# hack to make windows copies of them all too, but
# with '_w' on the tail of the executable.
windows = [{'script': "findpath.py",
            'uac_info': "requireAdministrator",
            },]

setup(
    version = "0.5.0",
    description = "py2exe user-access-control",
    name = "py2exe samples",
    # targets to build
    windows = windows,
    console = console,
    )

Upvotes: 1

Views: 4991

Answers (2)

Hilary Okoro
Hilary Okoro

Reputation: 341

I rewrite your setup script for you. This will work

from distutils.core import setup
import py2exe

# The targets to build
# create a target that says nothing about UAC - On Python 2.6+, this
# should be identical to "asInvoker" below.  However, for 2.5 and
# earlier it will force the app into compatibility mode (as no
# manifest will exist at all in the target.)
t1 = dict(script="findpath.py",
          dest_base="findpath",
          uac_info="requireAdministrator")
console = [t1]

# hack to make windows copies of them all too, but
# with '_w' on the tail of the executable.
windows = [{'script': "findpath.py",
            'uac_info': "requireAdministrator",
            },]

setup(
    version = "0.5.0",
    description = "py2exe user-access-control",
    name = "py2exe samples",
    # targets to build
    windows = windows,
    console = console,
    #the options is what you fail to include it will instruct py2exe to include these modules explicitly
    options={"py2exe":
               {"includes": ["sip","os","shutil"]}
              }
    )

Upvotes: 0

Aleksei
Aleksei

Reputation: 307

Try to set options={'py2exe': {'bundle_files': 1}}, and zipfile = None in setup section. Python will make single .exe file without dependencies. Example:

from distutils.core import setup
import py2exe

setup(
    console=['watt.py'],
    options={'py2exe': {'bundle_files': 1}},
    zipfile = None
)

Upvotes: 2

Related Questions