'An error occurred while packaging. Using pyinstaller

I'm trying to build a python file into a exe file. I don't get it since it builds on some days and gives errors on others. The python script works normally when I run it in CMD or Visual Code.

I have tried :

  • Reinstalling Django
  • Reinstalling Python
  • Reinstalling auto-py-to-exe
  • Installing GDAL
  • Running CMD as Admin

Error that comes up when trying to build the exe :

Running auto-py-to-exe v2.10.1
Building directory: C:\Users\white\AppData\Local\Temp\tmphwzenqcz
Provided command: pyinstaller --noconfirm --onefile --console  "C:/Users/white/Desktop/Security/password.py"
Recursion Limit is set to 5000
Executing: pyinstaller --noconfirm --onefile --console C:/Users/white/Desktop/Security/password.py --distpath C:\Users\white\AppData\Local\Temp\tmphwzenqcz\application --workpath C:\Users\white\AppData\Local\Temp\tmphwzenqcz\build --specpath C:\Users\white\AppData\Local\Temp\tmphwzenqcz

4527 INFO: PyInstaller: 4.5.1
4542 INFO: Python: 3.9.7
4563 INFO: Platform: Windows-10-10.0.22000-SP0
4573 INFO: wrote C:\Users\white\AppData\Local\Temp\tmphwzenqcz\password.spec
4576 INFO: UPX is not available.
4590 INFO: Extending PYTHONPATH with paths
['C:\\Users\\white\\Desktop\\Security',
 'C:\\Users\\white\\AppData\\Local\\Temp\\tmphwzenqcz']
