2014-09-24 45 views
1

我想從python到達我的MySQL數據庫。它運行在Openshift - Python 2.7,MySQL 5.5上。 Django和Mysql部署運行得很好。 PHPMyAdming的作品。raise ImportError「設置無法導入」ENVIRONMENT_VARIABLE

編輯setup.py

setup(
    name='YourAppName', 
    version='1.0', 
    description='OpenShift App', 
    author='Your Name', 
    author_email='[email protected]', 
    url='http://www.python.org/sigs/distutils-sig/', 
    install_requires=['Django<=1.4', 'MySQL-python',], 
) 


DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.mysql', 
     'NAME': os.environ['OPENSHIFT_APP_NAME'], 
     'USER': os.environ['OPENSHIFT_MYSQL_DB_USERNAME'], 
     'PASSWORD': os.environ['OPENSHIFT_MYSQL_DB_PASSWORD'], 
     'HOST': os.environ['OPENSHIFT_MYSQL_DB_HOST'], 
     'PORT': os.environ['OPENSHIFT_MYSQL_DB_PORT'] 
    } 
} 

但是,當我把這個

from django.db import connection 

到一些PY劇本我看到這個日誌:

mod_wsgi (pid=327977): Target WSGI script '/var/lib/openshift/542145275xxxxx9330000e6/app-root/runtime/repo/wsgi.py' cannot be loaded as Python module. 
mod_wsgi (pid=327977): Exception occurred processing WSGI script '/var/lib/openshift/542145275xxxxx9330000e6/app-root/runtime/repo/wsgi.py'. 
Traceback (most recent call last): 
File "/var/lib/openshift/542145275xxxxx9330000e6/app-root/runtime/repo/wsgi.py", line 18, in <module> 
from django.db import connection 
File "/var/lib/openshift/542145275xxxxx9330000e6/python/virtenv/lib/python2.7/site-packages/Django-1.4-py2.7.egg/django/db/__init__.py", line 11, in <module> 
if DEFAULT_DB_ALIAS not in settings.DATABASES: 
File "/var/lib/openshift/542145275xxxxx9330000e6/python/virtenv/lib/python2.7/site-packages/Django-1.4-py2.7.egg/django/utils/functional.py", line 184, in inner 
self._setup() 
File "/var/lib/openshift/542145275xxxxx9330000e6/python/virtenv/lib/python2.7/site-packages/Django-1.4-py2.7.egg/django/conf/__init__.py", line 40, in _setup 
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE) 
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined. 

可能是什麼原因問題? 感謝您的任何答案。

編輯: 部署,因爲它是,沒有settings.py(參照本應在DJANGO_SETTINGS_MODULE,我說得對不對?所以,現在我創建了一個後來我改變的變量是這樣的:

export DJANGO_SETTINGS_MODULE=var.lib.openshift.542145275xxxxx9330000e6.app-root.runtime.repo.settings 

我嘗試了不同的值,子文件夾等林不知道什麼正確的值

我刪除從setup.py數據庫並將它們轉移到我的新創建的settings.py 它看起來像這樣:。

import os 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.mysql', 
     'NAME': os.environ['OPENSHIFT_APP_NAME'], 
     'USER': os.environ['OPENSHIFT_MYSQL_DB_USERNAME'], 
     'PASSWORD': os.environ['OPENSHIFT_MYSQL_DB_PASSWORD'], 
     'HOST': os.environ['OPENSHIFT_MYSQL_DB_HOST'], 
     'PORT': os.environ['OPENSHIFT_MYSQL_DB_PORT'] 
    } 
} 

沒有變化。 爲什麼DJAGNO_SETTING_MODULE仍然是「未定義」(即使它會被定義爲錯誤)?

+2

「設置無法導入的部分,因爲環境變量DJANGO_SETTINGS_MODULE未定義。」令你感到困惑? – geoffspear 2014-09-24 15:28:58

+0

好的,我沒有寫得好。我能做些什麼來定義DJANGO_SETTINGS_MODULE? – 2014-09-24 15:34:05

+0

@PařezPařez:更具體。你是如何研究這個問題的?你採取了哪些步驟? (我假設你已經問了Google如何設置一個環境變量,我還假設你已經諮詢了關於如何使用DJANGO_SETTINGS_MODULE的django文檔)。 – 2014-09-24 15:56:21

回答

2

這是一個django項目嗎?如果是這樣,你錯過了很多的設置在settings.py文件,這裏是默認settings.py Django的1.4:

# Django settings for foo project. 

