首先可以去django官网查一下合适的版本(支持python 2.7 和python3.x)

安装方法参考官网,Option 2中 development version是非稳定版本,可以下载tar.gz包安装

Python环境(3.7.1)

方法一:使用pip安装("==" 后写django版本号)

pip install Django==2.1.4

安装成功后可以使用命令 python -m django --version 查看版本,也可以进入python交互环境执行 import django确认无报错。

Python 3.7.1 (default, Dec 15 2018, 19:57:31)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>>

>>> import django

>>>

方法二:使用源码安装

首先,把官网下载的tar.gz上传至服务器并解压缩

# tar -xzvf Django-2.1.4.tar.gz

然后进入Django解压后的路径,里面有setup.py,执行python setup.py install

# ls

Django-2.1.4  Django-2.1.4.tar.gz

# cd Django-2.1.4

# python setup.py install

如果顺利的话,安装完成。但是,遇到了两个问题

问题1:

[root@xxx Django-2.1.4]# python setup.py install

Traceback (most recent call last):

File "setup.py", line 32, in

""".format(*(REQUIRED_PYTHON + CURRENT_PYTHON)))

ValueError: zero length field name in format

[root@xoam Django-2.1.4]# python setup.py help

Traceback (most recent call last):

File "setup.py", line 32, in

""".format(*(REQUIRED_PYTHON + CURRENT_PYTHON)))

ValueError: zero length field name in format

原因:上网查了一下有博主进一步分析得出是python版本的问题。检查了一下自己的环境除了python3.7还妆了python2.6,而python2.6是不支持django的。

解决:执行python命令带上python3全路径,例如

[root@xxx Django-2.1.4]# /opt/python3/Python-3.7.1/python setup.py install

或者使用python3(没有试,应该可以)

[root@xxx Django-2.1.4]# python3 setup.py install

以上报错解决后,安装过程遇到了第二个报错

问题2:

...

Installed /usr/local/lib/python3.7/site-packages/Django-2.1.4-py3.7.egg

Processing dependencies for Django==2.1.4

Searching for pytz

Reading https://pypi.python.org/simple/pytz/

Download error on https://pypi.python.org/simple/pytz/: unknown url type: https -- Some packages may not be found!

Couldn't find index page for 'pytz' (maybe misspelled?)

Scanning index of all packages (this may take a while)

Reading https://pypi.python.org/simple/

Download error on https://pypi.python.org/simple/: unknown url type: https -- Some packages may not be found!

No local packages or working download links found for pytz

error: Could not find suitable distribution for Requirement.parse('pytz')

于是去python官网下载pytz安装(同样解压后去pytz路径执行python setup.py install)

再去安装Django,成功~

...

Installed /usr/local/lib/python3.7/site-packages/Django-2.1.4-py3.7.egg

Processing dependencies for Django==2.1.4

Searching for pytz==2018.7

Best match: pytz 2018.7

Processing pytz-2018.7-py3.7.egg

pytz 2018.7 is already the active version in easy-install.pth

Using /usr/local/lib/python3.7/site-packages/pytz-2018.7-py3.7.egg

Finished processing dependencies for Django==2.1.4

[root@xxx Django-2.1.4]# /opt/python3/Python-3.7.1/python -m django --version

2.1.4