이 번역은 완료되지 않았습니다. 이 문서를 번역해 주세요.

이제 Django가 무엇인지 알았으니, 윈도우, 리눅스(우분투), 맥 OS X에서 어떻게 Django 개발환경을 세팅하는지, 설치 후에는 어떻게 테스트하는지 살펴보겠습니다. 즉 이 문서를 통해서는 사용하고 있는 운영체제가 무엇인지와 상관없이 Django 어플리케이션 개발을 시작하기 위해 필요한 것들을 배우게 됩니다.

미리 필요한 것:

터미널 또는 커맨드 창을 열 수 있어야 합니다. 또, 자신이 사용하는 PC의 운영체제에 따라 PC에 소프트웨어 패키지를 설치할 수 있어야 합니다.

목표: Django가 컴퓨터에서 실행될 수 있도록 개발 환경을 세팅합니다.

Django 개발 환경 개요

Django는 개발 환경을 세팅하고 웹 어플리케이션을 개발하는 것이 매우 쉽습니다. 이 섹션에서는 개발 환경이 제공하는 것들과, 개발 환경 세팅 시 옵션사항을 알아봅니다. 또 우분투, 맥 OS X, 윈도우에서 Django 개발 환경을 설치하는 방법과 설치 후 테스트하는 방법을 설명합니다.

Django 개발 환경이란?

Django 개발 환경이란, Django를 로컬 컴퓨터에 설치하여 Django 어플리케이션을 개발, 실행, 테스트할 수 있는 환경을 말합니다. 로컬 컴퓨터는 자신이 어플리케이션을 개발하는 데 사용하는 컴퓨터입니다. 어플리케이션을 실제 배포하기 전에 로컬 컴퓨터 위에서 어플리케이션을 실행 및 테스트할 수 있습니다.

Django 자체가 제공하는 주요 툴에는 Django 프로젝트를 생성하고 작업하기 위한 파이썬 스크립트들과 심플한 개발용 웹 서버가 있습니다. 이 개발용 웹 서버로 우리는 로컬 컴퓨터에서 개발한 Django 어플리케이션을 같은 로컬 컴퓨터에서 테스트해볼 수 있습니다. 예를 들면, 자신의 PC에서 개발한 Django 웹 어플리케이션을 크롬 브라우저와 같은 웹 브라우저 상에서 실행하고 테스트해볼 수 있습니다.

앞서 설명한 것 외에도 Django 개발 환경은 여러 툴을 제공합니다. 코드 작성을 돕는 텍스트 에디터와 IDE, 소스코드의 버전을 안전하게 관리하기 위한 Git과 같은 소스 관리 도구와 같은 것들이 있습니다. 그러나 이것들은 여기서는 다루지 않습니다. 또 여기서는 미리 텍스트 에디터를 설치했다고 가정할 것입니다. 그러므로 텍스트 에디터를 아직 설치하지 않았다면 설치해주세요. 자주 사용하는 텍스트 에디터로는 Sublime Text 3, Gedit, Atom 등이 있습니다.)

What are the Django setup options?

Django is extremely flexible in terms of how and where it can be installed and configured. Django는 다음 사항이 가능합니다:

  • 여러 운영 체제에서 설치가 가능합니다.
  • Installed from source, from the Python Package Index (PyPi) and in many cases from the host computer's package manager application.
  • Configured to use one of several databases, which may also need to be separately installed and configured.
  • Run in the main system Python environment or within separate Python virtual environments.

Each of these options requires slightly different configuration and setup. The following subsections explain some of your choices. For the rest of the article we'll show you how to setup Django on a small number of operating systems, and that setup will be assumed throughout the rest of this module.

Note: Other possible installation options are covered in the official Django documentation. We link to the appropriate documents below.

What operating systems are supported?

Django web applications can be run on almost any machine that can run the Python 3 programming language: Windows, macOS X, Linux/Unix, Solaris, to name just a few. Almost any computer should have the necessary performance to run Django during development.

In this article we'll provide instructions for Windows, macOS X, and Linux/Unix.

Python은 어느 버전을 사용해야 할까?

가능한 최신 버전을 사용할 것을 권한다. - 이 글을 작성하는 시점에서는 Python 3.6버전이 최신이었다.

필요에 따라 Python 3.4 혹은 그 이후의 버전이 사용될 수 있다. (Python 3.4 support will be dropped in future releases).

Note: Python 2.7은 Django 2.0에서 사용할 수 없다. (Django 1.11.x series에서 마지막으로 Python 2.7을 지원했다).

Django는 어디서 다운로드 할 수 있나요?

There are three places to download

Django를 다운로드 할 수 있는 3가지 방법 :

  • pip tool을 사용해 파이썬 패키지 The Python Package Repository (PyPi)를 다운로드하기. 이 방법이 Django의 최신 버전을 받을 수 있는 최적의 방법이다.
  • Use a version from your computer's package manager. Distributions of Django that are bundled with operating systems offer a familiar installation mechanism. Note however that the packaged version may be quite old, and can only be installed into the system Python environment (which may not be what you want).
  • Install from source. You can get and install the latest bleeding-edge version of Django from source. This is not recommended for beginners, but is needed when you're ready to start contributing back to Django itself.

