-
Django 를 통한 웹 개발환경 구축하기Programming/Python 2015. 11. 2. 03:43
웹서비스 개발환경 구축하기 웹서비스 개발환경 구축하기
웹서비스 개발을 위해 다음의 패키지 설치가 필요하다. (2015/11/01 날짜의 최신버전이다.)
- Python 3.5
- Python Virtual Env
- Django 1.8.5
Python 설치하기
Mac 에서는 Python 의 설치도 하나의 패키지를 설치하는 것이다. Unix 기반이니 비슷하겠지, 뭐. OS X 에서 기본적으로는 패키지 관리 프로그램을 제공하지 않지만, Homebrew 라는 외부의 패키지 관리 프로그램이 있으니 이것부터 깔고, Homebrew 를 이용해서 Python 을 설치하자.
Homebrew 설치하기
Homebrew 는 Homebrew Homepage 에 가면 있는 Ruby Script 를 터미널에서 실행하면 자동으로 설치된다.
이후 상태를 확인하기 위해
brew doctor명령어를 사용한다.~ » brew doctor Please note that these warnings are just used to help the Homebrew maintainers with debugging if you file an issue. If everything you use Homebrew for is working fine: please don't worry and just ignore them. Thanks! Warning: The /usr/local is not writable. You should probably change the ownership and permissions of /usr/local back to your user account. sudo chown -R $(whoami) /usr/local Warning: Some directories in /usr/local/share/man aren't writable. This can happen if you "sudo make install" software that isn't managed by Homebrew. If a brew tries to add locale information to one of these directories, then the install will fail during the link step. You should probably `sudo chown -R $(whoami)` them: /usr/local/share/man/man5 /usr/local/share/man/man7 Warning: The /usr/local directory is not writable. Even if this directory was writable when you installed Homebrew, other software may change permissions on this directory. Some versions of the "InstantOn" component of Airfoil are known to do this. You should probably change the ownership and permissions of /usr/local back to your user account. sudo chown -R $(whoami):admin /usr/local Warning: Your Homebrew is outdated. You haven't updated for at least 24 hours. This is a long time in brewland! To update Homebrew, run `brew update`.Homebrew 에서 패키지를 관리할 때 /usr/local 아래의 권한이 필요하니 넣어야 한다는 것과 업데이트가 필요하다는 것 같다. 아래와 같이 조치했다.
~ » sudo chown -R donald:admin /usr/local ~ » brew update Updated Homebrew from 38df5e56 to e87cae8b. ==> New Formulae librest libsoundio ne otto xalan-c ykneomgr ==> Updated Formulae agda glib gst-plugins-good gtk+3 libunwind-headers purescript ykpers cairo gst-libav gst-plugins-ugly imagemagick macvim sbcl youtube-dl elasticsearch gst-plugins-bad gst-python libcroco nspr watchman yubico-piv-tool geoip gst-plugins-base gstreamer libressl ponyc winetricks ==> Deleted Formulae whereami ~ » brew doctor Your system is ready to brew.위에 나온
brew update는 Homebrew 자체를 업데이트하는 명령어이다.Python 설치하기
Homebrew 를 통해 패키지를 설치할 때는
brew install (package name)과 같이 명령어를 사용한다. 이미 설치한 이후에 적는 것이라 아래와 같이 추가적인 설치가 되지 않는다.현재 Python 최신 버전은 3.5.0 이다. 그리고 Homebrew 를 통해 설치 시에 PIP3 도 같이 설치된다.
~ » brew install python3 Warning: python3-3.5.0 already installedPython3? PIP3?
Mac 에는 Python 이 기본적으로 설치되어있고, Python 공식 홈페이지에서 이 것을 지우지 말라고 한다. 그런데 이 내장 Python 의 버전은 2.x 이다. 그래서 3.x 를 통시에 사용할 경우 혼동을 피하기 위해 Mac 에 Python 3.x 버전을 설치할 때는 실행명령어 뒤에 3 이라는 숫자가 붙는다. PIP 명령을 실행할 때도 3 을 붙여서 PIP3 라고 해야한다.
Python Virtual Env 설치/구축하기
위에서 설치한 PIP3 를 통해서 Python 의 가상 개발환경을 구축하는 VirtualEnv 를 설치해야 한다. 이 것을 설치하는 이유는 여러 버전의 Python 을 통해 여러 프로젝트를 개발한다던가, 각 모듈별 의존성이 다르기때문에 이 부분의 충돌이 없게 하기 위해 한다던가.. 하는 이유지만 그냥 별도로 내가 개발하는 환경 하나를 만든다고 생각하자. 그리고 얼마나 깔끔하냐, 분리해놓으면. 이 환경은 python, django, 각종 라이브러리를 내 컴퓨터에 깔린 것과 다르게 존재하는, 전혀 별도의 이 프로젝트만을 위한 구역이다. Python venv 문서 를 참고하자.
설치하는 명령어는 아래와 같다. 그리고 난 이미 설치하고 적는 것이라.. 중간 내용은 없다.
~/Documents/WebService/Invitation/bin(branch:master) » pip3 install virtualenv Password: The directory '/Users/donald/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. The directory '/Users/donald/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. Collecting virtualenv Downloading virtualenv-13.1.2-py2.py3-none-any.whl (1.7MB) 100% |████████████████████████████████| 1.7MB 393kB/s Installing collected packages: virtualenv Successfully installed virtualenv-13.1.2오류는 그냥 일단 무시하고 넘어갔다. 저 폴더들의 권한을 아무리 변경하고 소유권을 변경해봐도 위 오류는 변하지 않았다. 그리고 만약 권한때문에 안되면 앞에
sudo를 붙이면 된다.이번엔 virtualenvwrapper 패키지를 설치한다. 솔직히 뭐하는 건진 모르겠는데, 여튼 virtualenv, virtualenvwrapper 2가지 패키지를 설치해야 한다.
~/Documents/WebService/Invitation/bin(branch:master*) » pip3 install virtualenvwrapper Requirement already satisfied (use --upgrade to upgrade): virtualenvwrapper in /usr/local/lib/python3.5/site-packages Requirement already satisfied (use --upgrade to upgrade): virtualenv-clone in /usr/local/lib/python3.5/site-packages (from virtualenvwrapper) Requirement already satisfied (use --upgrade to upgrade): virtualenv in /usr/local/lib/python3.5/site-packages (from virtualenvwrapper) Requirement already satisfied (use --upgrade to upgrade): stevedore in /usr/local/lib/python3.5/site-packages (from virtualenvwrapper) Requirement already satisfied (use --upgrade to upgrade): argparse in /usr/local/lib/python3.5/site-packages (from stevedore->virtualenvwrapper) Requirement already satisfied (use --upgrade to upgrade): pbr>=1.6 in /usr/local/lib/python3.5/site-packages (from stevedore->virtualenvwrapper) Requirement already satisfied (use --upgrade to upgrade): six>=1.9.0 in /usr/local/lib/python3.5/site-packages (from stevedore->virtualenvwrapper)이제 마음속으로 프로젝트의 소스를 저장하고 각 프로젝트별로 개발환경을 구축할 폴더를 정하자. 나는
/Users/donald/Documents/WebService/폴더가 프로젝트들의 최상위 폴더이고, 이 아래Invitation,Pystagram이라는 2개의 프로젝트 소스를 구성할 것이다. 전자는 모바일 청첩장을 만들 개인 프로젝트를, 후자는 이 교육에서 진행하는 프로젝트를 진행할 소스들이다.내 쉘의 환경설정 파일을 열자. 나는 zshell 을 쓰기 때문에 ~/.zprofile 이 환경설정 파일이다. 그리고 그 맨 아래 아래와 같은 구문을 추가한다.
export WORKON_HOME=/Users/donald/Documents/WebService VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 source `which virtualenvwrapper.sh`WORKON_HOME 은 Python 의 개발 가상환경을 만들 때, 이 환경들의 상위폴더를 말한다. 위에서 프로젝트들의 최상위 폴더를 여기에 적어준다.
VIRTUALENVWRAPPER_PYTHON 은.. Mac 에서 이 설정 없이 돌리면 컴파일 시에 계속 내장 Python 인 2.x 를 사용해서 오류가 난다. 그래서 이렇게 강제적으로 Python 3.x 가 있는 경로를 지정해준다. 이 경로를 모르면 터미널에서
which python3을 쳐서 나오는 결과를 적어주면 된다.이제 가상환경을 만들 준비가 다 됐다. 아래 명령어를 통해 가상환경을 만든다.
mkvirtualenv pystagram -p `which python3`그러면 WORKON_HOME 경로 아래에 pystagram 이라는 폴더가 만들어지고, 이 안에 python3 의 실행파일들과 라이브러리가 추가되서 별도 개발 환경이 구축된다. 터미널에서는 앞에
(pystagram)이라고 표시가 떠서 내가 어떤 가상환경에 있는지 알 수 있다.가상환경에환경에 접속/접속해지 하는 방법은 아래와 같다.
~/Documents/WebService/Pystagram/bin(branch:master) » cd $WORKON_HOME/Pystagram/bin ~/Documents/WebService/Pystagram/bin(branch:master) » source activate (pystagram)------------------------------------------------------------ ~/Documents/WebService/Pystagram/bin(branch:master) » (pystagram)------------------------------------------------------------ ~/Documents/WebService/Pystagram/bin(branch:master) » deactivate ------------------------------------------------------------ ~/Documents/WebService/Pystagram/bin(branch:master) »해당 가상환경에 접속하려면 그 프로젝트 폴더의 bin 으로 가서
source activate를, 해지하려면 어디에서는deactivate를 치면 된다.Django 설치하기
Django 는 가상환경 내에서 설치하자. 가상환경에 접속한 뒤 아래 명령어를 실행한다.
(pystagram)------------------------------------------------------------ ~/Documents/WebService/Pystagram/bin(branch:master) » pip3 install django끝이다..
자! 이제 개발환경 구축 끝! 은 개뿔, 다음 시간에 Github 와 연동해야 끝이다. 내일 정리해야겠다.