Standalone with django + pyinstaller

3

Dear, I have developed an app with django, but now I have a problem when I want to compile binary (.exe). I'm using Django + PyInstaller, although I can get to get the binary, but when executing it there is an error that obstructs the execution of the app.

When I run: Django + PyInstaller the .exe does nothing, closes quickly, I can not see the error and it does not activate the server (# & mysqt.exe runserver 127.0.0.1:8000)

Detail of the compilation with PyInstaller

C:\DjangoCRM>pyinstaller --name=mysite manage.py
140 INFO: PyInstaller: 3.2
140 INFO: Python: 3.5.3
140 INFO: Platform: Windows-7-6.1.7601-SP1
140 INFO: wrote C:\DjangoCRM\mysite.spec
140 INFO: UPX is not available.
140 INFO: Extending PYTHONPATH with paths
['C:\DjangoCRM', 'C:\DjangoCRM']
140 INFO: checking Analysis
140 INFO: Building Analysis because out00-Analysis.toc is non existent
140 INFO: Initializing module dependency graph...
148 INFO: Initializing module graph hooks...
148 INFO: Analyzing base_library.zip ...
2048 INFO: running Analysis out00-Analysis.toc
2057 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of f
inal executable
  required by C:\Users\AppData\Local\Programs\Python\Python35-32\python.
exe
2101 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api-
ms-win-crt-stdio-l1-1-0.dll
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 695, in ge
tImports
    return _getImports_pe(pth)
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 122, in _g
etImports_pe
    dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
2449 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api-
ms-win-crt-heap-l1-1-0.dll
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 695, in ge
tImports
    return _getImports_pe(pth)
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 122, in _g
etImports_pe
    dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
2449 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api-
ms-win-crt-locale-l1-1-0.dll
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 695, in ge
tImports
    return _getImports_pe(pth)
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 122, in _g
etImports_pe
    dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
2494 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api-
ms-win-crt-runtime-l1-1-0.dll
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 695, in ge
tImports
    return _getImports_pe(pth)
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 122, in _g
etImports_pe
    dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
2529 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api-
ms-win-crt-math-l1-1-0.dll
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 695, in ge
tImports
    return _getImports_pe(pth)
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 122, in _g
etImports_pe
    dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
2529 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api-
ms-win-crt-process-l1-1-0.dll
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 695, in ge
tImports
    return _getImports_pe(pth)
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 122, in _g
etImports_pe
    dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
2556 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api-
ms-win-crt-string-l1-1-0.dll
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 695, in ge
tImports
    return _getImports_pe(pth)
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 122, in _g
etImports_pe
    dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
2565 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api-
ms-win-crt-conio-l1-1-0.dll
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 695, in ge
tImports
    return _getImports_pe(pth)
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 122, in _g
etImports_pe
    dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
2574 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api-
ms-win-crt-convert-l1-1-0.dll
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 695, in ge
tImports
    return _getImports_pe(pth)
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 122, in _g
etImports_pe
    dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
2583 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api-
ms-win-crt-filesystem-l1-1-0.dll
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 695, in ge
tImports
    return _getImports_pe(pth)
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 122, in _g
etImports_pe
    dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
2592 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api-
ms-win-crt-time-l1-1-0.dll
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 695, in ge
tImports
    return _getImports_pe(pth)
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 122, in _g
etImports_pe
    dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
2601 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api-
ms-win-crt-environment-l1-1-0.dll
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 695, in ge
tImports
    return _getImports_pe(pth)
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 122, in _g
etImports_pe
    dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
2601 INFO: Caching module hooks...
2601 INFO: Analyzing C:\DjangoCRM\manage.py
3132 INFO: Processing pre-find module path hook   distutils
5234 INFO: Loading module hooks...
5234 INFO: Loading module hook "hook-django.core.cache.py"...
5287 INFO: Loading module hook "hook-encodings.py"...
5296 INFO: Loading module hook "hook-lib2to3.py"...
5305 INFO: Loading module hook "hook-pkg_resources.py"...
5385 INFO: Processing pre-safe import module hook   win32com
5569 INFO: Loading module hook "hook-django.core.management.py"...
6568 INFO: Import to be excluded not found: 'matplotlib'
6568 INFO: Import to be excluded not found: 'tkinter'
6568 INFO: Excluding import 'IPython'
6568 WARNING:   Removing import django.core.management.commands.shell from modul
e IPython
6568 WARNING:   Removing import django.core.management.commands.shell from modul
e IPython.start_ipython
6568 INFO: Loading module hook "hook-django.py"...
6568 INFO: Django root directory C:\DjangoCRM\DjangoCRM
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\utils\hooks\subproc\django_import_fin
der.py", line 37, in <module>
    list(settings.TEMPLATE_LOADERS) + \
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\django\conf\__init__.py", line 57, in __getattr__
    val = getattr(self._wrapped, name)
AttributeError: 'Settings' object has no attribute 'TEMPLATE_CONTEXT_PROCESSORS'

7818 INFO: Collecting Django migration scripts.
8610 INFO: Loading module hook "hook-sysconfig.py"...
8610 INFO: Loading module hook "hook-xml.py"...
8620 INFO: Loading module hook "hook-django.db.backends.py"...
8953 WARNING: Hidden import "django.db.backends.__pycache__.base" not found!
8953 INFO: Loading module hook "hook-xml.dom.domreg.py"...
8963 INFO: Loading module hook "hook-pythoncom.py"...
9169 INFO: Loading module hook "hook-sqlite3.py"...
9179 INFO: Loading module hook "hook-pytz.py"...
9326 INFO: Loading module hook "hook-pywintypes.py"...
9522 INFO: Loading module hook "hook-win32com.py"...
9816 INFO: Loading module hook "hook-pydoc.py"...
9816 INFO: Loading module hook "hook-django.core.mail.py"...
9856 INFO: Loading module hook "hook-distutils.py"...
9856 INFO: Loading module hook "hook-django.db.backends.mysql.base.py"...
9856 INFO: Loading module hook "hook-django.db.backends.oracle.base.py"...
9964 INFO: Looking for ctypes DLLs
9973 INFO: Analyzing run-time hooks ...
9983 INFO: Including run-time hook 'pyi_rth_django.py'
9983 INFO: Including run-time hook 'pyi_rth_pkgres.py'
9983 INFO: Including run-time hook 'pyi_rth_win32comgenpy.py'
10003 INFO: Looking for dynamic libraries
11777 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api
-ms-win-crt-utility-l1-1-0.dll
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 695, in ge
tImports
    return _getImports_pe(pth)
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 122, in _g
etImports_pe
    dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
12189 WARNING: Can not get binary dependencies for file: C:\Windows\system32\api
-ms-win-crt-multibyte-l1-1-0.dll
Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 695, in ge
tImports
    return _getImports_pe(pth)
  File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2-py3.5.egg\PyInstaller\depend\bindepend.py", line 122, in _g
etImports_pe
    dll, _ = sym.forwarder.split('.')
TypeError: a bytes-like object is required, not 'str'
12189 INFO: Looking for eggs
12189 INFO: Using Python library C:\Users\AppData\Local\Programs\Python\
Python35-32\python35.dll
12189 INFO: Found binding redirects:
[]
12209 INFO: Warnings written to C:\DjangoCRM\build\mysite\warnmysite.txt
12356 INFO: checking PYZ
12356 INFO: Building PYZ because out00-PYZ.toc is non existent
12356 INFO: Building PYZ (ZlibArchive) C:\DjangoCRM\build\mysite\out00-PYZ.pyz
13989 INFO: checking PKG
13989 INFO: Building PKG because out00-PKG.toc is non existent
13999 INFO: Building PKG (CArchive) out00-PKG.pkg
14028 INFO: Bootloader C:\Users\AppData\Local\Programs\Python\Python35-3
2\lib\site-packages\pyinstaller-3.2-py3.5.egg\PyInstaller\bootloader\Windows-32b
it\run.exe
14038 INFO: checking EXE
14038 INFO: Building EXE because out00-EXE.toc is non existent
14038 INFO: Building EXE from out00-EXE.toc
14038 INFO: Appending archive to EXE C:\DjangoCRM\build\mysite\mysite.exe
14058 INFO: checking COLLECT
14058 INFO: Building COLLECT because out00-COLLECT.toc is non existent
14058 INFO: Building COLLECT out00-COLLECT.toc

After so much searching, the problem was to find how to import the "hidden" files that contains the pyinstaller hook directory. What I did was create a file as explained by @FJSevilla and add the following script:

#-----------------------------------------------------------------------------
# Copyright (c) 2005-2016, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License with exception
# for distributing bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

from PyInstaller.utils.hooks import collect_submodules
hiddenimports = [
    "dns.rdtypes.*",
    "dns.rdtypes.ANY.*",
    "django.contrib",
    "django.contrib.admin.apps",
    "django.contrib.auth.apps",
    "django.contrib.contenttypes.apps",
    "django.contrib.sessions.models",
    "django.contrib.sessions.apps"

]

But now another problem arises, the server still does not run and it shows me the following error:

Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you
 apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
May 23, 2017 - 19:35:32
Django version 1.11.1, using settings 'DjangoCRM.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Unhandled exception in thread started by <function check_errors.<locals>.wrapper
 at 0x02C06348>
Traceback (most recent call last):
  File "site-packages\django\core\servers\basehttp.py", line 47, in get_internal
_wsgi_application
  File "site-packages\django\utils\module_loading.py", line 20, in import_string

  File "importlib\__init__.py", line 126, in import_module
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 634, in _load_backward_compatible
  File "C:\Users\Carrito\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2.1-py3.5.egg\PyInstaller\loader\pyimod03_importers.py", line
 389, in load_module
    exec(bytecode, module.__dict__)
  File "DjangoCRM\wsgi.py", line 16, in <module>
  File "site-packages\django\core\wsgi.py", line 14, in get_wsgi_application
  File "site-packages\django\core\handlers\wsgi.py", line 151, in __init__
  File "site-packages\django\core\handlers\base.py", line 80, in load_middleware

  File "site-packages\django\utils\module_loading.py", line 20, in import_string

  File "importlib\__init__.py", line 126, in import_module
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'django.contrib.messages.middleware'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "site-packages\django\utils\autoreload.py", line 227, in wrapper
  File "site-packages\django\core\management\commands\runserver.py", line 147, i
n inner_run
  File "site-packages\django\core\management\commands\runserver.py", line 68, in
 get_handler
  File "site-packages\django\core\servers\basehttp.py", line 57, in get_internal
_wsgi_application
  File "site-packages\django\utils\six.py", line 685, in reraise
  File "site-packages\django\core\servers\basehttp.py", line 47, in get_internal
_wsgi_application
  File "site-packages\django\utils\module_loading.py", line 20, in import_string

  File "importlib\__init__.py", line 126, in import_module
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 664, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 634, in _load_backward_compatible
  File "C:\Users\Carrito\AppData\Local\Programs\Python\Python35-32\lib\site-pack
ages\pyinstaller-3.2.1-py3.5.egg\PyInstaller\loader\pyimod03_importers.py", line
 389, in load_module
    exec(bytecode, module.__dict__)
  File "DjangoCRM\wsgi.py", line 16, in <module>
  File "site-packages\django\core\wsgi.py", line 14, in get_wsgi_application
  File "site-packages\django\core\handlers\wsgi.py", line 151, in __init__
  File "site-packages\django\core\handlers\base.py", line 80, in load_middleware

  File "site-packages\django\utils\module_loading.py", line 20, in import_string

  File "importlib\__init__.py", line 126, in import_module
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
django.core.exceptions.ImproperlyConfigured: WSGI application 'DjangoCRM.wsgi.ap
plication' could not be loaded; Error importing module: 'No module named 'django
.contrib.messages.middleware''

I want to know if someone had the same problem or can give me an idea of what the error might be.

    
asked by Nahuel Jakobson 19.05.2017 в 14:22
source

3 answers

1

I was able to make the app work, the problem was always the hidden files. I pass the script that solved the problem for me:

from PyInstaller.utils.hooks import collect_submodules
hiddenimports = [
    "dns.rdtypes.*",
    "dns.rdtypes.ANY.*",
    "django.contrib",
    "django.contrib.admin.apps",
    "django.contrib.auth.apps",
    "django.contrib.contenttypes.apps",
    "django.contrib.sessions.models",
    "django.contrib.sessions.apps",
    "django.contrib.messages.middleware",
    "django.contrib.auth.middleware",
    "django.contrib.sessions.middleware",
    "django.contrib.sessions.serializers"

]

Thanks to everyone!

    
answered by 24.05.2017 / 01:01
source
2

In the Changelog for PyInstaller you can see that they have corrected the following error:

  • (Windows) Correctly decodes byte object produced by pefile ( # 1981 )

Upgrade to pyinstaller version 3.2.1.

    
answered by 21.05.2017 в 18:04
1

The error you are presenting now:

  

'ImportError: No module named' django.contrib.messages.apps''

The problem is that pyinstaller can not get django.contrib , possibly a hidden import. You can try to create a file hook to enable pyinstaller to find the library properly:

  • Go to the directory where you have Python installed (if you have more than one version of Python logically go to the one you use with Pyinstaller).

  • Go to Lib\site-packages\PyInstaller\hooks

  • Inside the hooks folder you create a new file and call it:

      

    hook-django.contrib.py

  • Add the following content to this file and save it:

    #-----------------------------------------------------------------------------
    # Copyright (c) 2013-2016, PyInstaller Development Team.
    #
    # Distributed under the terms of the GNU General Public License with exception
    # for distributing bootloader.
    #
    # The full license is in the file COPYING.txt, distributed with this software.
    #-----------------------------------------------------------------------------
    
    
    from PyInstaller.utils.hooks import collect_submodules
    hiddenimports = collect_submodules('django.contrib')
    

Then create your executable from scratch.

It is really difficult to find the solution to your problem because we have no way to emulate it. If you still have problems, it would be very helpful if you try to create a minimum application example with django that gives you the same problem as this one and add the code to be able to reproduce it ourselves.

    
answered by 22.05.2017 в 23:21