以管理员身份运行 python 脚本

发布于 12-12 14:10 字数 1232 浏览 1 评论 0原文

我正在使用 py2exe 编写一个安装程序,它需要在管理员中运行才能有权执行各种文件操作。我修改了 py2exe 附带的 user_access_controls 目录中的一些示例代码来创建安装文件。当我在自己的计算机上运行它时,创建/运行生成的 exe 工作正常。但是,当我尝试在未安装 python 的计算机上运行 exe 时,我收到一条错误消息,指出导入模块(本例中为 shutdown 和 os)不存在。我的印象是 py2exe 自动将所有文件依赖项包装到 exe 中,但我想情况并非如此。 py2exe 确实生成了一个名为 library 的 zip 文件,其中包含所有 python 模块,但显然生成的 exe 并未使用它们。基本上我的问题是如何将导入包含在 py2exe 生成的 exe 中。也许需要对我的 setup.py 文件进行修改 - 代码如下:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

颜漓半夏2024-12-19 14:10:19

尝试在设置部分设置 options={'py2exe': {'bundle_files': 1}},zipfile = None 。 Python 将生成没有依赖项的单个 .exe 文件。例子:

from distutils.core import setup
import py2exe

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

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
)
最偏执的依靠2024-12-19 14:10:19

我为你重写你的设置脚本。这会起作用

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"]}
              }
    )

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"]}
              }
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文