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

本文档将帮助您使用 Django。

## 安装 Python

Django 是一个 Python Web 框架。参见 [我应该使用哪个版本的 Python 来配合 Django?](/zh-hans/3.0/faq/install/#faq-python-version-support) 获取更多细节。

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

> **Windows 上的 Python**
>
> 如果您刚刚开始学习 Django 并且使用 Windows，查看 [如何在 Windows 上安装 Django](/zh-hans/3.0/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 模块，请查看 [Django 如何利用 mod\_wsgi 工作](/zh-hans/3.0/howto/deployment/wsgi/modwsgi/) 了解如何配置。

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

## 运行你的数据库

If you plan to use Django's database API functionality, you'll need to make
sure a database server is running. Django supports many different database
servers and is officially supported with [PostgreSQL](https://www.postgresql.org/), [MariaDB](https://mariadb.org/), [MySQL](https://www.mysql.com/), [Oracle](https://www.oracle.com/)
and [SQLite](https://www.sqlite.org/).

If you are developing a small project or something you don't plan to deploy in
a production environment, SQLite is generally the best option as it doesn't
require running a separate server. However, SQLite has many differences from
other databases, so if you are working on something substantial, it's
recommended to develop with the same database that you plan on using in
production.

除了官方支持的数据库，还有 [第三方提供的后端](/zh-hans/3.0/ref/databases/#third-party-notes) 允许你在 Django 中使用其他数据库。

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

- 如果你正在使用 PostgreSQL，你需要 [psycopg2](https://www.psycopg.org/) 包。相关详细信息请参阅 [PostgreSQL 笔记](/zh-hans/3.0/ref/databases/#postgresql-notes)。
- If you're using MySQL or MariaDB, you'll need a [DB API driver](/zh-hans/3.0/ref/databases/#mysql-db-api-drivers) like `mysqlclient`. See [notes for the MySQL
  backend](/zh-hans/3.0/ref/databases/#mysql-notes) for details.
- 如果你正在使用 SQLite，则可能需要阅读 [SQLite 后端笔记](/zh-hans/3.0/ref/databases/#sqlite-notes) 。
- 如果你正在使用 Oracle，则需要 [cx\_Oracle](https://oracle.github.io/python-cx_Oracle/) 的副本，但请阅读 [针对 Oracle 后端的笔记](/zh-hans/3.0/ref/databases/#oracle-notes) 以获取有关 Oracle 和 `cx_Oracle` 支持的版本的详细信息。
- 如果你使用的是非官方的第三方后端，请参阅提供的文档以了解任何其他要求。

If you plan to use Django's `manage.py migrate` 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 grant Django `SELECT`, `INSERT`, `UPDATE` and
`DELETE` permissions. After creating a database user with these permissions,
you'll specify the details in your project's settings file, see
[`DATABASES`](/zh-hans/3.0/ref/settings/#std-setting-DATABASES) for details.

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

## 安装 Django 源码

安装过程可能会有些许差异，这取决于你是否在安装一个发行版——某个特定的版本，下载最新的正式发布包，或获取最新的开发版本。

### 通过 `pip` 安装正式发布版本

以下是安装 Django 的推荐方式。

1. 安装 [pip](https://pip.pypa.io/)。最简单的方式是使用 [独立 pip 安装器](https://pip.pypa.io/en/latest/installing/#installing-with-get-pip-py)。若你的系统早已安装 `pip`，你可能需要更新它，因为它可能过期了。如果它过期了，你会知道的，因为过期的用不了。
2. Take a look at [venv](https://docs.python.org/3/tutorial/venv.html). This tool provides
   isolated Python environments, which are more practical than installing
   packages systemwide. It also allows installing packages without
   administrator privileges. The [contributing tutorial](/zh-hans/3.0/intro/contributing/) walks through how to create a virtual environment.
3. 在你已创建并激活一个虚拟环境后，输入以下命令：

   ```console
   $ python -m pip install Django
   ```

   *Windows*

   ```doscon
   ...\> py -m pip install Django
   ```

### 安装特定发行版

Check the [distribution specific notes](/zh-hans/3.0/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 supported upgrade paths; however, these packages will rarely
contain the latest release of Django.

### 安装开发版本

> **跟踪 Django 开发**
>
> 如果你决定使用 Django 的最新开发版，你需要关注 [开发版时间轴](https://code.djangoproject.com/timeline) 和 [即将到来的新特性的发布说明](/zh-hans/3.0/releases/#development-release-notes)。这将保证你能获取所有新特性和最新的代码。（对于稳定发布版，所有必要的修改都在发布说明中记录。）

如果你希望偶尔能获取最新的补丁和改进，遵循以下说明：

1. 确保你已安装了 [Git](https://git-scm.com/)，这样你就可以从 shell 运行对应命令。（在 shell 中输入 `git help` 测试是否安装。）
2. 像这样检出 Django 的主开发分支：

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

   *Windows*

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

   这会在当前目录创建一个 `django` 目录。
3. Make sure that the Python interpreter can load Django's code. The most
   convenient way to do this is to use a virtual environment and [pip](https://pip.pypa.io/). The
   [contributing tutorial](/zh-hans/3.0/intro/contributing/) walks through how to
   create a virtual environment.
4. After setting up and activating the virtual environment, run the following
   command:

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

   *Windows*

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

   这会让 Django 的代码可导入，使得 `django-admin` 命令行工具可用。换句话说，大事可为。

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