Code Monkey home page Code Monkey logo

python-package-example's Introduction

python-package-example

An example method to package a python desktop app on windows

Packaging Python desktop applications in Windows can be painful.


Third Party Tools

There are number of third party tools which can make this task relatively easy. If you can get by using one of these, go for it. Unfortunately, each of these tools usually need a little help to make them work.

Probably the easiest to use. For the most part you can simply call pyinstaller myapp and it will work. I was happy with this method untill recently the .exe files produced by it started getting quarantined by Crowd Strike on my users computers. Since disabling Crowd Strike is not an option I had to look for other methods.

This requires a little extra work. I had trouble getting file assocations to work properly. Though, to be fair I only spent a few hours trying to debug it.

This tool is differenet from PyInstaller and CXFreeze in that it produces an installer with NSIS.


Embedded Python

The embeddable version of Python is stripped down and isolated from the system registry. Checkout the python docs: 3.8. Embedded Distribution, andownload it here.


Installing Pip In Embedded Python

I wanted to use pip to ease the installation and maintenance of packages. Unfortunately embedded python does not come with pip support. We can install pip we just need to do a little work:

  1. Extract python36.zip to a folder titled python36.

  2. Modify the python36._pth file which overrides sys.path meaning that the registry and enviroment variables will be ignored. You can read more .pth files in the Python Docs at 3.5. Finding modules. Edit your python36._pth to look like:

    lib/site-packages
    python36
    .
    import site
  3. Install pip with get-pip.py which you can download from from here. Ensure you are in the correct folder when you run this command, or use the absolute path to the Python executable:

    C:\Users\tarre\code\python-package-example\python-3.6.4-embed-win32\python.exe get-pip.py

Installing Packages in Embedded Python

Once pip is installed we can use pip as normal. Just be careful to call it from the embedded executable. It is probably safest to use its absolute path. For example:

C:\Users\tarre\code\python-package-example\python-3.6.4-embed-win32\python.exe -m pip install <packagename>

I have provided two barebones demo apps in this distro to demonstrate console and GUI apps, they are very creatively titled demo_console, and demo_pyqt. Note that the demo apps utilizes entry_points which you can read about here, here, and here.

C:\Users\tarre\code\python-package-example\python-3.6.4-embed-win32\python.exe -m pip install C:\Users\tarre\code\python-package-example\demo_pyqt

C:\Users\tarre\code\python-package-example\python-3.6.4-embed-win32\python.exe -m pip install C:\Users\tarre\code\python-package-example\demo_console

Launchers

The Python docs alluded to two different methods to launch python applications but they do not provide any examples.

Batch File

We can create a batch file to call the app.

Shortcuts

We can create windows shortcut.

Specialized Executable

I think this is the most interesting method. We can write a bit of C/C++ to create a custom executable that simply calls Py_Main and passes a few arguments.

The benefits of this include:

  • Custom Icon, and application metadata
  • Easy file assocations
  • Processes are unique and do not display as Python

Using CPython’s Embeddable Zip File

custom-python-launcher

Distribute a Windows Python app with Inno Setup

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.