4794 INFO: checking Analysis
4796 INFO: Building Analysis because Analysis-00.toc is non existent
4812 INFO: Initializing module dependency graph...
4830 INFO: Caching module graph hooks...
4853 INFO: Analyzing base_library.zip ...
6467 INFO: Processing pre-find module path hook distutils from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-distutils.py'.
6475 INFO: distutils: retargeting to non-venv dir 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib'
8715 INFO: Caching module dependency graph...
8863 INFO: running Analysis Analysis-00.toc
8876 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\Users\white\AppData\Local\Programs\Python\Python39\python.exe
8926 WARNING: lib not found: api-ms-win-core-path-l1-1-0.dll dependency of C:\Users\white\AppData\Local\Programs\Python\Python39\python39.dll
8934 INFO: Analyzing C:\Users\white\Desktop\Security\password.py
9313 INFO: Processing pre-safe import module hook urllib3.packages.six.moves from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-urllib3.packages.six.moves.py'.
15327 INFO: Processing pre-find module path hook site from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-site.py'.
15330 INFO: site: retargeting to fake-dir 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\fake-modules'
25292 INFO: Processing module hooks...
25311 INFO: Loading module hook 'hook-certifi.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
25329 INFO: Loading module hook 'hook-Crypto.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
25347 INFO: Loading module hook 'hook-cryptography.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
25523 INFO: Loading module hook 'hook-eel.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
25616 INFO: Loading module hook 'hook-lz4.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
25631 INFO: Loading module hook 'hook-pycparser.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
25633 INFO: Loading module hook 'hook-difflib.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
25637 INFO: Loading module hook 'hook-distutils.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
25646 INFO: Loading module hook 'hook-distutils.util.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
25664 INFO: Loading module hook 'hook-django.core.cache.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
25770 INFO: Loading module hook 'hook-django.core.mail.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
25807 INFO: Loading module hook 'hook-django.core.management.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
26521 INFO: Import to be excluded not found: 'matplotlib'
26529 INFO: Loading module hook 'hook-django.db.backends.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
27015 WARNING: Hidden import "django.db.backends.__pycache__.base" not found!
27020 INFO: Loading module hook 'hook-django.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
30750 INFO: Determining a mapping of distributions to packages...
37528 INFO: Packages required by django:
['sqlparse', 'tzdata', 'asgiref']
41420 INFO: Loading module hook 'hook-django.template.loaders.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
41497 INFO: Loading module hook 'hook-encodings.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
41553 INFO: Loading module hook 'hook-gevent.py' from 'C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\PyInstaller\\hooks'...
An error occurred while packaging
Traceback (most recent call last):
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\auto_py_to_exe\packaging.py", line 131, in package
    run_pyinstaller()
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\__main__.py", line 126, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\__main__.py", line 65, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\building\build_main.py", line 815, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\building\build_main.py", line 762, in build
    exec(code, spec_namespace)
  File "C:\Users\white\AppData\Local\Temp\tmphwzenqcz\password.spec", line 7, in <module>
    a = Analysis(['C:/Users/white/Desktop/Security/password.py'],
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\building\build_main.py", line 294, in __init__
    self.__postinit__()
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\building\datastruct.py", line 159, in __postinit__
    self.assemble()
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\building\build_main.py", line 473, in assemble
    self.graph.process_post_graph_hooks(self)
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\depend\analysis.py", line 373, in process_post_graph_hooks
    module_hook.post_graph(analysis)
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\depend\imphook.py", line 451, in post_graph
    self._load_hook_module()
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\depend\imphook.py", line 408, in _load_hook_module
    self._hook_module = importlib_load_source(
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\compat.py", line 632, in importlib_load_source
    return mod_loader.load_module()
  File "<frozen importlib._bootstrap_external>", line 529, in _check_name_wrapper
  File "<frozen importlib._bootstrap_external>", line 1029, in load_module
  File "<frozen importlib._bootstrap_external>", line 854, in load_module
  File "<frozen importlib._bootstrap>", line 274, in _load_module_shim
  File "<frozen importlib._bootstrap>", line 711, in _load
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\hooks\hook-gevent.py", line 16, in <module>
    datas, binaries, hiddenimports = collect_all(
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 1156, in collect_all
    hiddenimports = collect_submodules(package_name,
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 597, in collect_submodules
    names = exec_statement("""
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 98, in exec_statement
    return __exec_statement(statement, capture_stdout=True)
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 79, in __exec_statement
    return __exec_python_cmd(cmd, capture_stdout=capture_stdout)
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 70, in __exec_python_cmd
    txt = compat.exec_python(*cmd, env=pp_env)
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\compat.py", line 565, in exec_python
    return exec_command(*cmdargs, **kwargs)
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\site-packages\PyInstaller\compat.py", line 332, in exec_command
    out = proc.communicate(timeout=60)[0]
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1134, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "C:\Users\white\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1510, in _communicate
    raise TimeoutExpired(self.args, orig_timeout)
subprocess.TimeoutExpired: Command '('C:\\Users\\white\\AppData\\Local\\Programs\\Python\\Python39\\python.exe', '-c', '\nimport sys\nimport pkgutil\nimport traceback\n\n# ``pkgutil.walk_packages`` doesn\'t walk subpackages of zipped files\n# per https://bugs.python.org/issue14209. This is a workaround.\ndef walk_packages(path=None, prefix=\'\', onerror=None):\n    def seen(p, m={}):\n        if p in m:\n            return True\n        m[p] = True\n\n    for importer, name, ispkg in pkgutil.iter_modules(path, prefix):\n        if not name.startswith(prefix):   ## Added\n            name = prefix + name          ## Added\n        yield importer, name, ispkg\n\n        if ispkg:\n            try:\n                __import__(name)\n            except ImportError:\n                if onerror is not None:\n                    onerror(name)\n            except Exception:\n                if onerror is not None:\n                    onerror(name)\n                else:\n                    traceback.print_exc(file=sys.stderr)\n                    print("collect_submodules: failed to import %r!" %\n                          name, file=sys.stderr)\n            else:\n                path = getattr(sys.modules[name], \'__path__\', None) or []\n\n                # don\'t traverse path items we\'ve seen before\n                path = [p for p in path if not seen(p)]\n\n                ## Use Py2 code here. It still works in Py3.\n                for item in walk_packages(path, name+\'.\', onerror):\n                    yield item\n                ## This is the original Py3 code.\n                #yield from walk_packages(path, name+\'.\', onerror)\n\nfor module_loader, name, ispkg in walk_packages([\'C:\\\\Users\\\\white\\\\AppData\\\\Local\\\\Programs\\\\Python\\\\Python39\\\\lib\\\\site-packages\\\\gevent\'], \'gevent.\'):\n    print(\'\\n$_pyi:\' + name + \'*\')\n')' timed out after 60 seconds

Project output will not be moved to output folder
Complete.```


Solution 1:[1]

Never mind, figured it out! The issue was Windows Defender blocking the file when it was created.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ? WHITE