DEBUG = True 
TEMPLATE_DEBUG = DEBUG 

ADMINS = (
    # ('Your Name', '[email protected]'), 
) 

MANAGERS = ADMINS 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 
     'NAME': '',      # Or path to database file if using sqlite3. 
     'USER': '',      # Not used with sqlite3. 
     'PASSWORD': '',     # Not used with sqlite3. 
     'HOST': '',      # Set to empty string for localhost. Not used with sqlite3. 
     'PORT': '',      # Set to empty string for default. Not used with sqlite3. 
    } 
} 

# Local time zone for this installation. Choices can be found here: 
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name 
# although not all choices may be available on all operating systems. 
# On Unix systems, a value of None will cause Django to use the same 
# timezone as the operating system. 
# If running in a Windows environment this must be set to the same as your 
# system time zone. 
TIME_ZONE = 'America/Chicago' 

# Language code for this installation. All choices can be found here: 
# http://www.i18nguy.com/unicode/language-identifiers.html 
LANGUAGE_CODE = 'en-us' 

SITE_ID = 1 

# If you set this to False, Django will make some optimizations so as not 
# to load the internationalization machinery. 
USE_I18N = True 

# If you set this to False, Django will not format dates, numbers and 
# calendars according to the current locale. 
USE_L10N = True 

# If you set this to False, Django will not use timezone-aware datetimes. 
USE_TZ = True 

# Absolute filesystem path to the directory that will hold user-uploaded files. 
# Example: "/home/media/media.lawrence.com/media/" 
MEDIA_ROOT = '' 

# URL that handles the media served from MEDIA_ROOT. Make sure to use a 
# trailing slash. 
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/" 
MEDIA_URL = '' 

# Absolute path to the directory static files should be collected to. 
# Don't put anything in this directory yourself; store your static files 
# in apps' "static/" subdirectories and in STATICFILES_DIRS. 
# Example: "/home/media/media.lawrence.com/static/" 
STATIC_ROOT = '' 

# URL prefix for static files. 
# Example: "http://media.lawrence.com/static/" 
STATIC_URL = '/static/' 

# Additional locations of static files 
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
) 

# List of finder classes that know how to find static files in 
# various locations. 
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
# 'django.contrib.staticfiles.finders.DefaultStorageFinder', 
) 

# Make this unique, and don't share it with anybody. 
SECRET_KEY = 'vwbv9j#2vauzpvya8#_qm=rgm&amp;[email protected][email protected])ji' 

# List of callables that know how to import templates from various sources. 
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader', 
    'django.template.loaders.app_directories.Loader', 
#  'django.template.loaders.eggs.Loader', 
) 

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    # Uncomment the next line for simple clickjacking protection: 
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware', 
) 

ROOT_URLCONF = 'foo.urls' 

# Python dotted path to the WSGI application used by Django's runserver. 
WSGI_APPLICATION = 'foo.wsgi.application' 

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
) 

INSTALLED_APPS = (
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    # Uncomment the next line to enable the admin: 
    # 'django.contrib.admin', 
    # Uncomment the next line to enable admin documentation: 
    # 'django.contrib.admindocs', 
) 

# A sample logging configuration. The only tangible logging 
# performed by this configuration is to send an email to 
# the site admins on every HTTP 500 error when DEBUG=False. 
# See http://docs.djangoproject.com/en/dev/topics/logging for 
# more details on how to customize your logging configuration. 
LOGGING = { 
    'version': 1, 
    'disable_existing_loggers': False, 
    'filters': { 
     'require_debug_false': { 
      '()': 'django.utils.log.RequireDebugFalse' 
     } 
    }, 
    'handlers': { 
     'mail_admins': { 
      'level': 'ERROR', 
      'filters': ['require_debug_false'], 
      'class': 'django.utils.log.AdminEmailHandler' 
     } 
    }, 
    'loggers': { 
     'django.request': { 
      'handlers': ['mail_admins'], 
      'level': 'ERROR', 
      'propagate': True, 
     }, 
    } 
} 

您需要將該文件保存在一個目錄,然後指向DJANGO_SETTINGS_MODULE蟒蛇導入路徑對此文件。這意味着它不是文件路徑,而是類似DJANGO_SETTINGS_MODULE=someapp.settings

+0

謝謝,最後想出了這是如何工作的(即時通訊對Python的新增功能)。我經歷了sys.path。它只需要DJANGO_SETTINGS_MODULE =設置(因爲它在應用程序的根目錄中)。當您獲取時,我也擴展了settings.py。 – 2014-09-24 16:31:36

相關問題