Pytest-xdist cannot run after being packaged with pyinstaller

50 views Asked by At

This Python script can be run locally, but after being packaged with pyinstaller, it has been stuck in an endless loop. Is there a problem with packaging?

Python 3.9.6
pyinstaller==5.13.0
pytest==7.4.0
pytest-xdist==3.3.1

My code:

test_case.py

import pytest
 
def test_example1():
    assert 1==1

def test_example2():
    assert 1 + 1 == 2

if __name__ == "__main__":
    pytest.main(["-n", "1"])

test_case.spec

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

a = Analysis(
    ['test_case.py'],
    pathex=[],
    binaries=[],
    datas=[
        ('D:/Anaconda/envs/python3.9/Lib/site-packages/apipkg', './apipkg'), 
        ('D:/Anaconda/envs/python3.9/Lib/site-packages/apipkg-3.0.2.dist-info', './apipkg-3.0.2.dist-info'),
        ('D:/Anaconda/envs/python3.9/Lib/site-packages/pytest_xdist-3.3.1.dist-info', './pytest_xdist-3.3.1.dist-info'), 
        ('D:/Anaconda/envs/python3.9/Lib/site-packages/xdist', './xdist'),
        ('D:/Anaconda/envs/python3.9/Lib/site-packages/execnet', './execnet'), 
        ('D:/Anaconda/envs/python3.9/Lib/site-packages/execnet-2.0.2.dist-info', './execnet-2.0.2.dist-info'), 
        ],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='test_case',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

It was always running before.

I have tried using different versions of packages, but none of them worked.

1

There are 1 answers

0
fashionprivate On

I asked to the pyinstaller support and here you can find an answer to your question: https://github.com/pyinstaller/pyinstaller/issues/8283

I then wanted to make pyinstaller work with the BaseCase class and I proceeded like this (I wanted to include aaa.py in my bundle):

bundle_dir = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__)))
path_to_help = os.path.abspath(os.path.join(bundle_dir,'aaa.py'))

BaseCase.main(__name__, path_to_help, -n2, "-o", "log_cli=true", "--log-cli-level=INFO", "--uc", "--uc-cdp", "--pls=none", "--sjw", "--block-images", "--disable-warnings", "-p no:warnings", "--no-header", "--no-summary", "--quiet", "--maxfail=1")

In the BaseCase.main I had to modify this method:

subprocess.call(
   ["python", "-m", "pytest", file, "-s", *all_args]
)

because the sys.executable (instead of "python") referred to the .exe (done by Pyinstaller)