{"title":"如何安装 Django","version":"2.2","locale":"zh-hans","docname":"topics/install","url":"/zh-hans/2.2/topics/install/","canonical":"https://djangodocs.dev/zh-hans/2.2/topics/install/","summary":"本文档将帮助您使用 Django。Django 是一个 Python Web 框架。参见我应该使用哪个版本的 Python 来配合 Django? 获取更多细节。","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 Web 框架。参见 <a class=\"reference internal\" href=\"/zh-hans/2.2/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-django-on-jython admonition\" role=\"note\">\n<p class=\"admonition-title\">基于 Jython 的 Django</p>\n<p><a class=\"reference external\" href=\"http://www.jython.org/\">Jython</a> （一个针对 Java 平台的 Python 实现）和 Python 3 不兼容，所以Django 2.0 及以上的版本无法在 Jython 上运行。</p>\n</aside>\n<aside class=\"admonition-python-on-windows admonition\" role=\"note\">\n<p class=\"admonition-title\">Windows 上的 Python</p>\n<p>如果您刚刚开始学习 Django 并且使用 Windows，查看 <a class=\"reference internal\" href=\"/zh-hans/2.2/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\">mod_wsgi</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 —— 它在 Apache 中嵌入 Python，并在服务器启动时将 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/2.2/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/2.2/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://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/2.2/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=\"http://initd.org/psycopg/\">psycopg2</a> 包。相关详细信息请参阅 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/databases/#postgresql-notes\"><span class=\"std std-ref\">PostgreSQL 笔记</span></a>。</p></li>\n<li><p>如果你正在使用 MySQL，则需要一个像 <code class=\"docutils literal notranslate\">mysqlclient</code> 这样的 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/databases/#mysql-db-api-drivers\"><span class=\"std std-ref\">DB API driver</span></a> 。相关详细信息请参阅 <a class=\"reference internal\" href=\"/zh-hans/2.2/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/2.2/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-cx_Oracle/\">cx_Oracle</a> 的副本，但请阅读 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/databases/#oracle-notes\"><span class=\"std std-ref\">针对 Oracle 后端的笔记</span></a> 以获取有关 Oracle 和 <code class=\"docutils literal notranslate\">cx_Oracle</code> 支持的版本的详细信息。</p></li>\n<li><p>如果你使用的是非官方的第三方后端，请参阅提供的文档以了解任何其他要求。</p></li>\n</ul>\n<p>如果您打算使用 Django 的 <code class=\"docutils literal notranslate\">manage.py migrate</code> 命令为您的模型自动创建数据库表（在首次安装 Django 并创建项目之后），您需要确保 Django 有权在您正在使用的数据库中创建和更改表；如果你打算手动创建这些表，你可以只需授予 Django <code class=\"docutils literal notranslate\">SELECT</code>，<code class=\"docutils literal notranslate\">INSERT</code>，<code class=\"docutils literal notranslate\">UPDATE</code> 和 <code class=\"docutils literal notranslate\">DELETE</code> 权限。创建具有这些权限的数据库用户后，您要在项目的配置文件中指定详细信息，请参阅 <a class=\"reference internal\" href=\"/zh-hans/2.2/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\">DATABASES</code></a> 以获取详细信息。</p>\n<p>如果你正在使用 Django 的 <a class=\"reference internal\" href=\"/zh-hans/2.2/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<p>这很简单，不管你选哪种。</p>\n<section id=\"installing-an-official-release-with-pip\">\n<span id=\"installing-official-release\"></span><h3>通过 <code class=\"docutils literal notranslate\">pip</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/installing/#installing-with-get-pip-py\">独立 pip 安装器</a>。若你的系统早已安装 <code class=\"docutils literal notranslate\">pip</code>，你可能需要更新它，因为它可能过期了。如果它过期了，你会知道的，因为过期的用不了。</p></li>\n<li><p>瞅一眼 <a class=\"reference external\" href=\"https://virtualenv.pypa.io/\">virtualenv</a> 和 <a class=\"reference external\" href=\"https://virtualenvwrapper.readthedocs.io/en/latest/\">virtualenvwrapper</a>。这些工具能提供分离的 Python 环境，这将比直接在系统中安装包更实用。它们也允许在未经系统管理员许可的情况下安装包。<a class=\"reference internal\" href=\"/zh-hans/2.2/intro/contributing/\"><span class=\"doc\">贡献指南</span></a> 简单介绍了如何创建 virtualenv。</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>pip install 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> 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/2.2/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\" role=\"note\">\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/2.2/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\">git help</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 clone 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\">django</code> 目录。</p>\n</li>\n<li><p>确保 Python 解释器可以加载 Django 代码。最方便的方式是使用 <a class=\"reference external\" href=\"https://virtualenv.pypa.io/\">virtualenv</a>，<a class=\"reference external\" href=\"https://virtualenvwrapper.readthedocs.io/en/latest/\">virtualenvwrapper</a> 和 <a class=\"reference external\" href=\"https://pip.pypa.io/\">pip</a>。<a class=\"reference internal\" href=\"/zh-hans/2.2/intro/contributing/\"><span class=\"doc\">贡献指南</span></a> 简略介绍了如何创建 virtualenv。</p></li>\n<li><p>在配置并激活 virtualenv 后，运行以下命令：</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>pip install -e 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> pip install -e django\\\n</code></pre></div></div></div>\n<p>这会让 Django 的代码可导入，使得 <code class=\"docutils literal notranslate\">django-admin</code> 命令行工具可用。换句话说，大事可为。</p>\n</li>\n</ol>\n<p>当你要更新本地 Django 源码副本时，在 <code class=\"docutils literal notranslate\">django</code> 目录中运行 <code class=\"docutils literal notranslate\">git pull</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/2.2/topics/"}],"prev":{"docname":"topics/index","title":"使用 Django","url":"/zh-hans/2.2/topics/"},"next":{"docname":"topics/db/index","title":"模型和数据库","url":"/zh-hans/2.2/topics/db/"},"formats":{"html":"/zh-hans/2.2/topics/install/","markdown":"/zh-hans/2.2/topics/install.md","json":"/zh-hans/2.2/topics/install.json"},"source":"https://github.com/django/django/blob/stable/2.2.x/docs/topics/install.txt","official":"https://docs.djangoproject.com/zh-hans/2.2/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","pt-br","ko","es","el","pl"]}