faq.rst 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. FAQs
  2. ====
  3. Building on other platforms
  4. ---------------------------
  5. You can use Pynsist to build Windows installers from a Linux or Mac system.
  6. You'll need to install NSIS so that the ``makensis`` command is available.
  7. Here's how to do that on some common platforms:
  8. * Debian/Ubuntu: ``sudo apt-get install nsis``
  9. * Fedora: ``sudo dnf install mingw32-nsis``
  10. * Mac with `Homebrew <https://brew.sh/>`__: ``brew install makensis``
  11. Installing Pynsist itself is the same on all platforms::
  12. pip install pynsist
  13. If your package relies on compiled extension modules, like
  14. PyQt4, lxml or numpy, you'll need to ensure that the installer is built with
  15. Windows versions of these packages. There are a few options for this:
  16. - List them under ``pypi_wheels`` in the :ref:`Include section <cfg_include>`
  17. of your config file. Pynsist will download Windows-compatible wheels from
  18. PyPI. This is the easiest option if the dependency publishes wheels.
  19. - Get the importable packages/modules, either from a Windows installation, or
  20. by extracting them from an installer. Copy them into a folder called
  21. ``pynsist_pkgs``, next to your ``installer.cfg`` file. Pynsist will
  22. copy everything in this folder to the build directory.
  23. - Include exe/msi installers for those modules, and modify the ``.nsi`` template
  24. to extract and run these during installation. This can make your installer
  25. bigger and slower, and it may create unwanted start menu shortcuts
  26. (e.g. PyQt4 does), so it's a last resort. However, if the
  27. installer sets up other things on the system, you may need to do this.
  28. When running on non-Windows systems, Pynsist will bundle a 32-bit version of
  29. Python by default, though you can override this :ref:`in the config file <cfg_python>`.
  30. Whichever method you use, compiled libraries must have the same bit-ness as
  31. the version of Python that's installed.
  32. Using data files
  33. ----------------
  34. Applications often need data files along with their code. The easiest way to use
  35. data files with Pynsist is to store them in a Python package (a directory with
  36. a ``__init__.py`` file) you're creating for your application. They will be
  37. copied automatically, and modules in that package can locate them using
  38. ``__file__`` like this::
  39. data_file_path = os.path.join(os.path.dirname(__file__), 'file.dat')
  40. If you don't want to put data files inside a Python package, you will need to
  41. list them in the ``files`` key of the ``[Include]`` section of the config file.
  42. Your code can find them relative to the location of the launch script running your
  43. application (``sys.modules['__main__'].__file__``).
  44. .. note::
  45. The techniques above work for fixed data files which you ship with your
  46. application. For files which your app will *write*, you should use another
  47. location, because an app installed systemwide cannot write files in its
  48. install directory. Use the ``APPDATA`` or ``LOCALAPPDATA`` environment
  49. variables as locations to write hidden data files (`what's the difference?
  50. <https://superuser.com/a/21462/209976>`__)::
  51. writable_file = os.path.join(os.environ['LOCALAPPDATA'], 'MyApp', 'file.dat')
  52. .. _faq-tkinter:
  53. Packaging with tkinter
  54. ----------------------
  55. Because Pynsist makes use of the "bundled" versions of Python the ``tkinter``
  56. module isn't included by default. If your application relies on ``tkinter`` for
  57. a GUI then you need to find the following assets:
  58. * The ``tcl`` directory in the root directory of a Windows installation of
  59. Python. This needs to come from the same Python version and bitness (i.e.
  60. 32-bit or 64-bit) as the Python you are bundling into the installer.
  61. * The ``_tkinter.pyd``, ``tcl86t.dll`` and ``tk86t.dll`` libraries in the
  62. ``DLLs`` directory of the version of Python your are using in your app. As
  63. above, these must be the same bitness and version as your target version of
  64. Python.
  65. * The ``_tkinter.lib`` file in the ``libs`` directory of the version of Python
  66. you are using in your app. Same caveats as above.
  67. The ``tcl`` directory should be copied into the root of your project (i.e. in
  68. the directory that contains ``installer.cfg``) and renamed to ``lib``
  69. (this is important!).
  70. Create a new directory in the root of your project called ``pynsist_pkgs`` and
  71. copy over the other four files mentioned above into it (so it contains
  72. ``_tkinter.lib``, ``_tkinter.pyd``, ``tcl86t.dll`` and ``tk86t.dll``).
  73. Finally, in your ``.cfg`` file ensure the ``packages`` section contains
  74. ``tkinter`` and ``_tkinter``, and the ``files`` section contains ``lib``, like
  75. this::
  76. packages=
  77. tkinter
  78. _tkinter
  79. files=lib
  80. Build your installer and test it. You'll know everything is in the right place
  81. if the directory into which your application is installed contains a ``lib``
  82. directory containing the contents of the original ``tcl`` directory and the
  83. ``pkgs`` directory contains the remaining four files. If things still don't
  84. work check the bitness and Python version associated with these assets and
  85. make sure they're the same as the version of Python installed with your
  86. application.
  87. .. note::
  88. A future version of Pynsist might automate some of this procedure to make
  89. distributing tkinter applications easier.
  90. Code signing
  91. ------------
  92. People trying to use your installer will see an 'Unknown publisher' warning.
  93. To avoid this, you can sign it with a digital certificate. See
  94. `Mozilla's instructions on signing executables using Mono
  95. <https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Build_Instructions/Signing_an_executable_with_Authenticode>`__,
  96. or `this guide from Adafruit on signing an installer
  97. <https://learn.adafruit.com/how-to-sign-windows-drivers-installer/making-an-installer#sign-the-installer>`__.
  98. Signing requires a certificate from a provider trusted by Microsoft.
  99. As of summer 2017, these are the cheapest options I can find:
  100. * Certum's `open source code signing certificate <https://www.certum.eu/certum/cert,offer_en_open_source_cs.xml>`__:
  101. €86 for a certificate with a smart card and reader, €28 for a new certificate
  102. if you have the hardware. Each certificate is valid for one year.
  103. This is only for open source software.
  104. * Many companies resell Comodo code signing certificates at prices lower than
  105. Comodo themselves, especially if you pay for 3–4 years up front.
  106. `CodeSignCert <https://codesigncert.com/comodocodesigning>`__ ($59–75 per year),
  107. `K Software <http://codesigning.ksoftware.net/>`__ ($67–$84 per year) and
  108. `Cheap SSL Security <https://cheapsslsecurity.co.uk/comodo/codesigningcertificate.html>`__ (UK, £54–£64 per year)
  109. are a few examples; a search will turn up many more like them.
  110. I haven't used any of these companies, so I'm not making a recommendation.
  111. Please do your own research before buying from them.
  112. If you find another good way to get a code signing certificate, please make a
  113. pull request to add it!
  114. Alternatives
  115. ------------
  116. Other ways to distribute applications to users without Python installed include
  117. freeze tools, like `cx_Freeze <http://cx-freeze.sourceforge.net/>`_ and
  118. `PyInstaller <http://www.pyinstaller.org/>`_, and Python compilers like
  119. `Nuitka <http://nuitka.net/>`_.
  120. pynsist has some advantages:
  121. * Python code often does things—like using ``__file__`` to find its
  122. location on disk, or :data:`sys.executable` to launch Python processes—which
  123. don't work when it's run from a frozen exe. pynsist just installs Python files,
  124. so it avoids all these problems.
  125. * It's quite easy to make Windows installers on other platforms, which is
  126. difficult with other tools.
  127. * The tool itself is simpler to understand, and less likely to need updating for
  128. new Python versions.
  129. And some disadvantages:
  130. * Installers tend to be bigger because you're bundling the whole Python standard
  131. library.
  132. * You don't get an exe for your application, just a start menu shortcut to launch
  133. it.
  134. * pynsist only makes Windows installers.
  135. Popular freeze tools also try to automatically detect what packages you're using.
  136. Pynsist could do the same thing, but in my experience, this detection is complex and often
  137. misses things, so for now it expects an explicit list of the packages
  138. your application needs.
  139. Another alternative is `conda constructor <https://github.com/conda/constructor>`__,
  140. which builds an installer out of conda packages. Conda packages are more
  141. flexible than PyPI packages, and many libraries are already packaged, but you
  142. have to make a conda package of your own code as well before using conda
  143. constructor to make an installer.
  144. Conda constructor can also make Linux and Mac installers, but unlike Pynsist, it
  145. can't make a Windows installer from Linux or Mac.