1 year ago

#370441

test-img

Langueneers

compile django app to executable using PyInstaller

I am trying to compile a Django app as an executable. I know that this is not best practice - the app was built as a server app running on log in, but now the app shall be used by Users who shall not have access to the server. The challenge is now to make the app installable in such a way that the average Windows user is capable of double-clicking the exe and type in the url in a browser of their choice. I am working on Linux with Django 3.2.7, Python 3.8 and PyInstaller 4.1 (I know that I must compile on a windows platform for Windows OS to run the exe, but I first wanted to try using PyInstaller on my OS).

Now, when starting the app, it complains about not finding a specific folder in the dist directory, which, when I add the demanded folder, changes to file not found "settings.pyc". ".pyc" files are compiled Python files, but I cannot grep the filename, so PyInstaller did not even create it.

I executed the PyInstaller command in the directory where the first "mysite" is located. I also tried to separately hook the "myapp" folder by adding a hook file as proposed here (https://github.com/pyinstaller/pyinstaller/discussions/5911). The project directory and the error message are as follows:

mysite
    manage.py
    mysite
        settings.py
        urls.py
        wsgi.py
        __init__.py
        __pycache__
    myapp
        static
        templates
        apps.py
        models.py
        urls.py
        views.py
        ...
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
  File "manage.py", line 18, in main
  File "django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "django/core/management/__init__.py", line 363, in execute
    settings.INSTALLED_APPS
  File "django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "django/conf/__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "django/conf/__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "importlib/__init__.py", line 127, in import_module
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "PyInstaller/loader/pyimod03_importers.py", line 495, in exec_module
  File "mysite/settings.py", line 16, in <module>
  File "pathlib.py", line 1181, in resolve
  File "pathlib.py", line 363, in resolve
  File "pathlib.py", line 347, in _resolve
  File "pathlib.py", line 452, in readlink
FileNotFoundError: [Errno 2] No such file or directory: '/home/me/Documents/App/dist/MyApp/mysite'
[6324] Failed to execute script 'manage' due to unhandled exception!

When I create the demanded directory "dist/MyApp/mysite", the app throws the error of not finding "settings.pyc". What do I miss? The only thing I can come up with as explanation is that PyInstaller does not compile code that needs to be compiled and added to a subfolder of the dist directory, but I could not find any according warnings/errors in the terminal. (I wanted to include the output of the PyInstaller process, but SO considered it to be spam).

This is the standard manage.py file:

import os
import sys


def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'qudviewer.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

the command I used is:

pyinstaller --name=myTool mysite/manage.py

I also tried adding a custom hook

pyinstaller --additional-hooks-dir=extra-hooks --name=myTool mysite/manage.py

with this hook file:

from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('mysite')

python

django

pyinstaller

executable

0 Answers

Your Answer

Accepted video resources