如何安装 Django
本文档将指导你安装及运行 Django 框架。
安装 Python
作为一个 Python Web 框架,Django 是需要 Python 的。
Django 能在 Python 2.6.5 至 2.7 之间所有的版本下运行。它还具有实验性的支持 Python 3.2.3 至 3.3 版本的功能。
在 http://www.python.org 上获取 Python 。如果你运行在 Linux 或 Mac OS X 系统下,你可能 已经安装好了。
在 Jython 上运行 Django
如果你使用 Jython ( 一个在 Java 平台上实现的 Python ),你需要遵循一些额外的步骤。 请参考Running Django on Jython 获取更多信息。
在 Windows 系统上运行 Python
在 Windows 系统上,你可能需要i调整你的 PATH 环境变量来包含 Python 的可执行文件以及 附加的脚本文件的目录。比如说,假设你的 Python 安装在 C:Python27 目录下,那么你需要将如下所示的目录添加到你的 PATH 中去
安装 Apache 和 mod_wsgi
如果你只是想体验下 Django ,那么你可以直接跳到下一节了;Django 内置了一个轻量级的 Web 服务器让你用于测试,因此直到你准备在生产环境下部署 Django 之前,你都不需要配置 Apache 。
如果你想要在生产环境下使用 Django ,请使用包含 mod_wsgi 模块的 Apache 。 mod_wsgi 模块可以运行在两种模式下:嵌入模式和守护进程模式。 嵌入模式下, mod_wsgi 模块类似于 mod_perl 模块 mod_perl – 当服务器启动时它将在 Apache 内嵌入 Python 并将 Python 代码加载到内存中。 在 Apache 进程的整个生命周期内代码一直驻留在内存中,这会导致显著的性能提升超过其他 服务器的安排。守护进程模式下,mod_wsgi 将会生成一个独立的守护进程来处理请求。 这个守护进程在服务器中能以不同的用户身份运行,导致更高的安全性,而且重启守护进程无需 重启整个 Apache Web 服务器,从而使你无缝更新你的代码。 请参阅 mod_wsgi 文档来确定哪一种模式适合你的需求。 请确保你已经安装了 Apache ,并且激活了 mo_wsgi 模块。 Django 可以运行在任何带有 mod_wsgi 模块的 Apache 版本下。
请参阅 怎样通过 mod_wsgi 运行 Django 了解更多有关在安装 Django 后怎样配置 mod_wsgi 模块的信息。
如果某些原因你不能使用 mod_wsgi 模块,不要担心:Django 支持许多其他可选的的部署方案。 其中之一是 uWSGI;它在 nginx 下性能很好。另一个是 FastCGI , 完美运行 Django 的非 Apache 服务器。此外,Django 遵循 WSGI 规范 (PEP 3333),这使得它能运行在各种服务器平台上。 每个平台的具体安装说明请参阅 server-arrangements wiki 页面 。
运行你的数据库
如果你打算使用 Django 的数据库 API 功能,你将需要确保有一个运行中的数据库服务器。 Django 支持许多不同的数据库并正式支持 PostgreSQL ,MySQL_ ,Oracle_ 和 SQLite 。
如果你正在开发一个简单的项目,或者你不打算在生产环境下部署项目, SQLite 通常是最简单的选择,因为它不要求运行一个单独的服务器。 然后,SQLite 与其他数据库之间有许多差异,因此如果你在处理一些实质性的工作,建议你在准备使用在生产环境 中相同的数据库下进行开发。
除了正式支持的数据库外,还有由第 3 方提供的后端功能包,允许你在 Django 下使用其他数据库:
Django 支持的版本和 ORM 特性的这些非官方的后端功能包之间有很大的不同。 有关这些非官方的后端特定功能的查询,以及任何支持的功能查询,应该是通过每个第 3 方项目 提供的支持渠道进行查询。
除了数据库后端,你还需要确保你访问数据库的 Python 包已经安装。
如果你使用 PostgreSQL ,你将需要安装 postgresql_psycopg2 包。 你可能需要参考我们的 PostgreSQL 笔记进一步了解有关此数据库的 技术细节。
如果你使用的是 Windows , 请安装非官方的 compiled Windows version.
如果你使用 MySQL , 你将需要安装 MySQL-python 包,需要 1.2.1p2 或更高版本。你还会想要阅读 notes for the MySQL backend 了解此数据库的具体说明的。
如果你使用 Oracle ,你将需要安装 cx_Oracle 包,但是请仔细阅读 notes for the Oracle backend 有关数据库的特定说明 获取有关受支持版本的 Oracle 和 cx_Oracle 的重要信息。
如果你使用的是非官方的第 3 方后端,请参考文档提供的任何附加要求。
If you plan to use Django’s manage.py syncdb command to automatically create database tables for your models (after first installing Django and creating a project), you’ll need to ensure that Django has permission to create and alter tables in the database you’re using; if you plan to manually create the tables, you can simply grant Django SELECT, INSERT, UPDATE and DELETE permissions. On some databases, Django will need ALTER TABLE privileges during syncdb but won’t issue ALTER TABLEstatements on a table once syncdb has created it. After creating a database user with these permissions, you’ll specify the details in your project’s settings file, see DATABASES for details.
If you’re using Django’s testing framework to test database queries, Django will need permission to create a test database.
Remove any old versions of Django
If you are upgrading your installation of Django from a previous version, you will need to uninstall the old Django version before installing the new version.
If you installed Django using pip or easy_install previously, installing with pip or easy_install again will automatically take care of the old version, so you don’t need to do it yourself.
If you previously installed Django using python setup.py install, uninstalling is as simple as deleting the django directory from your Python site-packages. To find the directory you need to remove, you can run the following at your shell prompt (not the interactive Python prompt):
Install the Django code
Installation instructions are slightly different depending on whether you’re installing a distribution-specific package, downloading the latest official release, or fetching the latest development version.
It’s easy, no matter which way you choose.
Installing a distribution-specific package
Check the distribution specific notes to see if your platform/distribution provides official Django packages/installers. Distribution-provided packages will typically allow for automatic installation of dependencies and easy upgrade paths.
Installing an official release with pip
This is the recommended way to install Django.
Install pip. The easiest is to use the standalone pip installer. If your distribution already has pipinstalled, you might need to update it if it’s outdated. (If it’s outdated, you’ll know because installation won’t work.)
(optional) Take a look at virtualenv and virtualenvwrapper. These tools provide isolated Python environments, which are more practical than installing packages systemwide. They also allow installing packages without administrator privileges. It’s up to you to decide if you want to learn and use them.
If you’re using Linux, Mac OS X or some other flavor of Unix, enter the commandsudo pip install Django at the shell prompt. If you’re using Windows, start a command shell with administrator privileges and run the command pip install Django. This will install Django in your Python installation’s site-packages directory.
If you’re using a virtualenv, you don’t need sudo or administrator privileges, and this will install Django in the virtualenv’s site-packages directory.
Installing an official release manually
Download the latest release from our download page.
Untar the downloaded file (e.g. tar xzvf Django-X.Y.tar.gz, where X.Y is the version number of the latest release). If you’re using Windows, you can download the command-line tool bsdtar to do this, or you can use a GUI-based tool such as 7-zip.
Change into the directory created in step 2 (e.g. cd Django-X.Y).
If you’re using Linux, Mac OS X or some other flavor of Unix, enter the commandsudo python setup.py install at the shell prompt. If you’re using Windows, start a command shell with administrator privileges and run the command python setup.py install. This will install Django in your Python installation’s site-packages directory.
Removing an old version
If you use this installation technique, it is particularly important that you remove any existing installations of Django first. Otherwise, you can end up with a broken installation that includes files from previous versions that have since been removed from Django.
Installing the development version
Tracking Django development
If you decide to use the latest development version of Django, you’ll want to pay close attention to the development timeline, and you’ll want to keep an eye on the release notes for the upcoming release. This will help you stay on top of any new features you might want to use, as well as any changes you’ll need to make to your code when updating your copy of Django. (For stable releases, any necessary changes are documented in the release notes.)
If you’d like to be able to update your Django code occasionally with the latest bug fixes and improvements, follow these instructions:
Make sure that you have Git installed and that you can run its commands from a shell. (Entergit help at a shell prompt to test this.)
Check out Django’s main development branch (the ‘trunk’ or ‘master’) like so:
This will create a directory django-trunk in your current directory.
Make sure that the Python interpreter can load Django’s code. The most convenient way to do this is via pip. Run the following command:
(If using a virtualenv you can omit sudo.)
This will make Django’s code importable, and will also make the django-admin.py utility command available. In other words, you’re all set!
If you don’t have pip available, see the alternative instructions for installing the development version without pip.
Warning
Don’t run sudo python setup.py install, because you’ve already carried out the equivalent actions in step 3.
When you want to update your copy of the Django source code, just run the command git pull from within the django-trunk directory. When you do this, Git will automatically download any changes.
Installing the development version without pip
If you don’t have pip, you can instead manually modify Python’s search path.
First follow steps 1 and 2 above, so that you have a django-trunk directory with a checkout of Django’s latest code in it. Then add a .pth file containing the full path to the django-trunk directory to your system’s site-packages directory. For example, on a Unix-like system:
In the above line, change WORKING-DIR/django-trunk to match the full path to your new django-trunkdirectory, and change SITE-PACKAGES-DIR to match the location of your system’s site-packagesdirectory.
The location of the site-packages directory depends on the operating system, and the location in which Python was installed. To find your system’s site-packages location, execute the following:
(Note that this should be run from a shell prompt, not a Python interactive prompt.)
Some Debian-based Linux distributions have separate site-packages directories for user-installed packages, such as when installing Django from a downloaded tarball. The command listed above will give you the system’s site-packages, the user’s directory can be found in /usr/local/lib/ instead of/usr/lib/.
Next you need to make the django-admin.py utility available in your shell PATH.
On Unix-like systems, create a symbolic link to the file django-trunk/django/bin/django-admin.py in a directory on your system path, such as /usr/local/bin. For example:
(In the above line, change WORKING-DIR to match the full path to your new django-trunk directory.)
This simply lets you type django-admin.py from within any directory, rather than having to qualify the command with the full path to the file.
On Windows systems, the same result can be achieved by copying the filedjango-trunk/django/bin/django-admin.py to somewhere on your system path, for exampleC:Python27Scripts.