简体   繁体   English

如何为Windows Box编写安装程序(安装python,python模块和其他依赖项)?

[英]how to write installer (installing python, python modules and other dependencies) for windows boxes?

I know nothing on this subject, but I need suggestions about the best tools or method for creating a setup program that installs python, some custom python modules, some other python modules such as PIL, and some EXE dependencies, all living on a network repository, on windows machines. 我对此主题一无所知,但我需要有关创建安装程序的最佳工具或方法的建议,这些安装程序将安装在python,一些自定义python模块,一些其他python模块(例如PIL)和一些EXE依赖项中,这些都位于网络存储库中,在Windows计算机上。 In the repository are installers for python (msi file), PIL (exe file), the custom python modules (pyc files), and two windows executables (and exe file and a zip file). 存储库中包含python(msi文件),PIL(exe文件),自定义python模块(pyc文件)和两个Windows可执行文件(以及exe文件和zip文件)的安装程序。 Any advice welcome. 任何意见欢迎。

You can do this with any of the installer applications out there. 您可以使用任何安装程序来执行此操作。 Each of the dependent installers has a silent install option, so your installer just needs to invoke the installers for each of the dependencies in the right order. 每个依赖安装程序都有一个静默安装选项,因此您的安装程序只需要按正确的顺序为每个依赖项调用安装程序。 I won't recommend any windows installer application in particular because I don't like any of them, but they will all do what you want. 我不会特别推荐任何Windows Installer应用程序,因为我不喜欢它们中的任何一个,但是它们都会满足您的要求。

The other option you have is to use py2exe which can bundle everything into a single exe file that runs in its own python environment. 您拥有的另一个选择是使用py2exe,它将所有内容捆绑到一个在自己的python环境中运行的exe文件中。 The plus side to this is you don't have to worry about installing Python in the users environment and have the user potentially uninstall python and then have your app stop working because everything is in a standalone environment. 好的一面是,您不必担心在用户环境中安装Python,而不必担心用户可能会卸载python,然后让您的应用停止运行,因为一切都在独立的环境中进行。

Other ways that I have seen this done is with a custom exe written in whatever compiled Windows Language you prefer that does all this for you, but this takes a lot of work. 我看到的其他方式是使用自定义exe编写,该exe用您喜欢的任何已编译Windows语言编写,可以为您完成所有这些工作,但这需要大量工作。

You could also get the advantage of the py2exe route with a little work on an installer you write with either an installer app or a standalone exe that handles the install, by manually placing the python.exe, dll and related code in the proper directories relative to your application code. 通过手动将python.exe,dll和相关代码放置在相对的正确目录中,您还可以通过使用安装程序应用程序或处理安装的独立exe编写的安装程序上的一些工作来获得py2exe路由的优势。您的应用程序代码。 You may have to mess with your PYTHONPATH environment setting when your app starts to get everything working, but this way you don't have to worry about installing Python and whether the user already has Python installed or if they uninstall it because then you have the Python version you need bundled with your app. 当您的应用开始使一切正常运行时,您可能不得不弄混PYTHONPATH环境设置,但是通过这种方式,您不必担心安装Python以及用户是否已经安装了Python或是否卸载了它,因为这样您就可以您需要将Python版本与应用捆绑在一起。

One thing to note is that if you are worried about size the Python installer itself is about 10 MB before any dependencies, but a lot of that is not relevant to an end user using your app, There is no Python Runtime Environment installer like there is a Java runtime Environment installer that just install what you need to run Python, you always get the development tools. 需要注意的一件事是,如果您担心Python安装程序本身的大小在没有任何依赖项之前大约为10 MB,但是其中很多与使用您的应用程序的最终用户无关,那么就没有Python Runtime Environment安装程序了,一个Java运行时环境安装程序,只需安装运行Python所需的程序,您总会得到开发工具。

Hope this helps a little. 希望这有所帮助。

Nice response Michael. 迈克尔,反应很好。

