question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] KeyError: '__file__' with setuptools 60.0.0

See original GitHub issue

setuptools version

setuptools==60.0.0

Python version

Python 3.8.12

OS

Windows 10

Additional environment information

I use Anaconda with Python 3.8.12. I use PyArmor 7.0.3. I user PyInstaller 4.7.

Description

I use PyArmor to pack my project (Obfuscation + Executable with PyInstaller). It is used inside a Conda Environment. I have a script that calls PyArmor.

    call_pyarmor(
        [
            "pack",
            "-s",
            "./main.spec",
            "-x",
            " --exclude .venv*"
            " --enable-suffix"
            " --with-license outer"
            " --plugin pyarmor_plugins/plugin_example.py",
            "--clean",
            "./main.py",
        ]
    )

Expected behavior

To the project’s Executable be properly generated.

How to Reproduce

  1. Set the environment:
conda create --yes --quiet --name .venv python=3.8
call activate .venv
python -m pip install --upgrade pip setuptools wheel
python -m pip install pyarmor==7.0.3 pyinstaller[encryption]==4.7
  1. Create a sample default project (with main.spec and a plugin)
  2. Run PyArmor from a script:
from PyInstaller.__main__ import run as call_pyinstaller
call_pyarmor(
        [
            "pack",
            "-s",
            "./main.spec",
            "-x",
            " --exclude .venv*"
            " --enable-suffix"
            " --with-license outer"
            " --plugin pyarmor_plugins/plugin_example.py",
            "--clean",
            "./main.py",
        ]
) 

Output

