参考:一个和我同样遭遇的详细安装过程

http://blog.csdn.net/jeffreynicole/article/details/8280358

摘要:

环境:python:2.7.9 。django:1.7.1

情景:第一次在我的阿里云上的ECS上安装Django,并且建立第一个APP:blog;整个过程出现的错误有:ImportError: No module named blog  blog模块找不到,这是因为,我现在使用的django是1.7.1版本 ; 在django1.4之后,使用django创建一个project,然后在创建一个APP的目录结构就改变了!

django1.4:只有一个目录

django1.7.1:下面详细解释




[root@iZ28cumdzmgZ muahao01]# vim settings.py
将下面注释:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


报错:ImportError: No module named blog

分析:

我创建一个project:muahao01

产生一个文件和一个目录:

[root@iZ28cumdzmgZ muahao01]# ls
manage.py  muahao01(目录)
我想在muahao01这个项目下安装一个blog APP;

在我启动runserver的时候报错:

报错:找不到blog这个模块

[root@iZ28cumdzmgZ muahao01]# python manage.py runserver
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/apps/config.py", line 87, in create
    module = import_module(entry)
  File "/usr/local/python2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named blog



解决:分析,原来是# django-admin.py startapp blog这个命令需要在/root/muahao01这个目录下面执行!!!!我报错的原因是我在/root/muahao01/muahao01这个目录下面执行了;也就是说,# django-admin.py startapp blog产生的blog目录需要和manage.py同级目录!!


[root@iZ28cumdzmgZ muahao01]# pwd
/root/muahao01
[root@iZ28cumdzmgZ muahao01]# ls        #请在这个目录下,创建app  blog
manage.py  muahao01
[root@iZ28cumdzmgZ muahao01]# django-admin.py startapp blog
[root@iZ28cumdzmgZ muahao01]# tree
.
|-- blog
|   |-- __init__.py
|   |-- admin.py
|   |-- migrations
|   |   `-- __init__.py
|   |-- models.py
|   |-- tests.py
|   `-- views.py
|-- manage.py
`-- muahao01
    |-- __init__.py
    |-- __init__.pyc
    |-- settings.py
    |-- settings.pyc
    |-- urls.py
    |-- wsgi.py
    `-- wsgi.pyc
[root@iZ28cumdzmgZ muahao01]# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
December 22, 2014 - 10:55:14
Django version 1.7.1, using settings 'muahao01.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.




上面我们是在settting.py中,将数据库设置注释了!主要是因为我们没有安装sqlite3;

下面将数据库打开:

[root@iZ28cumdzmgZ muahao01]# vim settings.py

将下面注释取消:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


报错:


启动runserver:发现没有找到sqlite3

[root@iZ28cumdzmgZ muahao01]# python manage.py runserver
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/python2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/contrib/auth/models.py", line 40, in <module>
    class Permission(models.Model):
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/db/models/base.py", line 124, in __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/db/models/base.py", line 299, in add_to_class
    value.contribute_to_class(cls, name)
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/db/models/options.py", line 166, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/db/__init__.py", line 40, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/db/utils.py", line 242, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/db/utils.py", line 108, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/local/python2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/usr/local/python2.7/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 38, in <module>
    raise ImproperlyConfigured("Error loading either pysqlite2 or sqlite3 modules (tried in that order): %s" % exc)
django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named _sqlite3




下载:pysqlite 2.6.3

https://pypi.python.org/pypi/pysqlite/

安装sqlite3时报错:

