在Python中导入哪个库来使用create_shortcut()?

2024-06-28 20:20:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在python3.2中的安装后脚本中使用create_shortcut()函数,http://docs.python.org/distutils/builtdist.html#the-postinstallation-script。每次尝试运行函数时,都会得到以下结果:

NameError: name 'create_shortcut' is not defined

我觉得我缺少了一个导入,但我似乎找不到任何关于如何使其工作的文档。在

编辑 我应该早点明确我的最终目标和我的环境。我正在生成一个.msi,运行以下内容: python设置.pybdist_msi--initial target dir=“C:\path\to\install”--安装脚本=“install.py安装" 这个install.py安装文件与我的设置.py. 在

最终目标是有一个.msi文件,该文件将应用程序安装在指定的目录中,并在指定的位置创建“开始”菜单项。如果安装程序允许用户选择创建“开始”菜单快捷方式或桌面快捷方式,那将是一个不错的选择。在


Tags: install文件函数pyorg脚本httpdocs
2条回答

安装后脚本将在本机Windows安装例程中运行。在

函数get_special_folder_pathdirectory_createddirectory_createdcreate_shortcut不是python函数:例如,它们不能作为关键字-参数对调用-只能按位置调用。函数定义在https://github.com/python/cpython/blob/master/PC/bdist_wininst/install.c

shorcut创建的例程充当系统接口^{}(shell32.dll中的“lives”)的包装器,从第504行开始:

static PyObject *CreateShortcut(PyObject *self, PyObject *args)
{...

然后在第643行链接:

^{pr2}$

在本地安装中,上述C代码已在可执行文件中编译:

C:\Python26\Lib\distutils\command\wininst-6.0.exe
C:\Python26\Lib\distutils\command\wininst-7.1.exe
C:\Python26\Lib\distutils\command\wininst-8.0.exe
C:\Python26\Lib\distutils\command\wininst-9.0.exe
C:\Python26\Lib\distutils\command\wininst-9.0-amd64.exe

所以,回答:没有python库来导入函数create_shortcut(),它只在Windows安装后脚本中可用。在

如果要同时支持自动和手动安装后方案,请查看pywin32解决方案:

https://github.com/mhammond/pywin32/blob/master/pywin32_postinstall.py第80行

try:
    create_shortcut
except NameError:
    # Create a function with the same signature as create_shortcut provided
    # by bdist_wininst
    def create_shortcut(path, description, filename,
                        arguments="", workdir="", iconpath="", iconindex=0):
        import pythoncom
        from win32com.shell import shell, shellcon

        ilink = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None,
                                           pythoncom.CLSCTX_INPROC_SERVER,
                                           shell.IID_IShellLink)
        ilink.SetPath(path)
        ilink.SetDescription(description)
        if arguments:
            ilink.SetArguments(arguments)
        if workdir:
            ilink.SetWorkingDirectory(workdir)
        if iconpath or iconindex:
            ilink.SetIconLocation(iconpath, iconindex)
        # now save it.
        ipf = ilink.QueryInterface(pythoncom.IID_IPersistFile)
        ipf.Save(filename, 0)

如文件所述:

Starting with Python 2.3, a postinstallation script can be specified with the install-script option. The basename of the script must be specified, and the script filename must also be listed in the scripts argument to the setup function.

这些都是仅限windows的选项,您需要在构建模块的可执行安装程序时使用它。尝试:

python setup.py bdist_wininst  help
python setup.py bdist_wininst  install-script postinst.py  pre-install-script preinst.py

这个文件需要放在设置.py文件。在

Some functions especially useful in this context are available as additional built-in functions in the installation script.

这意味着您不必导入任何模块。在

相关问题 更多 >