2021-12-20T12:51:26.2832859Z INFO     PyArmor Version 7.0.3
2021-12-20T12:51:26.2833655Z INFO     Python 3.8.12
2021-12-20T12:51:26.2837167Z INFO     Target platforms: Native
2021-12-20T12:51:26.2837966Z INFO     Source path is "D:\a\1\s"
2021-12-20T12:51:26.2838558Z INFO     Entry scripts are ['.\\main.py']
2021-12-20T12:51:26.2839403Z INFO     Use cached capsule C:\Users\VssAdministrator\.pyarmor\.pyarmor_capsule.zip
2021-12-20T12:51:26.2840362Z INFO     Search scripts mode: Recursive
2021-12-20T12:51:26.2841345Z INFO     Exclude path "dist"
2021-12-20T12:51:26.2844968Z INFO     Exclude path ".venv*"
2021-12-20T12:51:26.2851366Z INFO     Auto exclude output path "dist\obf"
2021-12-20T12:51:26.3066863Z no previously-included directories found matching 'dist'
2021-12-20T12:51:26.3073564Z no previously-included directories found matching '.venv*'
2021-12-20T12:51:26.3115156Z no previously-included directories found matching 'dist\obf'
2021-12-20T12:51:26.3119135Z INFO     Save obfuscated scripts to "dist\obf"
2021-12-20T12:51:26.3120364Z INFO     Read product key from capsule
2021-12-20T12:51:26.3151411Z INFO     Obfuscate module mode is 2
2021-12-20T12:51:26.3152052Z INFO     Obfuscate code mode is 1
2021-12-20T12:51:26.3152539Z INFO     Wrap mode is 1
2021-12-20T12:51:26.3153215Z INFO     Restrict mode is 1
2021-12-20T12:51:26.3153770Z INFO     Advanced value is 0
2021-12-20T12:51:26.3155708Z INFO     Super mode is False
2021-12-20T12:51:26.3156579Z INFO     Super plus mode is not enabled
2021-12-20T12:51:26.3163577Z INFO     Generating runtime files to dist\obf
2021-12-20T12:51:26.3169121Z INFO     Extract pytransform.key
2021-12-20T12:51:26.3171851Z INFO     Use outer license file
2021-12-20T12:51:26.3174756Z INFO     Copying C:\Miniconda\envs\.venv\lib\site-packages\pyarmor\platforms\windows\x86_64\_pytransform.dll
2021-12-20T12:51:26.3175495Z INFO     Rename it to _pytransform_vax_0001.dll
2021-12-20T12:51:26.3210551Z INFO     Patch library dist\obf\_pytransform_vax_0001.dll
2021-12-20T12:51:26.6123821Z INFO     Patch library file OK
2021-12-20T12:51:27.2509613Z INFO     Copying C:\Miniconda\envs\.venv\lib\site-packages\pyarmor\pytransform.py
2021-12-20T12:51:27.2511236Z INFO     Rename it to pytransform_vax_0001.py
2021-12-20T12:51:27.2521723Z INFO     Generate runtime files OK
2021-12-20T12:51:27.2527229Z INFO     Start obfuscating the scripts...
2021-12-20T12:51:27.7791265Z INFO     Found plugin check_processor_id at: pyarmor_plugins/example_plugin.py
2021-12-20T12:51:27.7800488Z INFO     Patch this script with plugins
2021-12-20T12:51:27.7803068Z INFO     Apply plugin example_plugin
2021-12-20T12:51:27.7807320Z INFO     Patch this entry script with protection code
2021-12-20T12:51:27.8445234Z INFO     Insert bootstrap code to entry script dist\obf\main.py
2021-12-20T12:51:27.8456836Z INFO     Obfuscate 9 scripts OK.
2021-12-20T12:51:30.6824262Z 2464 INFO: PyInstaller: 4.7
2021-12-20T12:51:30.6825312Z 2464 INFO: Python: 3.8.12 (conda)
2021-12-20T12:51:30.6996302Z 2483 INFO: Platform: Windows-10-10.0.17763-SP0
2021-12-20T12:51:30.7045110Z 2488 INFO: UPX is not available.
2021-12-20T12:51:30.7052400Z 2490 INFO: Removing temporary files and cleaning cache in C:\Users\VssAdministrator\AppData\Local\pyinstaller
2021-12-20T12:51:30.7111993Z Traceback (most recent call last):
2021-12-20T12:51:30.7112816Z   File "C:\Miniconda\envs\.venv\lib\runpy.py", line 194, in _run_module_as_main
2021-12-20T12:51:30.7113603Z     return _run_code(code, main_globals, None,
2021-12-20T12:51:30.7114264Z   File "C:\Miniconda\envs\.venv\lib\runpy.py", line 87, in _run_code
2021-12-20T12:51:30.7114913Z     exec(code, run_globals)
2021-12-20T12:51:30.7115338Z   File "C:\Miniconda\envs\.venv\lib\site-packages\PyInstaller\__main__.py", line 134, in <module>
2021-12-20T12:51:30.7115776Z     run()
2021-12-20T12:51:30.7118603Z   File "C:\Miniconda\envs\.venv\lib\site-packages\PyInstaller\__main__.py", line 124, in run
2021-12-20T12:51:30.7119564Z     run_build(pyi_config, spec_file, **vars(args))
2021-12-20T12:51:30.7120046Z   File "C:\Miniconda\envs\.venv\lib\site-packages\PyInstaller\__main__.py", line 58, in run_build
2021-12-20T12:51:30.7120607Z     PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
2021-12-20T12:51:30.7121099Z   File "C:\Miniconda\envs\.venv\lib\site-packages\PyInstaller\building\build_main.py", line 782, in main
2021-12-20T12:51:30.7122155Z     build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
2021-12-20T12:51:30.7123140Z   File "C:\Miniconda\envs\.venv\lib\site-packages\PyInstaller\building\build_main.py", line 714, in build
2021-12-20T12:51:30.7124574Z     exec(code, spec_namespace)
2021-12-20T12:51:30.7124905Z   File "main-patched.spec", line 5, in <module>
2021-12-20T12:51:30.7125488Z     from pip._internal.operations import freeze
2021-12-20T12:51:30.7126946Z   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2021-12-20T12:51:30.7128349Z   File "<frozen importlib._bootstrap>", line 971, in _find_and_load_unlocked
2021-12-20T12:51:30.7128859Z   File "<frozen importlib._bootstrap>", line 914, in _find_spec
2021-12-20T12:51:30.7129415Z   File "C:\Miniconda\envs\.venv\lib\site-packages\_distutils_hack\__init__.py", line 83, in find_spec
2021-12-20T12:51:30.7129816Z     return method()
2021-12-20T12:51:30.7130701Z   File "C:\Miniconda\envs\.venv\lib\site-packages\_distutils_hack\__init__.py", line 104, in spec_for_pip
2021-12-20T12:51:30.7131192Z     if self.pip_imported_during_build():
2021-12-20T12:51:30.7131784Z   File "C:\Miniconda\envs\.venv\lib\site-packages\_distutils_hack\__init__.py", line 115, in pip_imported_during_build
2021-12-20T12:51:30.7132215Z     return any(
2021-12-20T12:51:30.7132809Z   File "C:\Miniconda\envs\.venv\lib\site-packages\_distutils_hack\__init__.py", line 116, in <genexpr>
2021-12-20T12:51:30.7133491Z     frame.f_globals['__file__'].endswith('setup.py')
2021-12-20T12:51:30.7133835Z KeyError: '__file__'
2021-12-20T12:51:30.8726971Z Traceback (most recent call last):
2021-12-20T12:51:30.8759812Z   File "deployment.py", line 70, in <module>
2021-12-20T12:51:30.8761995Z     main(sys.argv)
2021-12-20T12:51:30.8765357Z   File "deployment.py", line 27, in main
2021-12-20T12:51:30.8766912Z     run_pyarmor()
2021-12-20T12:51:30.8768238Z   File "deployment.py", line 40, in run_pyarmor
2021-12-20T12:51:30.8768744Z     call_pyarmor(
2021-12-20T12:51:30.8769402Z   File "C:\Miniconda\envs\.venv\lib\site-packages\pyarmor\pyarmor.py", line 1572, in main
2021-12-20T12:51:30.8769991Z     args.func(args)
2021-12-20T12:51:30.8770836Z   File "C:\Miniconda\envs\.venv\lib\site-packages\pyarmor\packer.py", line 540, in packer
2021-12-20T12:51:30.8771583Z     _pyinstaller(src, entry, output, extra_options, xoptions, args)
2021-12-20T12:51:30.8772362Z   File "C:\Miniconda\envs\.venv\lib\site-packages\pyarmor\packer.py", line 440, in _pyinstaller
2021-12-20T12:51:30.8773051Z     run_command([sys.executable] + packcmd + ['-y', '--clean', patched_spec])
2021-12-20T12:51:30.8773787Z   File "C:\Miniconda\envs\.venv\lib\site-packages\pyarmor\packer.py", line 86, in run_command
2021-12-20T12:51:30.8774419Z     raise RuntimeError('Run command failed')
2021-12-20T12:51:30.8774985Z RuntimeError: Run command failed

Code of Conduct

  • I agree to follow the PSF Code of Conduct

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ironstar94commented, Dec 20, 2021

The bug is occuring because setuptools bundled distutils that’s breaking stuff. It was apparently only tested on Fedora https://github.com/pypa/setuptools/pull/2896

Temporary workaround is setting SETUPTOOLS_USE_DISTUTILS=stdlib in your environment.

1reaction
lestevecommented, Dec 20, 2021

A simple snippet reproducing the problem:

if [ -d /tmp/test-venv ]; then
    rm -rf /tmp/test-venv
fi
python -m venv /tmp/test-venv
source /tmp/test-venv/bin/activate
pip install 'setuptools>=60'

pip list
python -c 'import importlib; importlib.import_module("pip")'

A few comments:

  • this fails with setuptools 60.0.0 with a similar error as shown above but succeeds with setuptools 59.8

    Full stack-trace
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/home/local/lesteve/miniconda3/lib/python3.9/importlib/__init__.py", line 127, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
      File "<frozen importlib._bootstrap>", line 982, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 925, in _find_spec
      File "/tmp/test-venv/lib/python3.9/site-packages/_distutils_hack/__init__.py", line 83, in find_spec
        return method()
      File "/tmp/test-venv/lib/python3.9/site-packages/_distutils_hack/__init__.py", line 104, in spec_for_pip
        if self.pip_imported_during_build():
      File "/tmp/test-venv/lib/python3.9/site-packages/_distutils_hack/__init__.py", line 115, in pip_imported_during_build
        return any(
      File "/tmp/test-venv/lib/python3.9/site-packages/_distutils_hack/__init__.py", line 116, in <genexpr>
        frame.f_globals['__file__'].endswith('setup.py')
    KeyError: '__file__'
    
  • this is only a problem with python -c, executing a .py file (with the same content) works fine

Read more comments on GitHub >

github_iconTop Results From Across the Web

[BUG] KeyError: 'unix_user' with setuptools 60.0.0 · Issue #2938
Getting this error with setuptools 60.0.0. Traceback (most recent call last): File "", line 1, in. File "/tmpfs/src/github/jax/setup.py", ...
Read more >
Unable to install scikit-learn using python 3.11.0
As in error message suggests to try with setuptools < 60.0.0 I tried poetry run pip install setuptools==59.8.0.
Read more >
setuptools 3.5.2 - PyPI
Easily download, build, install, upgrade, and uninstall Python packages.
Read more >
History - setuptools 65.6.3.post20221216 documentation
#3515: Fixed “inline” file copying for editable installations and ... #1994: Fixed a bug in the “setuptools.finalize_distribution_options” hook that lead to ...
Read more >
FS#74725 : Last python-setuptools update breaks Firefox builds
Description: After updating python-setuptools to 1:60.0.0-1, ... File "/home/emilio/src/moz/gecko-2/mach", line 96, in <module>
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found