But, py2exe can intimidate the more novice user, for them it may be easier to use pyinstaller 1.5.1 (There's 2.x available, but it over complicated itself, although it has simple and powerful library hooking [ which you will most likely not need]). 但是,py2exe可能会威胁到更多的新手用户,因为他们可能更容易使用pyinstaller 1.5.1(有2.x可用,但它覆盖了复杂的本身,尽管它具有简单而强大的库挂钩[您很可能不会需要])。

All pyinstaller requires is to be Configured (Automatic with Configure.py), and the main source file of your executable, and it will take care of the rest. pyinstaller所需的所有东西都将被配置(使用Configure.py自动运行)以及可执行文件的主要源文件,其余部分将由它来处理。

After downloading pyinstaller, you can set it's path as an environment variable, and you can now make an executable in about three lines in a batch or cmd. 下载pyinstaller之后,您可以将其路径设置为环境变量,并且现在可以在批处理或cmd中大约三行中创建一个可执行文件。

I usually have a batch script made that will allow me to easily change a few labels, drop the script in the directory of my choosing, and make the executable in one click. 通常,我会制作一个批处理脚本,使我可以轻松地更改一些标签,将脚本放到所选目录中,然后单击即可生成可执行文件。

Your script might look something like: 您的脚本可能类似于:

python %pyinstaller%/Configure.py

python %pyinstaller%/Makespec.py -F --icon=C:\Path\To\MyIcon.ico -n ExecutableName 
       C:\Path\To\Source\Main.py

python %pyinstaller%/Build.py ExecutableName.spec

Also, you may have to include a pseudo pause if Build.py is called before the spec file is available. 另外,如果在规格文件可用之前调用Build.py,则可能必须包含伪暂停。 I put this line: 我把这一行:

ping -n 2 127.0.0.1>nul

Between each %pyinstaller% command to make sure that nothing is called before it is ready, and because "pause" requires a key entry to continue. 在每个%pyinstaller%命令之间,以确保在准备就绪之前什么都不会调用,并且因为“ pause”需要键输入才能继续。 The "-n 2" will pause the script for 1 second before continuing. “ -n 2”将脚本暂停1秒钟,然后继续。

After the script is run, it will create two directories (Build, and dist). 运行脚本后,它将创建两个目录(Build和dist)。 Your standalone executable will be located in the "dist" folder. 您的独立可执行文件将位于“ dist”文件夹中。

I've used this for all my applications, and have never had a problem, except for with use with PyMC. 我在所有应用程序中都使用了此功能,除了与PyMC一起使用外,从未遇到过问题。 This had a garbage library import call in one of the modules which stopped all module finders from following the next import, even py2exe wouldn't work. 这在其中一个模块中进行了一个垃圾库导入调用,该调用使所有模块查找器都无法进行下一次导入,即使py2exe也无法正常工作。 If you encounter something like this, you will most likely have to go into the module source code and fix the garbage. 如果遇到这样的情况,您很可能必须进入模块源代码并修复垃圾。 * Chances of this are very slim for most libraries * *对于大多数图书馆来说,这种机会非常渺茫*

-- Edit (2 minutes later) -- -编辑(2分钟后)-

After re-reading your question after posting, it seems you want an actual installer. 发布后重新阅读您的问题后,似乎您想要一个实际的安装程序。

I would suggest using pyinstaller to make your python executable. 我建议使用pyinstaller来使您的python可执行文件。 The EXE created will include any imports in the main script, so you won't have to include the physical installers for python, PIL, custom modules in the main Installer program ( So long as they are referenced in your script ). 创建的EXE将在主脚本中包含所有导入,因此您不必在主Installer程序中包含python,PIL,自定义模块的物理安装程序(只要在脚本中引用了它们)。 Now is the fun part: 现在是有趣的部分:

You will want to install InnoSetup. 您将要安装InnoSetup。 It is a free open source program used for developing professional looking installers for windows. 它是一个免费的开源程序,用于开发Windows专业外观的安装程序。 The main installer will require a script to run. 主安装程序将需要运行脚本。 This may need some reading up on the syntax, but I will try my best to give you the jist. 这可能需要对语法进行一些阅读,但是我会尽力为您提供指导。

The syntax of the script is similar to Pascal, and there are usually 6 sections in this script that are used in the installer: 该脚本的语法类似于Pascal,该脚本中通常有6个部分供安装程序使用:

[Setup] , [Files] , [Tasks] , [Icons] , [Run] , and [Code] [设置],[文件],[任务],[图标],[运行]和[代码]

The [Setup] section includes all the basic information about your installer: [安装程序]部分包含有关安装程序的所有基本信息:

[Setup]
AppId={{123LKJESA-1441-FAKE-IDNO-THISWONTWRK}
OutputDir=.
OutputBaseFilename=MyInstaller
AppName=ExecutableName
AppVersion=1.0
AppPublisher=YourCompany
AppPublisherURL=http://www.URL.com
AppSupportURL=http://www.URL.com
AppUpdatesURL=http://www.URL.com
DefaultGroupName=A_GroupName
LicenseFile=C:\Path\To\Your\License.txt
DefaultDirName={pf}\MainDirName
WizardImageBackColor=$00001a
SetupIconFile=C:\Path\To\Icon.ico
Compression=lzma
SolidCompression=yes
  • Note: That the AppID is unique to your application, if you create a new version, this ID must be the same, or the installer will install a separate exe, and there's a good chance that the uninstaller will break. 注意:AppID对您的应用程序是唯一的,如果您创建新版本,则此ID必须相同,否则安装程序将安装单独的exe,并且卸载程序很有可能会崩溃。

Also, DefaultDirName has the key {pf} which stands for Program Files. 另外,DefaultDirName具有键{pf},代表程序文件。 There are other location keys such available, and can be found on the website Documentation. 还有其他可用的位置键,可以在网站文档中找到。


[Files] - This section will include any files required for the installer, and it follows a pretty standard format. [文件]-此部分将包含安装程序所需的所有文件,并且遵循相当标准的格式。 You will need a Source: and a Destination (DestDir:). 您将需要一个Source:和一个Destination(DestDir :)。 Other useful options are CopyMode, which tell the installer what to do on a reinstallation, and Flags: which give even more options on what to do with this file. 其他有用的选项是CopyMode(它告诉安装程序重新安装时该怎么做)和Flags(标志):它提供了更多有关处理此文件的选项。

Example: 例:

[Files]
Source: C:\Path\to\my\created\python\executable.exe; DestDir: {app}; CopyMode: alwaysoverwrite;
Source: C:\Path\to\directory\of\files; DestDir: {localapp}; CopyMode: alwaysoverwrite; Flags: recursesubdirs;
  • The {app} key here in the first source destdir is a reference to the destination of the application. 第一个源目标目录中的{app}键是对应用程序目标的引用。
  • The {localapp} key will store all the files in my directory given, in the users local app data file, which can be referenced by the python application. {localapp}键会将所有文件存储在给定目录中的用户本地应用程序数据文件中,该文件可由python应用程序引用。

[Tasks] will give the user options to select certain tasks for this application. [任务]将为用户提供选择此应用程序的某些任务的选项。 In the example below I want the user to select whether or not they want to have a desktop icon. 在下面的示例中,我希望用户选择是否要具有桌面图标。

[Tasks]
Name: "desktopicon" Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}";

The [Icons] section is pretty self explanatory, and can be looked up in the online docs (also this post is getting too long) [Icons]部分的内容很容易解释,可以在在线文档中查找(该帖子也太长了)

Last the [Code] section 最后[代码]部分

This section isn't completely necessary, as the installer will run in default mode with out it. 这部分不是完全必要的,因为安装程序将在没有它的默认模式下运行。 But, if you want to create a more complex installer, here is where you will do it. 但是,如果您想创建一个更复杂的安装程序,则可以在这里进行。 This is the section which can be handled by an ordinary programmer, but if you have a background with Pascal it will really help. 这是普通程序员可以处理的部分,但是如果您有Pascal的背景,那将真的很有帮助。 Here, you can create custom windows, setup AfterInstall and BeforeInstall checks (such as check registries if certain programs are already installed, else start another installer before continuing) 在这里,您可以创建自定义窗口,设置AfterInstall和BeforeInstall检查(例如检查注册表是否已安装某些程序,否则请先启动另一个安装程序,然后再继续)

-- Edit 2 -- -编辑2-

In my haste I forgot about the [Run] section. 我匆忙地忘记了[运行]部分。 The [Run] section give the user the option to run the application immediately after install. [运行]部分为用户提供了在安装后立即运行应用程序的选项。 Here is an example that will show a check box on the last page, that is initially unchecked, for the user to select if they want to run the program. 这是一个示例,该示例将在最后一页上显示一个复选框,该复选框最初未被选中,供用户选择是否要运行该程序。

[Run]
Filename: "{app}\ExecutableName.exe"; Flags: nowait postinstall unchecked

*ReCap: A good application and installer should not require the user to install python and the separate libraries used. * ReCap:一个好的应用程序和安装程序不应要求用户安装python和使用的单独库。 Using py2exe or pyinstaller will create a standalone executable so none of this will need to be installed on the end-users machine. 使用py2exe或pyinstaller将创建一个独立的可执行文件,因此无需在最终用户计算机上安装所有可执行文件。 InnoSetup will allow you to create the installer, and place that executable (and any other non-python essential sources) in a desired location, so that it can run exactly the same as it did while still in .pyc files from your machine. InnoSetup允许您创建安装程序,并将该可执行文件(以及任何其他非Python必需的源文件)放置在所需的位置,以便它可以与仍从计算机中的.pyc文件中运行的方式完全相同。

** Before deploying your application it is a good idea to test your application and installer on different machine (or create virtual machines). **在部署应用程序之前,最好在其他计算机上测试应用程序和安装程序(或创建虚拟机)。 Eg. 例如。 The {localappdata} key on windows 7 refers to "C:\\Users\\UserName\\AppData\\Local" and on XP it refers to "C:\\Documents and Settings\\UserName\\Local" (trying to remember off the top of my head so might be wrong). Windows 7上的{localappdata}键指向“ C:\\ Users \\ UserName \\ AppData \\ Local”,而在XP上,它指向“ C:\\ Documents and Settings \\ UserName \\ Local”(试图记住我的头顶)因此可能是错误的)。 So there's a good chance that if your program is accessing the user's local application data to store files, that the path link will be broken on XP, using os.environ['LOCALAPPDATA']. 因此,如果您的程序正在访问用户的本地应用程序数据以存储文件,那么很有可能使用os.environ ['LOCALAPPDATA']在XP上断开路径链接。 On XP the InnoSetup key {userappdata} will point to "C:\\Documents and Settings\\UserName\\Application Data", which is in the right direction of where the python os.environ mapping will be pointing. 在XP上,InnoSetup键{userappdata}将指向“ C:\\ Documents and Settings \\ UserName \\ Application Data”,这是python os.environ映射所指向的正确方向。 Using "{userappdata}\\Local" will yield the right path. 使用“ {userappdata} \\ Local”将产生正确的路径。 But on Windows 7, {userappdata} will point to "C:\\Users\\UserName\\AppData\\Roaming". 但是在Windows 7上,{userappdata}将指向“ C:\\ Users \\ UserName \\ AppData \\ Roaming”。 So if the application will be backward compatible with windows versions, you will need to setup a bit of code in the [Code] section, to determine the correct version, and set a boolean flag on which key to use. 因此,如果应用程序与Windows版本向后兼容,则需要在[代码]部分中设置一些代码,以确定正确的版本,并在要使用的键上设置一个布尔标志。

The URL for the online Documents is: 联机文档的URL为:

http://www.jrsoftware.org/ishelp/ http://www.jrsoftware.org/ishelp/

I hope that you (or others) find this useful, because it is a powerful tool, as all the files you include in the [Files] section will be packaged up into the standalone installer, and will appear in the destination you give it. 我希望您(或其他人)发现它有用,因为它是一个功能强大的工具,因为您在[文件]部分中包含的所有文件都将打包到独立安装程序中,并出现在指定的目标位置。 Even though this may be a little long winded, it should give you a real good start on making executables and packaging it with an installer. 即使这可能需要花费很长的时间,它也应该为您制作可执行文件并将其与安装程序打包提供一个真正好的开始。

Good Luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM