配置python问题实践中的解决方法集锦(本问题集锦将持续更新,欢迎提出问题并在文下交流)

来源:互联网 发布:如何用c语言开发软件 编辑:程序博客网 时间:2024/05/17 15:36

以下都是从实践中来,完全是个人所经历的真实问题集合。所以大可放心其可信度。


系统检查命令:在根目录打开cmd:python manage.py syncdb


1,变量导致的错误:

 

 raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named mysite.settings


ImportError: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named mysite.settings


提示:django.wsgi 内容:

解决办法:1,设置绝对路径:

import os, sys

sys.path.insert(0, "\\填写实际绝对路径\\....")


例如:


sys.path.insert(0, "\\usr/home\\joe\\lib\\python")
sys.path.insert(0, "\\usr\\local\\lib\\python")


注意:这里的双反斜杠是正规的sys.path.语言格式。


解决办法2: 在计算机变量中设置PATH变量值。


盘符:/项目根路径


例如:c:/apache/mysite/apache/wsgi



2,"NameError: name 'os' is not defined"  ? 



You try to use something from module os, which is not imported, thus you cannot use it -> add its import somewhere at the beggining of 'settings.py':
import os
edit:
if you don't have GOOGLE_OAUTH2_CLIENT_ID in os.evniron, don't load it from there, set it directly in settings.py:
GOOGLE_OAUTH2_CLIENT_ID = 'your-actual-client-id-value'


 3,"NameError: name 'BASE' is not defined"  ? (这个变量仅供参考)

import os
BASE = os.path.dirname(__file__)
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))


MEDIA_ROOT = os.path.join(BASE, 'site-media')
MEDIA_URL = '/site-media/'
ADMIN_MEDIA_PREFIX = '/admin-media/'


import sys
path = os.path.dirname(os.path.realpath(__file__))
sys.path.append("c:\\apache\\mysite")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

====================================================================================

4,模板载入错误?

修改settings.py,对模板设置使用绝对路径

举例1:

[plain] view plaincopy
  1. import os  
  2. SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))  
  3.   
  4. TEMPLATE_DIRS = (  
  5.     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".  
  6.     # Always use forward slashes, even on Windows.  
  7.   
  8.     # Don't forget to use absolute paths, not relative paths.  
  9. #    'templates'  
  10.     os.path.join(SETTINGS_PATH, '../templates'),  
  11. )  


5,数据库访问出错


前提条案:到下列地址安装相应版本的mysql-python 

根据数据库类型选择安装Python包

http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python

http://sourceforge.net/projects/mysql-python/files/mysql-python/

如果是MySQL数据库请选择:

Last MySQL for Python:

http://sourceforge.net/projects/mysql-python/files/latest/download?source=files

解决方法:

对数据库设置绝对路径

[python] view plaincopy
  1. DATABASES = {  
  2.     'default': {  
  3.         'ENGINE''django.db.backends.sqlite3'# Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.  
  4.         'NAME''/var/www/vhost1/tdjproj/db/tdata.sqlite3',                      # Or path to database file if using sqlite3.  
  5.         'USER''',                      # Not used with sqlite3.  
  6.         'PASSWORD''',                  # Not used with sqlite3.  
  7.         'HOST''',                      # Set to empty string for localhost. Not used with sqlite3.  
  8.         'PORT''',                      # Set to empty string for default. Not used with sqlite3.  
  9.     }  
  10. }  

举例2:


from settings import *DEBUG = FalseTEMPLATE_DEBUG = False# The default database should point to the master.DATABASES = {    'default': {        'NAME': 'zamboni',        'ENGINE': 'django.db.backends.mysql',        'HOST': '',        'PORT': '',        'USER': '',        'PASSWORD': '',        'OPTIONS': {'init_command': 'SET storage_engine=InnoDB'},    },    'slave': {        'NAME': 'zamboni',        'ENGINE': 'django.db.backends.mysql',        'HOST': '',        'PORT': '',        'USER': '',        'PASSWORD': '',        'OPTIONS': {'init_command': 'SET storage_engine=InnoDB'},    },}# Put the aliases for slave databases in this list.SLAVE_DATABASES = ['slave']# Use IP:PORT pairs separated by semicolons.CACHE_BACKEND = 'django_pylibmc.memcached://localhost:11211;localhost:11212?timeout=500'# This is used to hash some things in Django.SECRET_KEY = 'replace me with something long'# Remove any apps that are only needed for development.INSTALLED_APPS = tuple(app for app in INSTALLED_APPS if app not in DEV_APPS)LOG_LEVEL = logging.WARNINGDEBUG_PROPAGATE_EXCEPTIONS = DEBUG

6,数据库名称空白或设置不正确导致的错误:


错误提示:


ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.


数据库类型:

django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'


名称填写处:


ENGINE''django.db.backends.sqlite3'# Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.  


如下:


MANAGERS = ADMINS


DATABASES = {
       'default': {
       'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
       # The following settings are not used with sqlite3:
        'USER': '',
       'PASSWORD': '',
       'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}


7,DatabaseError: (1046, 'No database selected')

没有数据选择,请重装数据库

原创粉丝点击