Skip to content

Instantly share code, notes, and snippets.

@nicoddemus
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicoddemus/5458ca3fc5241cedaff3 to your computer and use it in GitHub Desktop.
Save nicoddemus/5458ca3fc5241cedaff3 to your computer and use it in GitHub Desktop.
How to embed `pytest.main` into a frozen executable created by cx_freeze
import pytest, sys
sys.exit(pytest.main())
from cx_Freeze import setup, Executable
build_exe_options = {"includes": [
'_pytest._argcomplete',
'_pytest._doctest',
'_pytest._pdb',
'_pytest._unittest',
'_pytest.capture',
'_pytest.config',
'_pytest.core',
'_pytest.genscript',
'_pytest.helpconfig',
'_pytest.hookspec',
'_pytest.junitxml',
'_pytest.main',
'_pytest.mark',
'_pytest.monkeypatch',
'_pytest.nose',
'_pytest.pastebin',
'_pytest.pytester',
'_pytest.python',
'_pytest.recwarn',
'_pytest.resultlog',
'_pytest.runner',
'_pytest.skipping',
'_pytest.standalonetemplate',
'_pytest.terminal',
'_pytest.tmpdir',
'py._builtin',
'py._path.local',
'py._io.capture',
'py._io.saferepr',
'py._iniconfig',
'py._io.terminalwriter',
'py._xmlgen',
'py._error',
'py._std',
# implicit std modules used by pytest
'argparse',
'shlex',
'warnings',
'types',
]}
setup(
name = "runtests",
version = "0.1",
description = "runtests",
executables = [Executable("runtests.py")],
options = {"build_exe": build_exe_options},
)
@nicoddemus
Copy link
Author

To test, install into a fresh virtual env:

$ pip install cx_freeze pytest
$ python setup.py build

And execute the generated executable with --help. This should yield a traceback like this:

Traceback (most recent call last):
  File "X:\pytest_cx_freeze\.env27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
    exec(code, m.__dict__)
  File "runtests.py", line 1, in <module>
    import pytest, sys
  File "X:\pytest_cx_freeze\.env27\lib\site-packages\pytest.py", line 14, in <module>
    from _pytest.config import main, UsageError, _preloadplugins, cmdline
  File "X:\pytest_cx_freeze\.env27\lib\site-packages\_pytest\config.py", line 398, in <module>
    class MyOptionParser(py.std.argparse.ArgumentParser):
  File "X:\pytest_cx_freeze\.env27\lib\site-packages\py\_std.py", line 15, in __getattr__
    raise AttributeError("py.std: could not import %s" % name)
AttributeError: py.std: could not import argparse

@nicoddemus
Copy link
Author

Seems like listing all packages in py, _pytest and a few standard library modules imported by pytest internally does the trick.

Updated, still needs more testing:

from cx_Freeze import setup, Executable

build_exe_options = {"includes": [
    '_pytest._argcomplete',
    '_pytest._doctest',
    '_pytest._pdb',
    '_pytest._unittest',
    '_pytest.capture',
    '_pytest.config',
    '_pytest.core',
    '_pytest.genscript',
    '_pytest.helpconfig',
    '_pytest.hookspec',
    '_pytest.junitxml',
    '_pytest.main',
    '_pytest.mark',
    '_pytest.monkeypatch',
    '_pytest.nose',
    '_pytest.pastebin',
    '_pytest.pytester',
    '_pytest.python',
    '_pytest.recwarn',
    '_pytest.resultlog',
    '_pytest.runner',
    '_pytest.skipping',
    '_pytest.standalonetemplate',
    '_pytest.terminal',
    '_pytest.tmpdir',

    'py._builtin',
    'py._path.local',
    'py._io.capture',
    'py._io.saferepr',
    'py._iniconfig',
    'py._io.terminalwriter',
    'py._xmlgen',
    'py._error',
    'py._std',

    'argparse',
    'shlex',
    'warnings',
    'types',
]}

setup(  name = "runtests",
        version = "0.1",
        description = "runtests",
        executables = [Executable("runtests.py")],
        options = {"build_exe": build_exe_options},
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment