问题
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 errorModuleNotFoundError: 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