---
title: "如何安装Django"
version: 2.1
locale: zh-hans
source: https://docs.djangoproject.com/zh-hans/2.1/topics/install/
canonical: https://djangodocs.dev/zh-hans/2.1/topics/install/
---
# 如何安装Django

本文档将帮助您使用Django。

## 安装Python

Django is a Python Web framework. See [我应该使用哪个版本的 Python 来配合 Django?](/zh-hans/2.1/faq/install/#faq-python-version-support) for
details.

获取最新版本的Python可以通过：访问 <https://www.python.org/downloads/> ；或者操作系统的包管理工具。

> **基于 Jython 的 Django**
>
> [Jython](http://www.jython.org/) （一个针对Java平台的Python实现）和Python 3不兼容，所以Django 2.0及以上的版本无法在Jython上运行。

> **Windows上的Python**
>
> 如果您刚刚开始学习Django并且使用Windows，查看 [如何在Windows上安装Django](/zh-hans/2.1/howto/windows/)  可能对你有帮助。

## 安装Apache和 `mod_wsgi`

如果您只是想试验Django，请跳到下一部分；Django包含一个可用于测试的轻量级Web服务器，因此在准备好在生产环境中部署Django之前，您不需要设置Apache。

如果要在生产站点上使用Django，请将 [Apache](https://httpd.apache.org/) 与 [mod\_wsgi](https://modwsgi.readthedocs.io/en/develop/) 一起使用。 mod\_wsgi以两种模式中的一种运行：嵌入模式或守护进程模式。在嵌入模式下，mod\_wsgi类似于mod\_perl —— 它在Apache中嵌入Python，并在服务器启动时将Python代码加载到内存中。代码在Apache进程的整个生命周期中都保留在内存中，与其他服务器相比，这可以显着提高性能。在守护进程模式下，mod\_wsgi 会生成一个处理请求的独立守护进程。守护进程可以作为与Web服务器不同的用户运行，可能会提高安全性。可以在不重新启动整个Apache Web服务器的情况下重新启动守护进程，从而可以更加无缝地刷新代码库。请参阅mod\_wsgi文档以确定适合您的设置的模式。确保你在安装了Apache并且启用了mod\_wsgi模块。 Django将在任何支持mod\_wsgi的Apache版本上工作。

关于如何在安装后配置mod\_wsgi模块，请查看 [How to use Django with mod\_wsgi](/zh-hans/2.1/howto/deployment/wsgi/modwsgi/) 。

如果由于某种原因你不能使用mod\_wsgi，请不要担心： Django支持许多其他部署选项。一个是 [uWSGI](/zh-hans/2.1/howto/deployment/wsgi/uwsgi/) ；它和 [nginx](https://nginx.org/) 配合使用很好。此外，Django遵循 WSGI 规范（ [**PEP 3333**](https://peps.python.org/pep-3333/) ），允许它在各种服务器平台上运行。

## 运行你的数据库

如果你打算使用Django的数据库API功能，则需要确保数据库服务器正在运行。Django支持很多不同的数据库服务器，并且正式支持 [PostgreSQL](https://www.postgresql.org/)， [MySQL](https://www.mysql.com/)， [Oracle](https://www.oracle.com/) 和 [SQLite](https://www.sqlite.org/) 。

如果你正在开发一个简单的项目或者你不打算在生产环境中部署的东西，SQLite通常是最简单的选择，因为它不需要运行一个独立的服务器。但是，SQLite与其他数据库有许多不同之处，因此如果你正在开展大量工作，建议使用你计划在生产中使用的相同数据库进行开发。

除了官方支持的数据库，还有 [backends provided by 3rd parties](/zh-hans/2.1/ref/databases/#third-party-notes) 允许你在Django中使用其他数据库。

除了数据库后端，你还要确保安装了Python数据库绑定。

- 如果你正在使用PostgreSQL，你需要 [psycopg2](http://initd.org/psycopg/) 包。相关详细信息请参阅 [PostgreSQL notes](/zh-hans/2.1/ref/databases/#postgresql-notes) 。
- 如果你正在使用MySQL，则需要一个像 mysqlclient\`\`这样的 [DB API driver](/zh-hans/2.1/ref/databases/#mysql-db-api-drivers) 。相关详细信息请参阅 [notes for the MySQL backend](/zh-hans/2.1/ref/databases/#mysql-notes) 。
- 如果你正在使用SQLite，则可能需要阅读 [SQLite backend notes](/zh-hans/2.1/ref/databases/#sqlite-notes) 。
- 如果你正在使用Oracle，则需要 [cx\_Oracle](https://oracle.github.io/python-cx_Oracle/) 的副本，但请阅读 [notes for the Oracle backend](/zh-hans/2.1/ref/databases/#oracle-notes) 以获取有关 Oracle 和 `cx_Oracle` 的支持的版本的详细信息。
- 如果你使用的是非官方的第三方后端，请参阅提供的文档以了解任何其他要求。

如果您打算使用Django的 `manage.py migrate` 命令为您的模型自动创建数据库表（在首次安装Django并创建项目之后），您需要确保Django有权在您正在使用的数据库中创建和更改表；如果你打算手动创建这些表，你可以只需授予Django `SELECT`，`INSERT`，`UPDATE` 和 DELETE\`\`权限。创建具有这些权限的数据库用户后，您要在项目的配置文件中指定详细信息，请参阅 [`DATABASES`](/zh-hans/2.1/ref/settings/#std-setting-DATABASES) 以获取详细信息。

如果你正在使用Django的 [testing framework](/zh-hans/2.1/topics/testing/) 来测试数据库查询，Django将需要创建测试数据库的权限。

## 安装Django代码

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 an official release with `pip`

This is the recommended way to install Django.

1. Install [pip](https://pip.pypa.io/). The easiest is to use the [standalone pip installer](https://pip.pypa.io/en/latest/installing/#installing-with-get-pip-py). If your
   distribution already has `pip` installed, you might need to update it if
   it's outdated. If it's outdated, you'll know because installation won't
   work.
2. Take a look at [virtualenv](https://virtualenv.pypa.io/) and [virtualenvwrapper](https://virtualenvwrapper.readthedocs.io/en/latest/). These tools provide
   isolated Python environments, which are more practical than installing
   packages systemwide. They also allow installing packages without
   administrator privileges. The [contributing tutorial](/zh-hans/2.1/intro/contributing/) walks through how to create a virtualenv.
3. After you've created and activated a virtual environment, enter the command:

   ```console
   $ pip install Django
   ```

   *Windows*

   ```doscon
   ...\> pip install Django
   ```

### Installing a distribution-specific package

Check the [distribution specific notes](/zh-hans/2.1/misc/distributions/) 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; however, these packages will rarely
contain the latest release of 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](https://code.djangoproject.com/timeline),
> and you'll want to keep an eye on the [release notes for the
> upcoming release](/zh-hans/2.1/releases/#development-release-notes). 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:

1. Make sure that you have [Git](https://git-scm.com/) installed and that you can run its commands
   from a shell. (Enter `git help` at a shell prompt to test this.)
2. Check out Django's main development branch like so:

   ```console
   $ git clone https://github.com/django/django.git
   ```

   *Windows*

   ```doscon
   ...\> git clone https://github.com/django/django.git
   ```

   This will create a directory `django` in your current directory.
3. Make sure that the Python interpreter can load Django's code. The most
   convenient way to do this is to use [virtualenv](https://virtualenv.pypa.io/), [virtualenvwrapper](https://virtualenvwrapper.readthedocs.io/en/latest/), and
   [pip](https://pip.pypa.io/). The [contributing tutorial](/zh-hans/2.1/intro/contributing/) walks through
   how to create a virtualenv.
4. After setting up and activating the virtualenv, run the following command:

   ```console
   $ pip install -e django/
   ```

   *Windows*

   ```doscon
   ...\> pip install -e django\
   ```

   This will make Django's code importable, and will also make the
   `django-admin` utility command available. In other words, you're all
   set!

When you want to update your copy of the Django source code, just run the
command `git pull` from within the `django` directory. When you do this,
Git will automatically download any changes.
