2

I am working to convert my Django project to a .exe file using Pyinstaller. I want to be able to just click an icon and open the project in a browser. Here is my folder structure:

proj
    __pycache__
    proj
        __pycache__
        __init__.py
        manage.py
        Dashboard
            __pycache__
            __init__.py
            urls.py
        proj
            __pycache__
            __init__.py
            settings.py
            urls.py
            wsgi.py
    static_cdn

And here is my manage.py file:

# -*- coding: utf-8 -*-
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj.settings")
    print("here")
    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

    import django.test
    from html.parser import HTMLParser
    execute_from_command_line(sys.argv)

Currently I cd to C:...\proj, then run pyinstaller --name=Dashboard proj/manage.py. Then when I click on Dashboard.exe in C:...\proj\dist\Dashboard, an error comes up.errorMessage

I'm not sure what is going wrong here. I think I may either have something wrong with my folder structure, or I may be calling the pyinstaller in the wrong folder. Any help is super appreciated!

Additional information:

  • I am following directions from this tutorial on how to make a .exe
  • A similar question suggested adding an __init__.py file to C:...\proj\proj, however this makes the pyinstaller function fail with the error ModuleNotFoundError: No module named 'proj.settings'.
  • I followed the answer of this question in my manage.py file.
Clay Records
  • 464
  • 6
  • 18

1 Answers1

6

Looks like the folder structure is fine, it was missing an import. In my manage.py file, I added the line "import Dashboard".

However, after I tried to run pyinstaller again it said it was missing more modules, like django.contrib.admin.apps. The missing modules seem to be coming from my settings.py file. enter image description here

To import these, I added them to my SPEC file. Whenever I had run pyinstaller before, it had added three new items: a build folder, a dist folder, and a Dashboard.spec file. In the SPEC file I appended my installed app names from settings (plus .apps) to the hiddenimports list.

Here is the entirety of my .spec file. enter image description here

This solved the problem and it compiled fine.

EDIT:

I eventually added more hiddenImports than listed above. Here are all the hiddenImports that I currently use.

hiddenimports=['django.contrib.admin.apps', 'django.contrib.auth.apps', 'django.contrib.contenttypes.apps', 'django.contrib.sessions.apps', 'django.contrib.messages.apps', 'django.contrib.staticfiles.apps', 'django.contrib.messages.middleware', 'django.contrib.sessions.middleware', 'django.contrib.sessions.serializers', 'django.template.loaders', 'django.contrib.auth.context_processors', 'django.contrib.messages.context_processors']
Clay Records
  • 464
  • 6
  • 18
  • I had the same issue as you with apps that I added to my settings.py. Your solution to editing the spec file helped me. – user1470034 Jun 20 '18 at 15:01
  • Could you elaborate on the additional files you had to add to hiddenimport? – user1470034 Jun 20 '18 at 15:15
  • Based on your edit i'm assuming we are having the same issue with the django.contrib.messages.middleware, etc. not coming over correctly. Even though the hook was included it's still occuring. – user1470034 Jun 22 '18 at 13:52
  • running pyinstaller again basically overwrites the .spec, how can I rerun pyinstaller without overwriting the .spec – PolarBear10 Oct 04 '22 at 17:02
  • @PolarBear10, if you run `pyinstaller name.spec` it will use the settings in the .spec file. If you don't want to use the .spec file and do not want to overwrite it, I guess just rename it. – Clay Records Oct 18 '22 at 18:47
  • what if I have more external pip packages or libraries. do i do that as hidden imports as well and how with path? – Gary Jul 03 '23 at 10:09