{"title":"如何安装 Django","version":"5.0","locale":"zh-hans","docname":"topics/install","url":"/zh-hans/5.0/topics/install/","canonical":"https://djangodocs.dev/zh-hans/5.0/topics/install/","summary":"本文档将帮助您使用 Django。 安装 Python Link to this heading # Django 是一个用于 Python 的网络框架。有关详细信息，请参阅 我应该使用哪个版本的 Python 来配合 Django? 。 可以通过 https://www.python.org/downloads/…","html":"<h1>如何安装 Django<a class=\"heading-anchor\" href=\"#how-to-install-django\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>本文档将帮助您使用 Django。</p>\n<section id=\"install-python\">\n<h2>安装 Python<a class=\"heading-anchor\" href=\"#install-python\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django 是一个用于 Python 的网络框架。有关详细信息，请参阅 <a class=\"reference internal\" href=\"/zh-hans/5.0/faq/install/#faq-python-version-support\"><span class=\"std std-ref\">我应该使用哪个版本的 Python 来配合 Django?</span></a>。</p>\n<p>可以通过 <a class=\"reference external\" href=\"https://www.python.org/downloads/\">https://www.python.org/downloads/</a> 或者操作系统的包管理工具获取最新版本的 Python。</p>\n<aside class=\"admonition-python-on-windows admonition\">\n<p class=\"admonition-title\">Windows 上的 Python</p>\n<p>如果您刚刚开始学习 Django 并且使用 Windows，查看 <a class=\"reference internal\" href=\"/zh-hans/5.0/howto/windows/\"><span class=\"doc\">如何在 Windows 上安装 Django</span></a>  可能对你有帮助。</p>\n</aside>\n</section>\n<section id=\"install-apache-and-mod-wsgi\">\n<h2>安装 Apache 和 <code class=\"docutils literal notranslate\"><span class=\"pre\">mod_wsgi</span></code><a class=\"heading-anchor\" href=\"#install-apache-and-mod-wsgi\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>如果您只是想试验 Django，请跳到下一部分；Django 包含一个可用于测试的轻量级 Web 服务器，因此在准备好在生产环境中部署 Django 之前，您不需要设置 Apache。</p>\n<p>如果您想在生产站点上使用 Django，请使用 <a class=\"reference external\" href=\"https://httpd.apache.org/\">Apache</a> 与 <a class=\"reference external\" href=\"https://modwsgi.readthedocs.io/en/develop/\">mod_wsgi</a>。mod_wsgi 有两种模式：嵌入模式和守护模式。在嵌入模式下，mod_wsgi 类似于 mod_perl -- 它将 Python 嵌入到 Apache 中，并在服务器启动时将 Python 代码加载到内存中。代码在整个 Apache 进程的生命周期内保持在内存中，这会比其他服务器配置方式带来显著的性能提升。在守护模式下，mod_wsgi 会生成一个独立的守护进程来处理请求。守护进程可以以与 Web 服务器不同的用户身份运行，从而可能提高安全性。守护进程可以在不重新启动整个 Apache Web 服务器的情况下重新启动，从而可能使代码库的刷新更加无缝。请参考 mod_wsgi 文档以确定哪种模式适合您的设置。确保已安装 Apache 并启用了 mod_wsgi 模块。Django 将与支持 mod_wsgi 的任何版本的 Apache 配合使用。</p>\n<p>若已安装 mod_wsgi 模块，请查看 <a class=\"reference internal\" href=\"/zh-hans/5.0/howto/deployment/wsgi/modwsgi/\"><span class=\"doc\">Django 如何利用 mod_wsgi 工作</span></a> 了解如何配置。</p>\n<p>如果由于某种原因你不能使用 mod_wsgi，请不要担心： Django 支持许多其他部署选项。一个是 <a class=\"reference internal\" href=\"/zh-hans/5.0/howto/deployment/wsgi/uwsgi/\"><span class=\"doc\">uWSGI</span></a> ；它和 <a class=\"reference external\" href=\"https://nginx.org/\">nginx</a> 配合使用很好。此外，Django 遵循 WSGI 规范（ <span class=\"target\" id=\"index-2\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-3333/\"><strong>PEP 3333</strong></a> ），允许它在各种服务器平台上运行。</p>\n</section>\n<section id=\"get-your-database-running\">\n<span id=\"database-installation\"></span><h2>运行你的数据库<a class=\"heading-anchor\" href=\"#get-your-database-running\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>如果你打算使用 Django 的数据库 API 功能，你需要确保一个数据库服务器正在运行。Django 支持许多不同的数据库服务器，官方支持 <a class=\"reference external\" href=\"https://www.postgresql.org/\">PostgreSQL</a>、<a class=\"reference external\" href=\"https://mariadb.org/\">MariaDB</a>、<a class=\"reference external\" href=\"https://www.mysql.com/\">MySQL</a>、<a class=\"reference external\" href=\"https://www.oracle.com/\">Oracle</a> 和 <a class=\"reference external\" href=\"https://www.sqlite.org/\">SQLite</a>。</p>\n<p>如果你正在开发一个小项目或不打算在生产环境中部署的东西，SQLite 通常是最好的选择，因为它不需要运行一个单独的服务器。然而，SQLite 与其他数据库有许多不同之处，所以如果你正在开发一些实质性的东西，建议使用你计划在生产中使用的同一数据库进行开发。</p>\n<p>除了官方支持的数据库，还有 <a class=\"reference internal\" href=\"/zh-hans/5.0/ref/databases/#third-party-notes\"><span class=\"std std-ref\">第三方提供的后端</span></a> 允许你在 Django 中使用其他数据库。</p>\n<p>除了数据库后端，你还要确保安装了 Python 数据库绑定。</p>\n<ul class=\"simple\">\n<li><p>如果您使用 PostgreSQL，您将需要 <a class=\"reference external\" href=\"https://www.psycopg.org/psycopg3/\">psycopg</a> 或 <a class=\"reference external\" href=\"https://www.psycopg.org/\">psycopg2</a> 包。有关更多详细信息，请参阅 <a class=\"reference internal\" href=\"/zh-hans/5.0/ref/databases/#postgresql-notes\"><span class=\"std std-ref\">PostgreSQL notes</span></a>。</p></li>\n<li><p>如果你正在使用 MySQL 或 MariaDB，则需要一个像 <code class=\"docutils literal notranslate\"><span class=\"pre\">mysqlclient</span></code> 一样的 <a class=\"reference internal\" href=\"/zh-hans/5.0/ref/databases/#mysql-db-api-drivers\"><span class=\"std std-ref\">DB API 驱动</span></a>。 详细信息参见 <a class=\"reference internal\" href=\"/zh-hans/5.0/ref/databases/#mysql-notes\"><span class=\"std std-ref\">MySQL 后端注意事项</span></a>。</p></li>\n<li><p>如果你正在使用 SQLite，则可能需要阅读 <a class=\"reference internal\" href=\"/zh-hans/5.0/ref/databases/#sqlite-notes\"><span class=\"std std-ref\">SQLite 后端笔记</span></a> 。</p></li>\n<li><p>如果你使用的是 Oracle，你需要安装 <a class=\"reference external\" href=\"https://oracle.github.io/python-oracledb/\">oracledb</a>，但请阅读 <a class=\"reference internal\" href=\"/zh-hans/5.0/ref/databases/#oracle-notes\"><span class=\"std std-ref\">Oracle 后端的说明</span></a> 以了解有关支持的 Oracle 版本和 <code class=\"docutils literal notranslate\"><span class=\"pre\">oracledb</span></code> 的详细信息。</p></li>\n<li><p>如果你使用的是非官方的第三方后端，请参阅提供的文档以了解任何其他要求。</p></li>\n</ul>\n<p>如果你打算使用 Django 的 <code class=\"docutils literal notranslate\"><span class=\"pre\">manage.py</span> <span class=\"pre\">migrate</span></code> 命令为你的模型自动创建数据库表（首先安装 Django 并创建项目后），你需要确保 Django 有权限在你使用的数据库中创建和修改表；如果你打算手动创建表，你可以授予 Django <code class=\"docutils literal notranslate\"><span class=\"pre\">SELECT</span></code>、<code class=\"docutils literal notranslate\"><span class=\"pre\">INSERT</span></code>、<code class=\"docutils literal notranslate\"><span class=\"pre\">UPDATE</span></code> 和 <code class=\"docutils literal notranslate\"><span class=\"pre\">DELETE</span></code> 权限。创建具有这些权限的数据库用户后，你将在项目的配置文件中指定详细信息，详细信息参见 <a class=\"reference internal\" href=\"/zh-hans/5.0/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a>。</p>\n<p>如果你正在使用 Django 的 <a class=\"reference internal\" href=\"/zh-hans/5.0/topics/testing/\"><span class=\"doc\">测试框架</span></a> 来测试数据库查询，Django 将需要创建测试数据库的权限。</p>\n</section>\n<section id=\"install-the-django-code\">\n<span id=\"install-django-code\"></span><h2>安装 Django 源码<a class=\"heading-anchor\" href=\"#install-the-django-code\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>安装过程可能会有些许差异，这取决于你是否在安装一个发行版——某个特定的版本，下载最新的正式发布包，或获取最新的开发版本。</p>\n<section id=\"installing-an-official-release-with-pip\">\n<span id=\"installing-official-release\"></span><h3>通过 <code class=\"docutils literal notranslate\"><span class=\"pre\">pip</span></code> 安装正式发布版本<a class=\"heading-anchor\" href=\"#installing-an-official-release-with-pip\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>以下是安装 Django 的推荐方式。</p>\n<ol class=\"arabic\">\n<li><p>安装 <a class=\"reference external\" href=\"https://pip.pypa.io/\">pip</a>。最简单的方式是使用 <a class=\"reference external\" href=\"https://pip.pypa.io/en/latest/installation/\">独立 pip 安装器</a>。若你的系统早已安装 <code class=\"docutils literal notranslate\"><span class=\"pre\">pip</span></code>，你可能需要更新它，因为它可能过期了。如果它过期了，你会知道的，因为过期的用不了。</p></li>\n<li><p>看一下 <a class=\"reference external\" href=\"https://docs.python.org/3/tutorial/venv.html\" title=\"(in Python v3.14)\"><span class=\"xref std std-doc\">venv</span></a>。这个工具提供了隔离的Python环境，比在系统内安装包更实用。它还允许在没有管理员权限的情况下安装包。<a class=\"reference internal\" href=\"/zh-hans/5.0/intro/contributing/\"><span class=\"doc\">贡献指南</span></a> 介绍了如何创建一个虚拟环境。</p></li>\n<li><p>在你已创建并激活一个虚拟环境后，输入以下命令：</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-0-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python<span class=\"w\"> </span>-m<span class=\"w\"> </span>pip<span class=\"w\"> </span>install<span class=\"w\"> </span>Django\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-0-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py -m pip install Django\n</code></pre></div></div></div>\n</li>\n</ol>\n</section>\n<section id=\"installing-a-distribution-specific-package\">\n<span id=\"installing-distribution-package\"></span><h3>安装特定发行版<a class=\"heading-anchor\" href=\"#installing-a-distribution-specific-package\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>查看 <a class=\"reference internal\" href=\"/zh-hans/5.0/misc/distributions/\"><span class=\"doc\">特定发行版注意事项</span></a> 以查看你的平台／发行版是否提供了官方的 Django 软件包／安装程序。 发行版提供的软件包通常将允许自动安装依赖项和且易于升级； 但是，这些软件包很少包含最新版本的 Django。</p>\n</section>\n<section id=\"installing-the-development-version\">\n<span id=\"installing-development-version\"></span><h3>安装开发版本<a class=\"heading-anchor\" href=\"#installing-the-development-version\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<aside class=\"admonition-tracking-django-development admonition\">\n<p class=\"admonition-title\">跟踪 Django 开发</p>\n<p>如果你决定使用 Django 的最新开发版，你需要关注 <a class=\"reference external\" href=\"https://code.djangoproject.com/timeline\">开发版时间轴</a> 和 <a class=\"reference internal\" href=\"/zh-hans/5.0/releases/#development-release-notes\"><span class=\"std std-ref\">即将到来的新特性的发布说明</span></a>。这将保证你能获取所有新特性和最新的代码。（对于稳定发布版，所有必要的修改都在发布说明中记录。）</p>\n</aside>\n<p>如果你希望偶尔能获取最新的补丁和改进，遵循以下说明：</p>\n<ol class=\"arabic\">\n<li><p>确保你已安装了 <a class=\"reference external\" href=\"https://git-scm.com/\">Git</a>，这样你就可以从 shell 运行对应命令。（在 shell 中输入 <code class=\"docutils literal notranslate\"><span class=\"pre\">git</span> <span class=\"pre\">help</span></code> 测试是否安装。）</p></li>\n<li><p>像这样检出 Django 的主开发分支：</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-1-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>git<span class=\"w\"> </span>clone<span class=\"w\"> </span>https://github.com/django/django.git\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-1-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> git clone https://github.com/django/django.git\n</code></pre></div></div></div>\n<p>这会在当前目录创建一个 <code class=\"docutils literal notranslate\"><span class=\"pre\">django</span></code> 目录。</p>\n</li>\n<li><p>确保 Python 解释器可以加载 Django 的代码。最方便的方法是使用虚拟环境和 <a class=\"reference external\" href=\"https://pip.pypa.io/\">pip</a>。 <a class=\"reference internal\" href=\"/zh-hans/5.0/intro/contributing/\"><span class=\"doc\">贡献指南</span></a> 简略介绍了如何创建虚拟环境。</p></li>\n<li><p>设置并激活虚拟环境后，运行以下命令：</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-2-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python<span class=\"w\"> </span>-m<span class=\"w\"> </span>pip<span class=\"w\"> </span>install<span class=\"w\"> </span>-e<span class=\"w\"> </span>django/\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-2-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py -m pip install -e django\\\n</code></pre></div></div></div>\n<p>这会让 Django 的代码可导入，使得 <code class=\"docutils literal notranslate\"><span class=\"pre\">django-admin</span></code> 命令行工具可用。换句话说，大事可为。</p>\n</li>\n</ol>\n<p>当你想更新你的 Django 源代码时，在 <code class=\"docutils literal notranslate\"><span class=\"pre\">django</span></code> 目录下运行 <code class=\"docutils literal notranslate\"><span class=\"pre\">git</span> <span class=\"pre\">pull</span></code> 命令。当你这样做的时候，Git 会下载所有变更。</p>\n</section>\n</section>","rootId":"how-to-install-django","toc":[{"title":"安装 Python","anchor":"install-python","children":[]},{"title":"安装 Apache 和 mod_wsgi","anchor":"install-apache-and-mod-wsgi","children":[]},{"title":"运行你的数据库","anchor":"get-your-database-running","children":[]},{"title":"安装 Django 源码","anchor":"install-the-django-code","children":[{"title":"通过 pip 安装正式发布版本","anchor":"installing-an-official-release-with-pip","children":[]},{"title":"安装特定发行版","anchor":"installing-a-distribution-specific-package","children":[]},{"title":"安装开发版本","anchor":"installing-the-development-version","children":[]}]}],"breadcrumbs":[{"docname":"topics/index","title":"使用 Django","url":"/zh-hans/5.0/topics/"}],"prev":{"docname":"topics/index","title":"使用 Django","url":"/zh-hans/5.0/topics/"},"next":{"docname":"topics/db/index","title":"模型和数据库","url":"/zh-hans/5.0/topics/db/"},"formats":{"html":"/zh-hans/5.0/topics/install/","markdown":"/zh-hans/5.0/topics/install.md","json":"/zh-hans/5.0/topics/install.json"},"source":"https://github.com/django/django/blob/stable/5.0.x/docs/topics/install.txt","official":"https://docs.djangoproject.com/zh-hans/5.0/topics/install/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}