아래 내용은 가상환경을 만들고 거기서 개발하는거다.


의미는? 만약 특정 패키지나 라이브러리를 전체에 설치하고 싶지 않다면? 

이때 가상환경을 하나 만들고 거기에만 설치하게 할 수 있다.


아래 내용을 잘보고 따라해보라.

(사실 내가 따라한 내용은 http://kswa.codingstar.net:14705/html/installation.html#installation 였다)


설치하기

Flask는 Werkzeug 와 Jinja2 라이브러리에 의존적이다. Werkzeug는 웹어플리케이션과 다양한 서버 사이의 개발과 배포를 위한 표준 파이썬 인터페이스인 WSGI를 구현한 툴킷이다. Jinja2 는 HTML 템플릿을 렌더링 하는 템플릿엔진이다.

여러분은 어떻게 여러분의 컴퓨터에 Flaks와 관련된 모든 것들을 빠르게 얻을 수 있을까? 선택할 수 있는 방법은 여러가지가 있으나 가장 강력한 방법은 virtualenv을 사용하는 것이다. 그래서 먼저 virtualenv를 살펴보려 한다.

Flask 설치를 시작하기 위해 파이썬 2.5 또는 상위 버전을 사용할 것이다. 그래서 최신 파이썬 2.x버전이 설치되어 있는 것을 확인해라. 이 글을 쓰는 시점에 WSGI 명세서는 아직 파이썬 3을 위해 마무리되지 않았다. 그래서 Flask는 파이썬 3.x 버전을 지원할 수 없다.

virtualenv

virtualenv는 아마도 당신이 개발 과정에서 꼭 사용해야만 하는 툴일것이다. 만약 당신이 운영서버에 쉘(shell) 환경에서 접속해야 하고 다양한 파이썬실행 환경이 필요하다면 유용하게 사용할 수 있는 툴이다.

virtualenv는 어떤 문제를 해결해 줄 수 있는가? 만약 당신이 나처럼 Python을 많이 사용하고 있고 Flask 기반의 웹 어플리케이션과 같이 현재 사용하고 있는 것과 다른 파이썬 환경을 사용해야 한다면 virtualenv를 사용해 볼 수 있는 좋은 기회가 될 수 있다. 게다가 당신이 진행하고 있는 더 많은 프로젝트들이 서로 다른 Python 버전에서 작동해야 한다거나 혹은 서로 다른 버전의 Python 라이브러리들 에서 작동해야 한다면 어떨까? 현실을 직시해보자! 많은 라이브러리들은 종종 하위버전 호환성들을 깨뜨린다. 그리고 어떤 심각한 응용프로그램이라 하더라도 라이브러리와 의존관계가 없을 수는 없다. 그렇다면 두개 혹은 더 많은 당신의 프로젝트들의 의존관계의 충돌을 해결하기 위해서 무엇을 할것인가?

Virtualenv가 당신을 구해 줄것이다. virtualenv를 이용하여 Python을 아무런 문제 없이 각각의 프로젝트 환경에 맞게 다중설치가 가능하도록 해준다. 이방법은 실제로 독립된 버전의 Python 실행 환경에 단순히 복사하는 것이 아니라 서로 다른 프로젝트 환경이 독립적으로 실행 환경을 명백하게 가져갈 수 있도록 격리 시키는 방법이다. 이제 virtualenv가 어떻게 작동하는지 알아보자!

만약 당신이 Mac OS X 혹은 Linux 환경이라면, 다음의 두가지 명령어로 virtualenv를 작동시킬 수 있다:

$ sudo easy_install virtualenv

혹은 좀더 나은 방법으로:

$ sudo pip install virtualenv

위의 방법을 사용하면 당신의 시스템에 virtualenv가 설치 될걸이다. 아마도 당신의 시스템에서 사용하는 package manager를 사용할 수 도 있다. 만약 당신이 우분투를 사용한다면

$ sudo apt-get install python-virtualenv

만약 당신이 Windows 를 사용하고 있거나 easy_install 명령어를 가지고 있지 않다면, 명령어를 먼저 설치해야 한다. Windows에서의 pip 와 distribute 을 확인하여 설치 방법에 대한 자세한 정보를 확인할 수 있다. easy_install이 설치가 완료되면 위와 같은 방법으로 명령어를 실행하여 virtualenv를 설치할 수 있다. 다만, Windows 환경에서는 sudo 가 필요없다.

일단 virtualenv가 설치되었다면, 쉘을 실행시키고 자신만의 실행환경을 만들 수 있다. 나는 보통 프로젝트 폴더를 만들고 폴더안에서 venv 를 이용하여 다음과 같이 작업한다.

$ mkdir myproject
$ cd myproject
$ virtualenv venv
New python executable in venv/bin/python
Installing distribute............done.

이제, 당신이 해당 프로젝트에서 작업하고 싶을 때마다, 그에 해당하는 실행환경을 활성화 시킬 수 있다. OS X혹은 Linux 환경이라면 다음과 같이 실행한다.:

$ . venv/bin/activate

만약 당신이 Windows 사용자라면, 다음과 같이 실행한다.:

$ venv\scripts\activate

어떤 방식이든, 당신은 이제 virtualenv 를 사용할 수밖에 없을 것이다. (virtualenv이 활성화된 상태를 보여주기 위핸 당신의 쉘 프롬프트가 어떻게 변화 되는지 주목하라).

이제 당신은 다음의 명령어를 입력하여 활성화된 virtualenv 를 통해 Flask를 설치할 수 있다.:

$ pip install Flask

몇초동안만 기다리면 설치가 완료 된다.

시스템 전체에 적용하여 설치

이 방법이 가능하긴 하지만, 추천하지는 않겠다. 단지 루트(root)권한을 이용하여 pip 를 실행시키면 된다.:

$ sudo pip install Flask

(Windows 시스템에서는 , 위 명령어를 명령어 프롬프트에서 관리자(administrator) 권한으로 실행시키면 된다. 그리고 sudo 는 생략하자.)

위태로운 모험 하기

만약 당신이 가장 최신버전의 Flask를 이용하여 작업을 하고 싶다면, 두가지 방법이 있다.: pip 를 이용하여 작업환경으로 Flask의 개발중 버전을 가져오거나, git checkout을 사용할 수 있다. 어떤 방법을 사용하든, virtualenv의 사용을 추천한다.

새로운 virtualenv를 만들고 git checkout을 실행하여 개발모드의 Flask를 실행할 수 있다.:

$ git clone http://github.com/mitsuhiko/flask.git
Initialized empty Git repository in ~/dev/flask/.git/
$ cd flask
$ virtualenv venv --distribute
New python executable in venv/bin/python
Installing distribute............done.
$ . venv/bin/activate
$ python setup.py develop
...
Finished processing dependencies for Flask

이 방법을 통해 의존관계에 있는 것들을 가져오게 되고 git에 등록된 현재버전을 virtualenv안으로 가져오게 된다. 그다음에 git pull origin 을 통하여 최신 버전으로 업데이트가 가능하다.

git을 사용하지 않고 최신 버전을 가져오는 방법은 다음과 같다.:

$ mkdir flask
$ cd flask
$ virtualenv venv --distribute
$ . venv/bin/activate
New python executable in venv/bin/python
Installing distribute............done.
$ pip install Flask==dev
...
Finished processing dependencies for Flask==dev

Windows에서의 pip 와 distribute

Windows 환경에서는 easy_install 의 설치가 조금 복잡해 보인다. 하지만 그래도 그리 어렵지 않다. 가장 쉬운 방법은 distribute_setup.py 를 다운로드 받아서 실행하는 것이다. 다운로드 폴더를 열고 다운 받은 파일을 더블클릭한다.

다음으로, easy_install 명령어를 PATH 에 실행경로로 추가하고 Python 설치 디렉토리의 Scripts의 경로도 실행경로에 추가하여야 한다. 이렇게 하려면, 바탕화면이나 시작메뉴의 내컴퓨터 아이콘을 마우스 오른쪽 버튼으로 클릭하여 나오는 메뉴에서 “등록정보”를 선택하여 실행한다. 이제 해당 메뉴에서 “고급 시스템 설정”(Windows XP의 경우 “고급” 탭) 을 선택한다. 다음으로 “환경 변수”를 선택한 후 “PATH” 를 클릭하여 “시스템 변수” 항목에 Python 설치 경로와 Python의 Scripts 폴더이름을 실행경로로 등록한다. 주의할 점은 기존에 설정되어 있는 값과 세미콜론(;)으로 구분되어져야 한다는 점이다. 설치된 Python 버전이 2.7이고 기본 경로에 설치되었다면 다음과 같을 것이다.:

;C:\Python27\Scripts

마침내 당신은 해냈다! 이제 제대로 동작하는지 확인해 보자, 명령어 창을 열고 프롬프트에 easy_install. 만약 Windows Vista 혹은 Windows7 을 사용중이고 ‘User Account Control’ 이 활성화 되어 있다면 관리자 권한으로 진행하여야 한다.

이제 당신은 easy_install 를 손에 넣었다! pip 를 설치해 보자:

> easy_install pip


---------------------------------------------------------------------------------------------------------------



from http://hackercodex.com/guide/python-virtualenv-on-mac-osx-mountain-lion-10.8/

Python and Virtualenv on Mac OS X Mountain Lion 10.8

Installing Python and Virtualenv on Mac OS X 10.8 Mountain Lion can be done any number of ways. While there’s no best or perfect configuration, this tutorial will guide you through the process of configuring a stock Mountain Lion installation into a rockin’ Python development system.

First steps

This guide assumes that you have already installed Xcode and Homebrew. For details, please follow the steps in the Mountain Lion Configuration Guide.

Python

We are going to install the latest 2.7.x version of Python via Homebrew. Why bother, you ask, when Apple includes Python along with Mountain Lion? Here are some reasons:

  • Homebrew always has the most recent version (currently 2.7.5). The version bundled with OS X is well out-of-date at 2.7.2.
  • Apple has made significant changes to its bundled Python, potentially resulting in hidden bugs.
  • Homebrew’s Python includes the latest Python package management tools: pip and Setuptools

Along the same lines, Mountain Lion’s version of OpenSSL (0.9.8r) is from February of 2011, so we’re going to tell Homebrew to download the latest OpenSSL and compile Python with it.

Use the following command to install Python via Homebrew:

brew install python --with-brewed-openssl

You’ve already modified your PATH as mentioned in the Mountain Lion Configuration Guide, right? If not, please do so now.

Optionally, we can also install Python 3.x alongside Python 2.x:

brew install python3 --with-brewed-openssl

… which makes it easy to test your code on both Python 2.x and Python 3.x.

Pip

Let’s say you want to install a Python package, such as the fantastic virtualenv environment isolation tool. While nearly every Python-related article for Mac OS X tells the reader to install it via sudo easy_install virtualenv, here’s why I don’t do it that way:

  1. installs with root permissions
  2. installs into the system /Library
  3. pip is more actively developed than Setuptools’ easy_install
  4. using the tools provided by Homebrew yields a more reliable Python environment

As you might have guessed by now, we’re going to use the tools provided by Homebrew to install the Python packages that we want to be globally available. When installing via Homebrew Python’s pip, packages will be installed to/usr/local/lib/python2.7/site-packages, with binaries placed in /usr/local/bin.

Version control (optional)

The first thing I pip-install is Mercurial. Since I have Mercurial repositories that I push to both Bitbucket and GitHub, I installMercurial and hg-git on all my systems:

pip install Mercurial hg-git

At a minimum, you’ll need to add a few lines to your .hgrc file in order to use Mercurial:

vim ~/.hgrc

The following lines should get you started; just be sure to change the values to your name and email address, respectively:

[ui]
username = YOUR NAME <address@example.com>

To test whether Mercurial is configured and ready for use, run the following command:

hg debuginstall

If the last line in the response is “No problems detected”, then Mercurial has been installed and configured properly.

Virtualenv

Python packages installed via the steps above are global in the sense that they are available across all of your projects. That can be convenient at times, but it can also create problems. For example, sometimes one project needs the latest version of Django, while another needs Django 1.3 to retain compatibility with a critical third-party extension. This is one of many use cases that virtualenv was designed to solve. On my systems, only a handful of general purpose Python packages (such asMercurial and virtualenv) are globally available — every other package is confined to virtual environments.

With that explanation behind us, let’s install virtualenv:

pip install virtualenv

Create some directories to store our projects and virtual environments, respectively:

mkdir -p ~/Projects ~/Virtualenvs

We’ll then open the ~/.bashrc file…

vim ~/.bashrc

… and add some lines to it:

# pip should only run if there is a virtualenv currently activated
export PIP_REQUIRE_VIRTUALENV=true
# cache pip-installed packages to avoid re-downloading
export PIP_DOWNLOAD_CACHE=$HOME/.pip/cache

Let’s re-load our bash environment:

. ~/.bash_profile

Now we have virtualenv installed and ready to create new virtual environments, which we will store in ~/Virtualenvs. New virtual environments can be created via:

cd ~/Virtualenvs
virtualenv foobar

If you have both Python 2.x and 3.x and want to create a Python 3.x virtualenv:

virtualenv -p python3 foobar-py3

… which makes it easier to switch between Python 2.x and 3.x foobar environments.

Restricting pip to virtual environments

What happens if we think we are working in an active virtual environment, but there actually is no virtual environment active, and we install something via pip install foobar? Well, in that case the foobar package gets installed into our global site-packages, defeating the purpose of our virtual environment isolation.

In an effort to avoid mistakenly pip-installing a project-specific package into my global site-packages, I previously usedeasy_install for global packages and the virtualenv-bundled pip for installing packages into virtual environments. That accomplished the isolation objective, since pip was only available from within virtual environments, making it impossible for me to pip install foobar into my global site-packages by mistake. But easy_install has some deficiences, such as the inability to uninstall a package, and I found myself wanting to use pip for both global and virtualenv packages.

Thankfully, pip has a sparsely-documented setting that tells it to bail if there is no active virtual environment, which is exactly what I want. In fact, we’ve already set that above, via the export PIP_REQUIRE_VIRTUALENV=true directive. For example, let’s see what happens when we try to install a package in the absence of an activated virtual environment:

$ pip install markdown
Could not find an activated virtualenv (required).

Perfect! But once that option is set, how do we install or upgrade a global package? We can temporarily turn off this restriction by adding the following to your ~/.bashrc:

syspip(){
   PIP_REQUIRE_VIRTUALENV="" pip "$@"
}

If in the future we want to upgrade our global packages, the above function enables us to do so via:

syspip install --upgrade pip setuptools virtualenv

You could, of course, do the same via PIP_REQUIRE_VIRTUALENV="" pip install --upgrade foobar, but that’s much more cumbersome to type.

Creating virtual environments

Let’s create a virtual environment for Pelican, a Python-based static site generator:

cd ~/Virtualenvs
virtualenv pelican

Change to the new environment and activate it via:

cd pelican
. bin/activate

To install Pelican into the virtual environment, we’ll use pip:

pip install pelican markdown

For more information about virtual environments, read the virtualenv docs.

Dotfiles

These are obviously just the basic steps to getting a Python development environment configured. If you found this useful, you might to also check out my dotfiles (GitHub mirror), which I recently started rebuilding from scratch. I’m still early in the process of selectively adding one building block at a time, with more to be added soon.

Thoughts?

Have a question or suggestion? Leave a comment below!



'IT > python' 카테고리의 다른 글

Linter pylint is not installed and pip  (0) 2019.08.01
django and python in intellij  (0) 2013.02.27
mac os x 에서 파이썬 python 업그레이드 하기...  (1) 2013.02.27

+ Recent posts