[英]Which lib do I import to use create_shortcut() in Python?
I am trying to use the create_shortcut() function in a post-installation script in Python 3.2, per http://docs.python.org/distutils/builtdist.html#the-postinstallation-script .我正在尝试在 Python 3.2 的安装后脚本中使用 create_shortcut() function,根据http://docs.python.org/distutils/builddist.html#the-postinstallation-script 。 Every time I try to run the function, i get the following:
每次我尝试运行 function 时,我都会得到以下信息:
NameError: name 'create_shortcut' is not defined
I feel like I am missing an import, but I can't seem to find any documentation anywhere on how to get this working.我觉得我错过了一个导入,但我似乎无法在任何地方找到任何关于如何让它工作的文档。
EDIT I should have specified earlier my final goal and my environment.编辑我应该早点指定我的最终目标和我的环境。 I am building an.msi running the following: python setup.py bdist_msi --initial-target-dir="C:\path\to\install" --install-script="install.py" The install.py file lives in the same directory as my setup.py.
我正在构建运行以下命令的 an.msi: python setup.py bdist_msi --initial-target-dir="C:\path\to\install" --install-script="install.py" install.py 文件存在在与我的 setup.py 相同的目录中。
The final goal is to have a.msi file that installs the application in the specified directory and creates a Start Menu item in a specified location.最终目标是有一个 .msi 文件,将应用程序安装在指定目录中,并在指定位置创建开始菜单项。 It would be a nice to have if the installer allows the user to select to create the Start Menu shortcut or a desktop shortcut.
如果安装程序允许用户 select 创建“开始”菜单快捷方式或桌面快捷方式,那就太好了。
Postinstallation script is to run in the native Windows installation routine.安装后脚本将在本机 Windows 安装例程中运行。
Fuctions get_special_folder_path
, directory_created
, directory_created
and create_shortcut
are not pythonic: for example, they cannot be called as keyword-argument pairs - only positionally. get_special_folder_path
、 directory_created
、 directory_created
和create_shortcut
不是 pythonic 的:例如,它们不能被称为关键字参数对 - 只能在位置上调用。 The functions are defined at https://github.com/python/cpython/blob/master/PC/bdist_wininst/install.c这些函数定义在https://github.com/python/cpython/blob/master/PC/bdist_wininst/install.c
A routine for the shorcut creation acts as a wrapper around system interface IShellLink
( "lives" in shell32.dll ) and starts at line 504:创建快捷方式的例程充当系统接口
IShellLink
的包装器(在 shell32.dll 中“存在”)并从第 504 行开始:
static PyObject *CreateShortcut(PyObject *self, PyObject *args)
{...
and then is linked at line 643:然后链接到第 643 行:
PyMethodDef meth[] = {
{"create_shortcut", CreateShortcut, METH_VARARGS, NULL},
{"get_special_folder_path", GetSpecialFolderPath, METH_VARARGS, NULL},
...
};
In your local installation the above C code is already compiled in the executables:在您的本地安装中,上述 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
So, answer: There is no python lib to import the function create_shortcut(), it is available as-is only inside a Windows postinstallation script.所以,回答:没有 python 库来导入 function create_shortcut(),它仅在 Windows 安装后脚本中可用。
If you want to support both automatic and manual postinstall scenarios, look at the pywin32 workaround:如果您想同时支持自动和手动安装后方案,请查看 pywin32 解决方法:
https://github.com/mhammond/pywin32/blob/master/pywin32_postinstall.py line 80 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)
As the documentation says:正如文档所说:
Starting with Python 2.3, a postinstallation script can be specified with the --install-script option .
从 Python 2.3 开始,可以使用 --install-script 选项指定安装后脚本。 The basename of the script must be specified, and the script filename must also be listed in the scripts argument to the setup function .
必须指定脚本的基本名称,脚本文件名也必须列在设置 function 的脚本参数中。
These are windows only options, you need to use it when you build a executable installer of your module.这些是 windows 唯一的选项,您需要在构建模块的可执行安装程序时使用它。 Try:
尝试:
python setup.py bdist_wininst --help
python setup.py bdist_wininst --install-script postinst.py --pre-install-script preinst.py
This file needs to go in the "script" section of your setup.py file.该文件需要位于 setup.py 文件的“脚本”部分中的 go。
Some functions especially useful in this context are available as additional built-in functions in the installation script.
一些在此上下文中特别有用的函数可作为安装脚本中的附加内置函数使用。
This means you don't have to import any module.这意味着您不必导入任何模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.