走过了不少弯路,前面的步骤已经实现了:

1 系统centos6.5
2 安装了python3.3 并且安装了pip
3 同时保留了系统中的python2.6.6

如果使用过程中出现了各种错误,要么就是缺少模块,要么就是lib库没有正确配置
现在安装Django

  1. python3 -m pip install django
  2. python3 django-admin.py startproject testdj
  3. cd testdj
  4. python manage.py runserver

错误1:

[root@localhost ~]# python3 django-admin.py
python3: can’t open file ‘django-admin.py’: [Errno 2] No such file or directory
这种错误需要把Django bin路径配置到环境变量去,也可以直接到Django的bin目录下去执行

* 错误2 :*

缺少sqlite3
其实python2.5以上就自动集成了sqlite3,但是为什么提示缺少呢?
import也出现了这种错误,使用python2.6正常

>>> import sqlite3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/python3.3/lib/python3.3/sqlite3/__init__.py", line 23, in <mo                                                                                        dule>
    from sqlite3.dbapi2 import *
  File "/usr/local/python3.3/lib/python3.3/sqlite3/dbapi2.py", line 26, in <modu                                                                                        le>
    from _sqlite3 import *
ImportError: No module named '_sqlite3'

解决方法

安装sqlite-devel-3.3.6-2.x86_64.rpm:
[root@localhost mysite]# yum install sqlite-devel
重新安装python3.3:
cd 到python3的安装包
make
make install
import sqlite3 正常没有错误了

错误3

[root@localhost testdj]# python manage.py runserver
Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
ImportError: No module named django.core.management

这个是需要到你创建的项目那个路径下去执行,否则会有些错误
最后解决了
错误4

[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142] mod_wsgi (pid=15591): Target WSGI script '/var/www/airport2.safaroff.com/baku_airport/wsgi.py' cannot be loaded as Python module.
[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142] mod_wsgi (pid=15591): Exception occurred processing WSGI script '/var/www/airport2.safaroff.com/baku_airport/wsgi.py'.
[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142] Traceback (most recent call last):
[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142]   File "/var/www/airport2.safaroff.com/baku_airport/wsgi.py", line 12, in <module>
[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142]     from django.core.wsgi import get_wsgi_application
[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142]   File "/usr/local/lib/python3.4/site-packages/django/core/wsgi.py", line 2, in <module>
[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142]     from django.core.handlers.wsgi import WSGIHandler
[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142]   File "/usr/local/lib/python3.4/site-packages/django/core/handlers/wsgi.py", line 10, in <module>
[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142]     from django import http
[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142]   File "/usr/local/lib/python3.4/site-packages/django/http/__init__.py", line 2, in <module>
[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142]     from django.http.request import (
[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142]   File "/usr/local/lib/python3.4/site-packages/django/http/request.py", line 10, in <module>
[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142]     from django.core import signing
[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142]   File "/usr/local/lib/python3.4/site-packages/django/core/signing.py", line 51, in <module>
[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142]     from django.utils.module_loading import import_string
[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142]   File "/usr/local/lib/python3.4/site-packages/django/utils/module_loading.py", line 67, in <module>
[Thu Feb 11 21:38:00 2016] [error] [client 37.26.12.142]     from importlib.util import find_spec as importlib_find

这个是因为Django和当前的python版本不兼容,可以升级python或者安装低版本的Django ,我选择了安装低版本的Django

[root@localhost testdj]# python3 manage.py runserver
Validating models...

0 errors found
December 25, 2016 - 14:48:11
Django version 1.6, using settings 'testdj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

验证一下,发现成功了

[root@localhost testdj]# curl -XGET http://127.0.0.1:8000

<!DOCTYPE html>
<html lang="en"><head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="robots" content="NONE,NOARCHIVE"><title>Welcome to Django</title>
  <style type="text/css">
    html * { padding:0; margin:0; }
    body * { padding:10px 20px; }
    body * * { padding:0; }
    body { font:small sans-serif; }
    body>div { border-bottom:1px solid #ddd; }
    h1 { font-weight:normal; }
    h2 { margin-bottom:.8em; }
    h2 span { font-size:80%; color:#666; font-weight:normal; }
    h3 { margin:1em 0 .5em 0; }
    h4 { margin:0 0 .5em 0; font-weight: normal; }
    table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
    tbody td, tbody th { vertical-align:top; padding:2px 3px; }
    thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
    tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
    #summary { background: #e0ebff; }
    #summary h2 { font-weight: normal; color: #666; }
    #explanation { background:#eee; }
    #instructions { background:#f6f6f6; }
    #summary table { border:none; background:transparent; }
  </style>
</head>

<body>
<div id="summary">
  <h1>It worked!</h1>
  <h2>Congratulations on your first Django-powered page.</h2>
</div>

<div id="instructions">
  <p>
    Of course, you haven't actually done any work yet.
    Next, start your first app by running <code>python manage.py startapp [appname]</code>.
  </p>
</div>

<div id="explanation">
  <p>
    You're seeing this message because you have <code>DEBUG = True</code> in your
    Django settings file and you haven't configured any URLs. Get to work!
  </p>
</div>
</body></html>

后面开始Django的学习,从菜鸟教程开始
http://www.runoob.com/django/django-install.html

python3 和python2同时存在的时候,尤其是在linux上面进行环境安装时比较麻烦,但是遇到问题还是都能够解决的。

Logo

更多推荐