Django project to .exe with Pyinstaller - Missing modules, ModuleNotFoundError

天大地大妈咪最大 提交于 2019-12-13 02:50:00

问题


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.

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.

回答1:


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.

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.

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']


来源:https://stackoverflow.com/questions/49944462/django-project-to-exe-with-pyinstaller-missing-modules-modulenotfounderror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!