py2exe and Django Import Error no module named apps

こ雲淡風輕ζ 提交于 2020-01-13 05:11:51

问题


I'm trying to make a standalone Django (1.8.2) app using py2exe.

To achieve that I have a setup.py file with all settings required by py2exe: modules to be included, not to include, etc. The py2exe console parameter (within my setup.py) is set to 'manage.py'. After running it with python setup.py py2exe py2exe runs succesfully adding all (?) required modules and my Django apps. I get an manage.exe file - seems this is working, right?

However, after running the manage.exe file I get the following error:

File "manage.py", line 11, in <module>
File "django\core\management\__init__.pyo", line 338, in execute_from_command_line
File "django\core\management\__init__.pyo", line 312, in execute
File "django\__init__.pyo", line 18, in setup
File "django\apps\registry.pyo", line 85, in populate
File "django\apps\config.pyo", line 112, in create
File "importlib\__init__.pyo", line 37, in import_module
ImportError: No module named apps

I have added django.apps to the setup.py import modules part and it didn't solve the problem. Why is this happening and how to fix that?

UPDATE:

For the setup.py file I have followed this website: https://misunderstandings.wordpress.com/2008/06/26/django-desktop-app/ and after customizing it to my needs here's what I have ended-up with:

setup.py:

# encoding: utf-8
from distutils.core import setup
import py2exe
import os
import reportlab

def add_path_tree( base_path, path, skip_dirs=[ '.svn', '.git' ]):
  path = os.path.join( base_path, path )
  partial_data_files = []
  for root, dirs, files in os.walk( os.path.join( path )):
    sample_list = []
    for skip_dir in skip_dirs:
      if skip_dir in dirs:
        dirs.remove( skip_dir )
    if files:
      for filename in files:
        sample_list.append( os.path.join( root, filename ))
    if sample_list:
      partial_data_files.append((
        root.replace(
          base_path + os.sep if base_path else '',
          '',
          1
        ),
        sample_list
      ))
  return partial_data_files


 py2exe_options = {
    'py2exe': {
      'compressed': 1,
      'optimize': 2,
      'ascii': 1,
      'bundle_files': 1,
      'dist_dir': 'dist',
      'packages': [ 'encodings','gabinet','pacjent','kalendarz','terapeuta','superwizja','ustawienia','zapiszsesje_registration', ],
      'excludes' : [
        'pywin',
        'pywin.debugger',
        'pywin.debugger.dbgcon',
        'pywin.dialogs',
        'pywin.dialogs.list',
        'Tkconstants',
        'Tkinter',
        'tcl',
      ],
      'dll_excludes': [ 'w9xpopen.exe', 'MSVCR71.dll' ],
      'includes': [
        'django.template.loaders.filesystem',
        'django.template.loaders.app_directories',
        'django.middleware.common',
        'django.contrib.sessions.middleware',
        'django.contrib.auth.middleware',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sessions.backends.db',
        'django.contrib.sites',
        'django.contrib.admin',
        'django.core.cache.backends',
        'django.db.backends.sqlite3.base',
        'django.db.backends.sqlite3.introspection',
        'django.db.backends.sqlite3.creation',
        'django.db.backends.sqlite3.client',
        'django.template.defaulttags',
        'django.template.defaultfilters',
        'django.template.loader_tags',
        'django.contrib.admin.views.main',
        'django.core.context_processors',
        'django.contrib.auth.views',
        'django.contrib.auth.backends',
        'django.views.static',
        'django.views.defaults',
        'django.core.cache.backends.locmem',
        'django.templatetags.i18n',
        'django.views.i18n',
        'django.apps',
        'email.mime.audio',
        'email.mime.base',
        'email.mime.image',
        'email.mime.message',
        'email.mime.multipart',
        'email.mime.nonmultipart',
        'email.mime.text',
        'email.charset',
        'email.encoders',
        'email.errors',
        'email.feedparser',
        'email.generator',
        'email.header',
        'email.iterators',
        'email.message',
        'email.parser',
        'email.utils',
        'email.base64mime',
        'email.quoprimime',
            'django.db.backends.sqlite3.base',
        'django.db.backends.sqlite3.introspection',
        'django.db.backends.sqlite3.creation',
        'django.db.backends.sqlite3.client',
        'django.apps', 
        'django.template.defaulttags',
        'django.template.defaultfilters',
        'django.template.loader_tags',
        'django.core.wsgi',
        'gabinet.forms',
        'gabinet.models',
        'gabinet.urls',
        'gabinet.views',
        'kalendarz.forms',
        'kalendarz.models',
        'kalendarz.urls',
        'kalendarz.views',
        'pacjent.forms',
        'pacjent.models',
        'pacjent.urls',
        'pacjent.views',
        'superwizja.models',
        'superwizja.urls',
        'superwizja.urls',
        'superwizja.views',
        'terapeuta.models',
        'terapeuta.settings',
        'terapeuta.urls',
        'terapeuta.views',
        'terapeuta.wsgi',
        'ustawienia.forms',
        'ustawienia.models',
        'ustawienia.urls',
        'ustawienia.views',
        'zapiszsesje_registration.auth_urls',
        'zapiszsesje_registration.forms',
        'zapiszsesje_registration.models',
        'zapiszsesje_registration.urls',
        'zapiszsesje_registration.views',
      ],
    }
  }

  # Take the first value from the environment variable PYTHON_PATH
  python_path = os.environ[ 'PYTHONPATH' ].split( ';' )[ 0 ]

  django_admin_path = os.path.normpath( python_path + '/lib/site-packages/django/contrib/admin' )
  py2exe_data_files = []

  # django admin files
  py2exe_data_files += add_path_tree( django_admin_path, 'templates' )
  py2exe_data_files += add_path_tree( django_admin_path, 'media' )
  # project files
  py2exe_data_files += add_path_tree( '', 'db' )
  py2exe_data_files += add_path_tree( '', 'templates' )

  setup(
    options=py2exe_options,
    data_files=py2exe_data_files,
    zipfile = None,
    console=[ 'manage.py' ],
  )

来源:https://stackoverflow.com/questions/32735536/py2exe-and-django-import-error-no-module-named-apps

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