[root@iZ28cumdzmgZ pysqlite-2.6.3]# pwd
/usr/local/pysqlite/pysqlite-2.6.3
[root@iZ28cumdzmgZ pysqlite-2.6.3]# python setup.py install
running install
running build
running build_py
running build_ext
building 'pysqlite2._sqlite' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DMODULE_NAME="pysqlite2.dbapi2" -DSQLITE_OMIT_LOAD_EXTENSION=1 -I/usr/local/pysqlite/pysqlite-2.6.3/include -I/usr/local/python2.7/include/python2.7 -c src/module.c -o build/temp.linux-x86_64-2.7/src/module.o
In file included from src/module.c:24:
src/connection.h:33:21: error: sqlite3.h: No such file or directory
In file included from src/module.c:24:
src/connection.h:38: error: expected specifier-qualifier-list before 'sqlite3'
In file included from src/module.c:25:
src/statement.h:37: error: expected specifier-qualifier-list before 'sqlite3'
src/module.c: In function 'module_complete':
src/module.c:103: warning: implicit declaration of function 'sqlite3_complete'
src/module.c: At top level:
src/module.c:269: error: 'SQLITE_OK' undeclared here (not in a function)
src/module.c:270: error: 'SQLITE_DENY' undeclared here (not in a function)
src/module.c:271: error: 'SQLITE_IGNORE' undeclared here (not in a function)
src/module.c:272: error: 'SQLITE_CREATE_INDEX' undeclared here (not in a function)
src/module.c:273: error: 'SQLITE_CREATE_TABLE' undeclared here (not in a function)
src/module.c:274: error: 'SQLITE_CREATE_TEMP_INDEX' undeclared here (not in a function)
src/module.c:275: error: 'SQLITE_CREATE_TEMP_TABLE' undeclared here (not in a function)
src/module.c:276: error: 'SQLITE_CREATE_TEMP_TRIGGER' undeclared here (not in a function)
src/module.c:277: error: 'SQLITE_CREATE_TEMP_VIEW' undeclared here (not in a function)
src/module.c:278: error: 'SQLITE_CREATE_TRIGGER' undeclared here (not in a function)
src/module.c:279: error: 'SQLITE_CREATE_VIEW' undeclared here (not in a function)
src/module.c:280: error: 'SQLITE_DELETE' undeclared here (not in a function)
src/module.c:281: error: 'SQLITE_DROP_INDEX' undeclared here (not in a function)
src/module.c:282: error: 'SQLITE_DROP_TABLE' undeclared here (not in a function)
src/module.c:283: error: 'SQLITE_DROP_TEMP_INDEX' undeclared here (not in a function)
src/module.c:284: error: 'SQLITE_DROP_TEMP_TABLE' undeclared here (not in a function)
src/module.c:285: error: 'SQLITE_DROP_TEMP_TRIGGER' undeclared here (not in a function)
src/module.c:286: error: 'SQLITE_DROP_TEMP_VIEW' undeclared here (not in a function)
src/module.c:287: error: 'SQLITE_DROP_TRIGGER' undeclared here (not in a function)
src/module.c:288: error: 'SQLITE_DROP_VIEW' undeclared here (not in a function)
src/module.c:289: error: 'SQLITE_INSERT' undeclared here (not in a function)
src/module.c:290: error: 'SQLITE_PRAGMA' undeclared here (not in a function)
src/module.c:291: error: 'SQLITE_READ' undeclared here (not in a function)
src/module.c:292: error: 'SQLITE_SELECT' undeclared here (not in a function)
src/module.c:293: error: 'SQLITE_TRANSACTION' undeclared here (not in a function)
src/module.c:294: error: 'SQLITE_UPDATE' undeclared here (not in a function)
src/module.c:295: error: 'SQLITE_ATTACH' undeclared here (not in a function)
src/module.c:296: error: 'SQLITE_DETACH' undeclared here (not in a function)
src/module.c: In function 'init_sqlite':
src/module.c:426: warning: implicit declaration of function 'sqlite3_libversion'
src/module.c:426: warning: passing argument 1 of 'PyString_FromString' makes pointer from integer without a cast
/usr/local/python2.7/include/python2.7/stringobject.h:63: note: expected 'const char *' but argument is of type 'int'
error: command 'gcc' failed with exit status 1


解决:

参考:

http://stackoverflow.com/questions/1448819/how-to-install-pysqlite

我的解决:

安装两个东西:

# yum install sqlite-devel
#yum install -y python-devel


但是我不知到和这个路径有没有关系:

[root@iZ28cumdzmgZ pysqlite-2.6.3]# pwd
/usr/local/pysqlite/pysqlite-2.6.3   #我把pysqlite放在这个位置了,也许需要配置下面这个配置文件
[root@iZ28cumdzmgZ pysqlite-2.6.3]# vim setup.cfg
include_dirs=/usr/local/include
library_dirs=/usr/local/lib
#include_dirs=/usr/local/pysqlite/pysqlite-2.6.3/include
#library_dirs=/usr/local/pysqlite/pysqlite-2.6.4/lib


启动runserver:

[root@iZ28cumdzmgZ muahao01]# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.

December 22, 2014 - 13:13:40
Django version 1.7.1, using settings 'muahao01.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.


继续:

[root@iZ28cumdzmgZ muahao01]# python manage.py migrate
Operations to perform:
  Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying sessions.0001_initial... OK
[root@iZ28cumdzmgZ muahao01]# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
December 22, 2014 - 13:20:27
Django version 1.7.1, using settings 'muahao01.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.


虽然,sunserver已经启动。但是打开浏览器,输入http://127.0.0.1:8000/blog/index你会发现,没有打开你写的页面!!原因是:我现在操作的是我的 ECS,在阿里云上!

我需要这样启动runserver:

[root@iZ28cumdzmgZ muahao01]# python manage.py runserver 121.42.138.224:8000

然后在浏览器中输入:http://121.42.138.224:8000/blog/index/  可以看到:

hello Django!