---
title: "如何在 Windows 上安装 Django"
version: 3.0
locale: zh-hans
source: https://docs.djangoproject.com/zh-hans/3.0/howto/windows/
canonical: https://djangodocs.dev/zh-hans/3.0/howto/windows/
---
# 如何在 Windows 上安装 Django

This document will guide you through installing Python 3.8 and Django on
Windows. It also provides instructions for setting up a virtual environment,
which makes it easier to work on Python projects. This is meant as a beginner's
guide for users working on Django projects and does not reflect how Django
should be installed when developing patches for Django itself.

The steps in this guide have been tested with Windows 10. In other
versions, the steps would be similar. You will need to be familiar with using
the Windows command prompt.

## 安装Python

Django is a Python web framework, thus requiring Python to be installed on your
machine. At the time of writing, Python 3.8 is the latest version.

To install Python on your machine go to <https://python.org/downloads/>. The
website should offer you a download button for the latest Python version.
Download the executable installer and run it. Check the boxes next to "Install
launcher for all users (recommended)" then click "Install Now".

安装后，打开命令提示符，检查 Python 版本是否与你装的一致，通过运行:

```doscon
...\> py --version
```

> **See also**
>
> 想知晓更多细节，请查看 [Using Python on Windows](https://docs.python.org/3/using/windows.html) 文档。

## 关于 `pip`

[pip](https://pypi.org/project/pip/) is a package manager for Python and is included by default with the
Python installer. It helps to install and uninstall Python packages
(such as Django!). For the rest of the installation, we'll use `pip` to
install Python packages from the command line.

## Setting up a virtual environment

It is best practice to provide a dedicated environment for each Django project
you create. There are many options to manage environments and packages within
the Python ecosystem, some of which are recommended in the [Python
documentation](https://packaging.python.org/guides/tool-recommendations/).
Python itself comes with [venv](https://docs.python.org/3/tutorial/venv.html) for managing
environments which we will use for this guide.

To create a virtual environment for your project, open a new command prompt,
navigate to the folder where you want to create your project and then enter the
following:

```doscon
...\> py -m venv project-name
```

This will create a folder called 'project-name' if it does not already exist
and setup the virtual environment. To activate the environment, run:

```doscon
...\> project-name\Scripts\activate.bat
```

The virtual environment will be activated and you'll see "(project-name)" next
to the command prompt to designate that. Each time you start a new command
prompt, you'll need to activate the environment again.

## 安装 Django

Django 可以轻松地在你的虚拟环境中使用 `pip` 安装。

在命令提示行中，确认虚拟环境是激活的，然后运行以下命令:

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

这将下载并安装最新的 Django 发布版本。

安装完成后，你可以在命令提示符运行 `django-admin --version` 验证你安装的 Django。

参考 数据库安装 了解如何通过 Django 安装数据库。

## 常见失误

- 若 `django-admin` 无论输入啥参数都只显示帮助信息，这可能是一个 Windos 的文件关联问题。检查 `PATH` 中是否不止一个用于运行 Python 脚本的环境变量。该问题通常在安装了多个 Python 版本时出现。
- 若你通过代理来连接互联网，运行命令 `pip install Django` 时可能会出现问题。在命令提示符中为代理配置如下环境变量:

  ```doscon
  ...\> set http_proxy=http://username:password@proxyserver:proxyport
  ...\> set https_proxy=https://username:password@proxyserver:proxyport
  ```
