在Windows上使用pyinstaller的独立Django应用程序

时间:2014-01-19 21:10:23

标签: django windows pyinstaller

我需要将一个(相当简单的)django应用程序打包到windows上的独立可执行文件中。在使用py2exe尝试失败之后(我真的不明白为什么每个人都为django推荐它,设置起来似乎非常复杂)我试过pyinstaller。它确实打包应用程序而没有抱怨,但如果我尝试使用myapp.exe runserver localhost:8000运行它,我收到以下错误:

Traceback (most recent call last):
  File "<string>", line 5, in <module>
  File "c:\python27\lib\site-packages\PyInstaller-2.1-py2.7.egg\PyInstaller\loader\pyi_importers.py", line 270, in load_
module
    exec(bytecode, module.__dict__)
  File "...\build\ck\out00-PYZ.pyz\django.test", line 5, in <module>
  File "c:\python27\lib\site-packages\PyInstaller-2.1-py2.7.egg\PyInstaller\loader\pyi_importers.py", line 270, in load_
module
    exec(bytecode, module.__dict__)
  File "...\build\ck\out00-PYZ.pyz\django.test.client", line 21, in <module>
  File "c:\python27\lib\site-packages\PyInstaller-2.1-py2.7.egg\PyInstaller\loader\pyi_importers.py", line 270, in load_
module
    exec(bytecode, module.__dict__)
  File "...\build\ck\out00-PYZ.pyz\django.db", line 11, in <module>
  File "...\build\ck\out00-PYZ.pyz\django.conf", line 52, in __getattr__
  File "...\build\ck\out00-PYZ.pyz\django.conf", line 45, in _setup
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must eith
er define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

我的manage.py脚本如下所示:

#!/usr/bin/env python
import os
import sys
import Cookie
import django.test
import HTMLParser
from django.conf import settings

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

为什么它无法识别环境变量,我该如何解决?

1 个答案:

答案 0 :(得分:0)

我通过在行

之后导入django.test 解决了这个问题
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")