Search code examples
pythonflaskpy2exesetup.pysoftware-distribution

Python Flask - Building the Server as Executable (Py2exe)


I have a flask project, everything appears to be working fine. When using py2exe to build the package, (target server is a windows server ugh), the executable can execute, but leaves me with a ImportError: No module named 'jinja2.ext'

I have the module, and the website works fine with no ImportError when not executing from the .exe

I am pretty new at packaging and delivering, and not sure whats wrong with the setup that is causing the break from .py -> .exe conversion.

Setup.py

from setuptools import setup, find_packages

import py2exe



NAME = "WorkgroupDashboard"
VERSION = "1.0"



setup(
    name=NAME,
    version=VERSION,
    description="Provides real time ISIS connection data",
    long_description="",
    # Get strings from http://www.python.org/pypi?%3Aaction=list_classifiers
    classifiers=[],
    author="Test User",
    author_email='',
    url='',
    license='Free',
    packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
    include_package_data=True,
    zip_safe=False,
    install_requires=[
        'flask >= 0.10.1',
        'SQLAlchemy>=0.6'
    ],
    console=['DashboardBack.py']
)

The idea is in order to turn the server on, just execute the .exe. Server will not have python on it. I am using Python 3.4 64 bit.

Edit: build cmd = python setup.py py2exe


Solution

  • Figured it out it seems, Setup commands go inside the py2exe optuons=[]

    Working Setup.py

    __author__ = ''
    
    
    import sys
    from glob import glob # glob will help us search for files based on their extension or filename.
    from distutils.core import setup # distutils sends the data py2exe uses to know which file compile
    import py2exe
    
    data_files = []
    setup(
        name='WorkgroupDashboard',
        console=['DashboardBack.py'], # 'windows' means it's a GUI, 'console' It's a console program, 'service' a Windows' service, 'com_server' is for a COM server
        # You can add more and py2exe will compile them separately.
        options={ # This is the list of options each module has, for example py2exe, but for example, PyQt or django could also contain specific options
            'py2exe': {
                'packages':['jinja2'],
                'dist_dir': 'dist/test', # The output folder
                'compressed': True, # If you want the program to be compressed to be as small as possible
                'includes':['os', 'logging', 'yaml', 'flask', 'sqlalchemy'], # All the modules you need to be included,  because py2exe guesses which modules are being used by the file we want to compile, but not the imports
            }
        },
    
        data_files=data_files # Finally, pass the
    )