This article shows how to install Django from PyPi, in order to get the latest stable version.

어떤 Database를 써야 하나요?

Django supports four main databases (PostgreSQL, MySQL, Oracle and SQLite), and there are community libraries that provide varying levels of support for other popular SQL and NOSQL databases. We recommend that you select the same database for both production and development (although Django abstracts many of the database differences using its Object-Relational Mapper (ORM), there are still potential issues that are better to avoid).

For this article (and most of this module) we will be using the SQLite database, which stores its data in a file. SQLite is intended for use as a lightweight database and can’t support a high level of concurrency. It is however an excellent choice for applications that are primarily read-only.

Note: Django는 django-admin과 같이 웹사이트를 만드는 표준 툴을 사용하면 SQLite가 기본 값으로 설정되어 있습니다. 이는 추가적인 설정이 필요하지 않으므로 시작하기에 좋습니다. 

Installing system-wide or in a Python virtual environment?

When you install Python3 you get a single global environment that is shared by all Python3 code. While you can install whatever Python packages you like in the environment, you can only install one particular version of each package at a time.

Note: Python applications installed into the global environment can potentially conflict with each other (i.e. if they depend on different versions of the same package). 

만일 당신이 into the default/global environment then you will only be able to target one version of Django on the computer. This can be a problem if you want to create new websites (using the latest version of Django) while still maintaining websites that rely on older versions.

As a result, experienced Python/Django developers typically run Python apps within independent Python virtual environments. This enables multiple different Django environments on a single computer. The Django developer team itself recommends that you use Python virtual environments!

This module assumes that you've installed Django into a virtual environment, and we'll show you how below.

Python 3 설치

In order to use Django를 사용하기 위해서 당신은 Python을 당신의 운영체제에 설치해야 됩니다. 만일 당신이 Python 3를 사용하고 있다면 Python Package Index tool — pip3 —Django에서 사용되는 Python packages/libraries와 다른 Python apps을 관리하는(설치, 업데이트, 제거) tool 또한 설치해야 합니다.

이번 섹션에서는 현재 당신의 파이썬 버전이 무엇인지 확인하고, 필요에 따라 운영체제(Ubuntu Linux 16.04, macOS X, and Windows 10)별로 새로운 버전 설치 방법을 간단하게 설명합니다.

Note: Depending on your platform, you may also be able to install Python/pip from the operating system's own package manager or via other mechanisms. For most platforms you can download the required installation files from https://www.python.org/downloads/ and install them using the appropriate platform-specific method.

Ubuntu 16.04

Ubuntu Linux 16.04 LTS includes Python 3.5.2 by default. You can confirm this by running the following command in the bash terminal:

python3 -V
 Python 3.5.2

However the Python Package Index tool you'll need to install packages for Python 3 (including Django) is not available by default. You can install pip3 in the bash terminal using:

sudo apt-get install python3-pip

macOS X

macOS X "El Capitan" and other more recent versions do not include Python 3. You can confirm this by running the following commands in the bash terminal:

python3 -V
 -bash: python3: command not found

You can easily install Python 3 (along with the pip3 tool) from python.org:

  1. Download the required installer:
    1. Go to https://www.python.org/downloads/
    2. Select the Download Python 3.6.4 button (the exact minor version number may differ).
  2. Locate the file using Finder, and double-click the package file. Following the installation prompts.

You can now confirm successful installation by checking for the Python 3 as shown below:

python3 -V
 Python 3.6.4

You can similarly check that pip3 is installed by listing the available packages:

pip3 list

Windows 10

Windows doesn't include Python by default, but you can easily install it (along with the pip3 tool) from python.org:

  1. Download the required installer:
    1. Go to https://www.python.org/downloads/
    2. Select the Download Python 3.6.4 button (the exact minor version number may differ).
  2. Install Python by double-clicking on the downloaded file and following the installation prompts

You can then verify that Python 3 was installed by entering the following text into the command prompt:

py -3 -V 
 Python 3.6.4

The Windows installer incorporates pip3 (the Python package manager) by default. You can list installed packages as shown:

pip3 list

Note: The installer should set up everything you need for the above command to work. If however you get a message that Python cannot be found, you may need to add it to your system path.

Python 가상 환경에서 Django 사용하기

The libraries we'll use for creating our virtual environments are virtualenvwrapper (Linux and macOS X) and virtualenvwrapper-win (Windows), which in turn both use the virtualenv tool. The wrapper tools creates a consistent interface for managing interfaces on all platforms.

Installing the virtual environment software

Ubuntu virtual environment setup

After installing Python and pip you can install virtualenvwrapper (which includes virtualenv). The official installation guide can be found here, or follow the instructions below.

Install the tool using pip3:

sudo pip3 install virtualenvwrapper

Then add the following lines to the end of your shell startup file (this is a hidden file name .bashrc in your home directory). These set the location where the virtual environments should live, the location of your development project directories, and the location of the script installed with this package:

