Development

virtualenv 활용.

phpdoumi 2016. 5. 29. 22:02

phpdoumi@phpdoumi:~$ sudo apt-get install virtualenv

phpdoumi@phpdoumi:~$ mkdir aptana

phpdoumi@phpdoumi:~$ cd aptana

phpdoumi@phpdoumi:~/aptana$ mkdir testproject

phpdoumi@phpdoumi:~/aptana$ cd testproject

phpdoumi@phpdoumi:~/aptana/testproject$  virtualenv venv

phpdoumi@phpdoumi:~/aptana/testproject$ . venv/bin/activate

(venv) testproject$ pip install django

(venv)phpdoumi@phpdoumi:~/aptana/testproject$ django-admin startproject mysite <- 마지막 . 없음.

(venv)phpdoumi@phpdoumi:~/aptana/testproject$ tree -L 3

.

├── mysite

│   ├── manage.py

│   └── mysite

│       ├── __init__.py

│       ├── settings.py

│       ├── urls.py

│       └── wsgi.py

└── venv

    ├── bin

    │   ├── activate

    │   ├── activate.csh

    │   ├── activate.fish

    │   ├── activate_this.py

    │   ├── django-admin

    │   ├── django-admin.py

    │   ├── django-admin.pyc

    │   ├── easy_install

    │   ├── easy_install-2.7

    │   ├── pip

    │   ├── pip2

    │   ├── pip2.7

    │   ├── python -> python2

    │   ├── python2

    │   └── python2.7 -> python2

    ├── lib

    │   ├── python2.7

    │   └── python-wheels

    └── local

        ├── bin -> /home/phpdoumi/aptana/testproject/venv/bin

        └── lib -> /home/phpdoumi/aptana/testproject/venv/lib


10 directories, 20 files

(venv)phpdoumi@phpdoumi:~/aptana/testproject$ rm -Rf mysite

(venv)phpdoumi@phpdoumi:~/aptana/testproject$ django-admin startproject mysite . <- 마지막 . 있음.

(venv)phpdoumi@phpdoumi:~/aptana/testproject$ tree -L 3

.

├── manage.py

├── mysite

│   ├── __init__.py

│   ├── settings.py

│   ├── urls.py

│   └── wsgi.py

└── venv

    ├── bin

    │   ├── activate

    │   ├── activate.csh

    │   ├── activate.fish

    │   ├── activate_this.py

    │   ├── django-admin

    │   ├── django-admin.py

    │   ├── django-admin.pyc

    │   ├── easy_install

    │   ├── easy_install-2.7

    │   ├── pip

    │   ├── pip2

    │   ├── pip2.7

    │   ├── python -> python2

    │   ├── python2

    │   └── python2.7 -> python2

    ├── lib

    │   ├── python2.7

    │   └── python-wheels

    └── local

        ├── bin -> /home/phpdoumi/aptana/testproject/venv/bin

        └── lib -> /home/phpdoumi/aptana/testproject/venv/lib


9 directories, 20 files

(venv)phpdoumi@phpdoumi:~/aptana/testproject$ django-admin startapp polls
(venv)phpdoumi@phpdoumi:~/aptana/testproject$ tree -L 3
.
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── polls
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
└── venv
    ├── bin
    │   ├── activate
    │   ├── activate.csh
    │   ├── activate.fish
    │   ├── activate_this.py
    │   ├── django-admin
    │   ├── django-admin.py
    │   ├── django-admin.pyc
    │   ├── easy_install
    │   ├── easy_install-2.7
    │   ├── pip
    │   ├── pip2
    │   ├── pip2.7
    │   ├── python -> python2
    │   ├── python2
    │   └── python2.7 -> python2
    ├── lib
    │   ├── python2.7
    │   └── python-wheels
    └── local
        ├── bin -> /home/phpdoumi/aptana/testproject/venv/bin
        └── lib -> /home/phpdoumi/aptana/testproject/venv/lib

11 directories, 27 files
(venv)phpdoumi@phpdoumi:~/aptana/testproject$ deactivate
phpdoumi@phpdoumi:~/aptana/testproject$

[[ 실제 개발 환경에서 사용하기 ]]
// aptana directory가 python 개발 환경 root directory.
// mysite는 내가 개발할 project의 이름.
phpdoumi@phpdoumi:~/aptana$ mkdir mysite
phpdoumi@phpdoumi:~/aptana$ cd mysite
// virtualenv에서 직접적인 개발과 관련 없는 virtualenv 환경은 .venv로 감추기
phpdoumi@phpdoumi:~/aptana/mysite$ virtualenv .venv
Running virtualenv with interpreter /usr/bin/python2
New python executable in .venv/bin/python2
Also creating executable in .venv/bin/python
Installing setuptools, pip...done.
// 감춰진 .venv는 ls로도 보이지 않는다.
phpdoumi@phpdoumi:~/aptana/mysite$ ls -l
(.venv)phpdoumi@phpdoumi:~/aptana/mysite$ ls -al
total 12
drwxrwxr-x 3 phpdoumi phpdoumi 4096  5월 29 22:09 .
drwxrwxr-x 3 phpdoumi phpdoumi 4096  5월 29 22:09 ..
drwxrwxr-x 5 phpdoumi phpdoumi 4096  5월 29 22:09 .venv
// 감춰진 virtualenv 환경 activate
phpdoumi@phpdoumi:~/aptana/mysite$ . .venv/bin/activate
(.venv)phpdoumi@phpdoumi:~/aptana/mysite$ pip install django
Downloading/unpacking django
  Downloading Django-1.9.6-py2.py3-none-any.whl (6.6MB): 6.6MB downloaded
Installing collected packages: django
Successfully installed django
Cleaning up...
(.venv)phpdoumi@phpdoumi:~/aptana/mysite$ django-admin startproject mysite .
// .venv 가 보이지 않으니 가독성이 좋다.
(.venv)phpdoumi@phpdoumi:~/aptana/mysite$ tree -L 3
.
├── manage.py
└── mysite
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

1 directory, 5 files
// 전체가 궁금하면 -a 옵션 추가.
(.venv)phpdoumi@phpdoumi:~/aptana/mysite$ tree -L 3 -a
.
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── .venv
    ├── bin
    │   ├── activate
    │   ├── activate.csh
    │   ├── activate.fish
    │   ├── activate_this.py
    │   ├── django-admin
    │   ├── django-admin.py
    │   ├── django-admin.pyc
    │   ├── easy_install
    │   ├── easy_install-2.7
    │   ├── pip
    │   ├── pip2
    │   ├── pip2.7
    │   ├── python -> python2
    │   ├── python2
    │   └── python2.7 -> python2
    ├── lib
    │   ├── python2.7
    │   └── python-wheels
    └── local
        ├── bin -> /home/phpdoumi/aptana/mysite/.venv/bin
        └── lib -> /home/phpdoumi/aptana/mysite/.venv/lib

9 directories, 20 files
(.venv)phpdoumi@phpdoumi:~/aptana/mysite$ django-admin startapp polls
(.venv)phpdoumi@phpdoumi:~/aptana/mysite$ tree -L 3
.
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── polls
    ├── admin.py
    ├── apps.py
    ├── __init__.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py

3 directories, 12 files
(.venv)phpdoumi@phpdoumi:~/aptana/mysite$ pip list
argparse (1.2.1)
Django (1.9.6)
pip (1.5.6)
setuptools (18.4)
wsgiref (0.1.2)