Chapter 22: How to install Django
最后更新于:2022-04-01 04:48:23
This chapter provides more detailed instructions on installing Django than our simple setup in Chapter 1\. It covers the general setup, as well as additional instructions for getting Django up and running on Jython and Windows.
[TOC=3]
## Install Python
Being a Python Web framework, Django requires Python. It works with Python 2.7 or 3.3+.
Get the latest version of Python at [https://www.python.org/download/](https://www.python.org/download/) or with your operating systems package manager.
Django on Jython
If you use [Jython](http://www.jython.org/) (a Python implementation for the Java platform), youll need to follow a few additional steps which we will cover a bit later in this chapter.
Python on Windows
If you are just starting with Django and using Windows,the next section will be useful.
## Install Apache and mod_wsgi
If you want to use Django on a production site, use [Apache](http://httpd.apache.org/) with [mod_wsgi](http://code.google.com/p/modwsgi/). mod_wsgi can operate in one of two modes: an embedded mode and a daemon mode. In embedded mode, mod_wsgi is similar to mod_perl it embeds Python within Apache and loads Python code into memory when the server starts. Code stays in memory throughout the life of an Apache process, which leads to significant performance gains over other server arrangements. In daemon mode, mod_wsgi spawns an independent daemon process that handles requests. The daemon process can run as a different user than the Web server, possibly leading to improved security, and the daemon process can be restarted without restarting the entire Apache Web server, possibly making refreshing your codebase more seamless.
Chapter 13 has all the necessary information you need to configure mod_wsgi once you have it installed.
If you cant use mod_wsgi for some reason, fear not: Django supports many other deployment options. One is uWSGI ; it works very well with [nginx](http://nginx.org/). Additionally, Django follows the WSGI spec ([PEP 3333](https://www.python.org/dev/peps/pep-3333)), which allows it to run on a variety of server platforms.
## Get your database running
If you plan to use Djangos database API functionality, youll need to make sure a database server is running. Django supports many different database servers and is officially supported with [PostgreSQL](http://www.postgresql.org/),[MySQL](http://www.mysql.com/), [Oracle](http://www.oracle.com/) and [SQLite](http://www.sqlite.org/).
If you are developing a simple project or something you dont plan to deploy in a production environment, SQLite is generally the simplest option as it doesnt require running a separate server. However, SQLite has many differences from other databases, so if you are working on something substantial, its recommended to develop with the same database as you plan on using in production.
In addition to the officially supported databases, there are backends provided by 3rd parties that allow you to use other databases with Django.
In addition to a database backend, youll need to make sure your Python database bindings are installed.
* If youre using PostgreSQL, youll need the [postgresql_psycopg2](http://initd.org/psycopg/) package. You might want to refer to our PostgreSQL notes for further technical details specific to this database.
If youre on Windows, check out the unofficial [compiled Windows version](http://stickpeople.com/projects/python/win-psycopg/).
* If youre using MySQL, youll need the `MySQL-python` package, version 1.2.1p2 or higher. You will also want to read the database-specific notes for the MySQL backend .
* If youre using SQLite you might want to read the SQLite backend notes.
* If youre using Oracle, youll need a copy of [cx_Oracle](http://cx-oracle.sourceforge.net/), but please read the database-specific notes for the Oracle backend for important information regarding supported versions of both Oracle and `cx_Oracle`.
* If youre using an unofficial 3rd party backend, please consult the documentation provided for any additional requirements.
If you plan to use Djangos `manage.py migrate` command to automatically create database tables for your models (after first installing Django and creating a project), youll need to ensure that Django has permission to create and alter tables in the database youre using; if you plan to manually create the tables, you can simply grant Django `SELECT`, `INSERT`, `UPDATE` and `DELETE` permissions. After creating a database user with these permissions, youll specify the details in your projects settings file, see `DATABASES` for details.
If youre using Djangos testing framework to test database queries, Django will need permission to create a test database.
## Remove any old versions of Django
If you are upgrading your installation of Django from a previous version, you will need to uninstall the old Django version before installing the new version.
If you installed Django using [pip](http://www.pip-installer.org/) or `easy_install` previously, installing with [pip](http://www.pip-installer.org/) or `easy_install` again will automatically take care of the old version, so you dont need to do it yourself.
If you previously installed Django using `python setup.py install`, uninstalling is as simple as deleting the`django` directory from your Python `site-packages`. To find the directory you need to remove, you can run the following at your shell prompt (not the interactive Python prompt):
~~~
python -c "import sys; sys.path = sys.path[1:]; import django; print(django.__path__)"
~~~
## Install the Django code
Installation instructions are slightly different depending on whether youre installing a distribution-specific package, downloading the latest official release, or fetching the latest development version.
Its easy, no matter which way you choose.
### Installing an official release with `pip`
This is the recommended way to install Django.
1. Install [pip](http://www.pip-installer.org/). The easiest is to use the [standalone pip installer](http://www.pip-installer.org/en/latest/installing.html#install-pip). If your distribution already has `pip` installed, you might need to update it if its outdated. (If its outdated, youll know because installation wont work.)
2. (optional) Take a look at [virtualenv](http://www.virtualenv.org/) and [virtualenvwrapper](http://virtualenvwrapper.readthedocs.org/en/latest/). These tools provide isolated Python environments, which are more practical than installing packages systemwide. They also allow installing packages without administrator privileges. Its up to you to decide if you want to learn and use them.
3. If youre using Linux, Mac OS X or some other flavor of Unix, enter the command `sudo pip install Django` at the shell prompt. If youre using Windows, start a command shell with administrator privileges and run the command `pip install Django`. This will install Django in your Python installations `site-packages`directory.
If youre using a virtualenv, you dont need `sudo` or administrator privileges, and this will install Django in the virtualenvs `site-packages` directory.
### Installing an official release manually
1. Download the latest release from our [download page](https://www.djangoproject.com/download/).
2. Untar the downloaded file (e.g. `tar xzvf Django-X.Y.tar.gz`, where `X.Y` is the version number of the latest release). If youre using Windows, you can download the command-line tool [bsdtar](http://gnuwin32.sourceforge.net/packages/bsdtar.htm) to do this, or you can use a GUI-based tool such as [7-zip](http://www.7-zip.org/).
3. Change into the directory created in step 2 (e.g. `cd Django-X.Y`).
4. If youre using Linux, Mac OS X or some other flavor of Unix, enter the command `sudo python setup.pyinstall` at the shell prompt. If youre using Windows, start a command shell with administrator privileges and run the command `python setup.py install`. This will install Django in your Python installations `site-packages` directory.
Removing an old version
If you use this installation technique, it is particularly important that you remove any existing installations of Django first. Otherwise, you can end up with a broken installation that includes files from previous versions that have since been removed from Django.
### Installing a distribution-specific package
Check the distribution specific notes 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, youll want to pay close attention to [the development timeline](https://code.djangoproject.com/timeline), and youll want to keep an eye on the release notes for the upcoming release. This will help you stay on top of any new features you might want to use, as well as any changes youll 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 youd 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](http://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 Djangos main development branch (the trunk or master) like so:
~~~
git clone git://github.com/django/django.git django-trunk
~~~
This will create a directory `django-trunk` in your current directory.
3. Make sure that the Python interpreter can load Djangos code. The most convenient way to do this is via[pip](http://www.pip-installer.org/). Run the following command:
~~~
sudo pip install -e django-trunk/
~~~
(If using a [virtualenv](http://www.virtualenv.org/) you can omit `sudo`.)
This will make Djangos code importable, and will also make the `django-admin` utility command available. In other words, youre all set!
If you dont have [pip](http://www.pip-installer.org/) available, see the alternative instructions for [installing the development version without pip](http://masteringdjango.com/how-to-install-django/#id1).
Warning
Dont run `sudo python setup.py install`, because youve already carried out the equivalent actions in step 3.
When you want to update your copy of the Django source code, just run the command `git pull` from within the `django-trunk` directory. When you do this, Git will automatically download any changes.
### Installing the development version without pip
If you dont have [pip](http://www.pip-installer.org/), you can instead manually [modify Pythons search path](https://docs.python.org/install/index.html#modifying-python-s-search-path).
First follow steps 1 and 2 above, so that you have a `django-trunk` directory with a checkout of Djangos latest code in it. Then add a `.pth` file containing the full path to the `django-trunk` directory to your systems `site-packages` directory. For example, on a Unix-like system:
~~~
echo WORKING-DIR/django-trunk > SITE-PACKAGES-DIR/django.pth
~~~
In the above line, change `WORKING-DIR/django-trunk` to match the full path to your new `django-trunk`directory, and change `SITE-PACKAGES-DIR` to match the location of your systems `site-packages` directory.
The location of the `site-packages` directory depends on the operating system, and the location in which Python was installed. To find your systems `site-packages` location, execute the following:
~~~
python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
~~~
(Note that this should be run from a shell prompt, not a Python interactive prompt.)
Some Debian-based Linux distributions have separate `site-packages` directories for user-installed packages, such as when installing Django from a downloaded tarball. The command listed above will give you the systems `site-packages`, the users directory can be found in `/usr/local/lib/` instead of `/usr/lib/`.
Next you need to make the `django-admin.py` utility available in your shell PATH.
On Unix-like systems, create a symbolic link to the file `django-trunk/django/bin/django-admin` in a directory on your system path, such as `/usr/local/bin`. For example:
~~~
ln -s WORKING-DIR/django-trunk/django/bin/django-admin.py /usr/local/bin/
~~~
(In the above line, change WORKING-DIR to match the full path to your new `django-trunk` directory.)
This simply lets you type `django-admin.py` from within any directory, rather than having to qualify the command with the full path to the file.
On Windows systems, the same result can be achieved by copying the file `django-trunk/django/bin/django-admin.py` to somewhere on your system path, for example `C:\Python27\Scripts`.
Note that the rest of the documentation assumes this utility is installed as `django-admin`. Youll have to substitute `django-admin.py` if you use this method.
## How to install Django on Windows
This document will guide you through installing Python and Django for basic usage on Windows.
The steps in this guide have been tested with Windows 7, 8.1 and 10\. In other versions, the steps would be similar.
### Install Python
Django is a Python web framework, thus requiring Python to be installed on your machine.
To install Python on your machine go to [https://python.org/download/](https://python.org/download/), and download a Windows MSI installer for Python. Once downloaded, run the MSI installer and follow the on-screen instructions.
After installation, open the command prompt and check the Python version by executing `python --version`. If you encounter a problem, make sure you have set the `PATH` variable correctly. You might need to adjust your `PATH` environment variable to include paths to the Python executable and additional scripts. For example, if your Python is installed in `C:\Python34\`, the following paths need to be added to `PATH`:
~~~
C:\Python34\;C:\Python34\Scripts;
~~~
### Install Setuptools
To install Python packages on your computer, Setuptools is needed. Download the latest version of[Setuptools](https://pypi.python.org/pypi/setuptools) for your Python version and follow the installation instructions given there.
### Install PIP
[PIP](http://www.pip-installer.org/) is a package manager for Python that uses the [Python Package Index](https://pypi.python.org/) to install Python packages. PIP will later be used to install Django from PyPI. If youve installed Python 3.4, `pip` is included so you may skip this section.
Open a command prompt and execute `easy_install pip`. This will install `pip` on your system. This command will work if you have successfully installed Setuptools.
Alternatively, go to [http://www.pip-installer.org/en/latest/installing.html](http://www.pip-installer.org/en/latest/installing.html) for installing/upgrading instructions.
### Install Django
Django can be installed easily using `pip`.
In the command prompt, execute the following command: `pip install django`. This will download and install Django.
After the installation has completed, you can verify your Django installation by executing `django-admin --version` in the command prompt.
See `database-installation` for information on database installation with Django.
### Common pitfalls
* If `django-admin` only displays the help text no matter what arguments it is given, there is probably a problem with the file association in Windows. Check if there is more than one environment variable set for running Python scripts in `PATH`. This usually occurs when there is more than one Python version installed.
* If you are connecting to the internet behind a proxy, there might be problem in running the commands`easy_install pip` and `pip install django`. Set the environment variables for proxy configuration in the command prompt as follows:
~~~
set http_proxy=http://username:password@proxyserver:proxyport
set https_proxy=https://username:password@proxyserver:proxyport
~~~
## Running Django on Jython
[Jython](http://www.jython.org/) is an implementation of Python that runs on the Java platform (JVM). This document will get you up and running with Django on top of Jython.
### Installing Jython
Django works with Jython versions 2.7b2 and higher. See the [Jython](http://www.jython.org/) Web site for download and installation instructions.
### Creating a servlet container
If you just want to experiment with Django, skip ahead to the next section; Django includes a lightweight Web server you can use for testing, so you wont need to set up anything else until youre ready to deploy Django in production.
If you want to use Django on a production site, use a Java servlet container, such as [Apache Tomcat](http://tomcat.apache.org/). Full JavaEE applications servers such as [GlassFish](https://glassfish.java.net/) or [JBoss](http://www.jboss.org/) are also OK, if you need the extra features they include.
### Installing Django
The next step is to install Django itself. This is exactly the same as installing Django on standard Python, so see `removing-old-versions-of-django` and `install-django-code` for instructions.
### Installing Jython platform support libraries
The [django-jython](http://code.google.com/p/django-jython/) project contains database backends and management commands for Django/Jython development. Note that the builtin Django backends wont work on top of Jython.
To install it, follow the [installation instructions](https://pythonhosted.org/django-jython/quickstart.html#install) detailed on the project Web site. Also, read the [database backends](https://pythonhosted.org/django-jython/database-backends.html) documentation there.
### Differences with Django on Jython
At this point, Django on Jython should behave nearly identically to Django running on standard Python. However, are a few differences to keep in mind:
* Remember to use the `jython` command instead of `python`. The documentation uses `python` for consistency, but if youre using Jython youll want to mentally replace `python` with `jython` every time it occurs.
* Similarly, youll need to use the `JYTHONPATH` environment variable instead of `PYTHONPATH`.
* Any part of Django that requires [Pillow](http://pillow.readthedocs.org/en/latest/) will not work.