export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS=' -p /usr/bin/python3 '
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh

Then reload the startup file by running the following command in the terminal:

source ~/.bashrc

At this point you should see a bunch of scripts being run as shown below:

virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/postmkproject
...
virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/get_env_details

Now you can create a new virtual environment with the mkvirtualenv command.

macOS X virtual environment setup

Setting up virtualenvwrapper on macOS X is almost exactly the same as on Ubuntu (again, you can follow the instructions from either the offical installation guide or below.

Install virtualenvwrapper (and bundling virtualenv) using pip as shown.

sudo pip3 install virtualenvwrapper

Then add the following lines to the end of your shell startup file.

 

export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh

 

 

Note: The VIRTUALENVWRAPPER_PYTHON variable points to the normal installation location for Python3. If the virtualenv doesn't work when you test it, one thing to check is that Python is in the expected location (and then change the environment variable appropriately).

 


These are the same lines as for Ubuntu, but the startup file is the differently named hidden file .bash_profile in your home directory.

Note: If you can't find .bash-profile to edit in the finder, you can also open this in the terminal using nano.

The commands look something like this:

cd ~  # Navigate to my home directory
ls -la #List the content of the directory. YOu should see .bash_profile
nano .bash_profile # Open the file in the nano text editor, within the terminal
# Scroll to the end of the file, and copy in the lines above
# Use Ctrl+X to exit nano, Choose Y to save the file.

 

Then reload the startup file by making the following call in the terminal:

source ~/.bash_profile

At this point you may see a bunch of scripts being run (the same scripts as for the Ubuntu installation). You should now be able to create a new virtual environment with the mkvirtualenv command.

 

Windows 10 virtual environment setup

Installing virtualenvwrapper-win is even simpler than setting up virtualenvwrapper because you don't need to configure where the tool stores virtual environment information (there is a default value). All you need to do is run the folowing command in the command prompt:

pip3 install virtualenvwrapper-win

Now you can create a new virtual environment with the mkvirtualenv command

Creating a virtual environment

Once you've installed virtualenvwrapper or virtualenvwrapper-win then working with virtual environments is very similar on all platforms.

Now you can create a new virtual environment with the mkvirtualenv command. As this command runs you'll see the environment being set up (what you see is slightly platform specific). When the command completes the new virtual enviroment will be active — you can see this because the start of the prompt will be the name of the environment in brackets (as shown below).

$ mkvirtualenv my_django_environment

Running virtualenv with interpreter /usr/bin/python3
...
virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/t_env7/bin/get_env_details
(my_django_environment) ubuntu@ubuntu:~$

Now you're inside the virtual environment you can install Django and start developing.

Note: From now on in this article (and indeed the module) please assume that any commands are run within a Python virtual environment like the one we set up above.

Using a virtual environment

There are just a few other useful commands that you should know (there are more in the tool documentation, but these are the ones you'll use regularly):

  • deactivate — Exit out of the current Python virtual environment
  • workon — List available virtual environments
  • workon name_of_environment — Activate the specified Python virtual environment
  • rmvirtualenv name_of_environment — Remove the specified environment.

Django 설치하기

Once you've created a virtual environment, and called workon to enter it, you can use pip3 to install Django. 

pip3 install django

You can test that Django is installed by running the following command (this just tests that Python can find the Django module):

# Linux/macOS X
python3 -m django --version
 2.0

# Windows
py -3 -m django --version 
 2.0

Note: On Windows you launch Python 3 scripts by prefixing the command with py -3, while on Linux/macOS X, the command is python3.

Important: The rest of this module uses the Linux command for invoking Python 3 (python3) . If you're working on Windows simply replace this prefix with: py -3

설치한 것 확인하기

The above test works, but it isn't very much fun. A more interesting test is to create a skeleton project and see it working. To do this, first navigate in your command prompt/terminal to where you want to store your Django apps. Create a folder for your test site and navigate into it.

mkdir django_test
cd django_test

You can then create a new skeleton site called "mytestsite" using the django-admin tool as shown. After creating the site you can navigate into the folder where you will find the main script for managing projects, called manage.py.

django-admin startproject mytestsite
cd mytestsite

We can run the development web server from within this folder using manage.py and the runserver command, as shown.

$ python3 manage.py runserver 
Performing system checks...

System check identified no issues (0 silenced).

You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

December 29, 2017 - 03:03:47
Django version 2.0, using settings 'mytestsite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Note: The above command shows the Linux/macOS X command. You can ignore the warnings about  "14 unapplied migration(s)" at this point!

Once the server is running you can view the site by navigating to the following URL on your local web browser: http://127.0.0.1:8000/. You should see a site that looks like this:

Django Skeleton App Homepage

Summary

You now have a Django development environment up and running on your computer.

In the testing section you also briefly saw how we can create a new Django website using django-admin startproject, and run it in your browser using the development web server (python3 manage.py runserver). In the next article we expand on this process, building a simple but complete web application.

See also

In this module

 

문서 태그 및 공헌자

이 페이지의 공헌자: SYCHOI, kyueunQ, JieunKim
최종 변경자: SYCHOI,