본문 바로가기
Programming/Python

django(1)-프로젝트 만들기(with poetry)

by p4cho 2024. 3. 12.
728x90

poetry로 django 프로젝트를 작성하는 과정을 기록한다.

django 의 공식 문서를 참고했다.

Django

 

Django

The web framework for perfectionists with deadlines.

docs.djangoproject.com

실습 코드:

https://github.com/pacho-h/django-practice

 

GitHub - pacho-h/django-practice: django practice with poetry

django practice with poetry. Contribute to pacho-h/django-practice development by creating an account on GitHub.

github.com

 

set python version (pyenv)

# project 경로에서
$ pyenv local 3.12.1
$ cat .python-version 
3.12.1

poetry init

$ poetry init

This command will guide you through creating your pyproject.toml config.

# poetry로 관리할 python 프로젝트의 정보를 입력할 수 있다.
Package name [django-practice]:  
Version [0.1.0]:  
Description []:  
Author [pacho-h <pacho.h90@gmail.com>, n to skip]:  
License []:  
Compatible Python versions [^3.12]:  

# poetry init 시에 의존성 항목을 바로 추가할 수도 있고,
# no로 응답 후에 poetry add로 추가할 수도 있다.
# 여기서는 Django를 추가해보았다.
Would you like to define your main dependencies interactively? (yes/no) [yes] 
You can specify a package in the following forms:
  - A single name (requests): this will search for matches on PyPI
  - A name and a constraint (requests@^2.23.0)
  - A git url (git+https://github.com/python-poetry/poetry.git)
  - A git url with a revision (git+https://github.com/python-poetry/poetry.git#develop)
  - A file path (../my-package/my-package.whl)
  - A directory (../my-package/)
  - A url (<https://example.com/packages/my-package-0.1.0.tar.gz>)

Package to add or search for (leave blank to skip): django
Found 20 packages matching django
Showing the first 10 matches

Enter package # to add, or the complete package name if it is not listed []:
 [ 0] Django
 [ 1] django4django
 [ 2] django-503
 [ 3] django-filebrowser-django13
 [ 4] django-tracking-analyzer-django2
 [ 5] django-fsm-admin-django-4
 [ 6] django-suit-redactor-django2
 [ 7] django-debug-toolbar-django13
 [ 8] django-jchart-django3-uvm
 [ 9] django-totalsum-admin-django3
 [ 10] 
 > 0
Enter the version constraint to require (or leave blank to use the latest version): 
Using version ^5.0.3 for Django

Add a package (leave blank to skip): 

# 개발 시에 필요한 패키지들도 위와 같은 방식으로 바로 추가 가능.
Would you like to define your development dependencies interactively? (yes/no) [yes] 
Package to add or search for (leave blank to skip): ruff
Found 20 packages matching ruff
Showing the first 10 matches

Enter package # to add, or the complete package name if it is not listed []:
 [ 0] ruff
 [ 1] pytest-ruff
 [ 2] ruff2bitbucket
 [ 3] flake8-ruff
 [ 4] ruff-api
 [ 5] ruff-lsp
 [ 6] mdformat-ruff
 [ 7] autohooks-plugin-ruff
 [ 8] flake8-to-ruff
 [ 9] flake8-ruff-wrapper
 [ 10] 
 > 0
Enter the version constraint to require (or leave blank to use the latest version): 
Using version ^0.3.2 for ruff

Add a package (leave blank to skip): pytest
Found 20 packages matching pytest
Showing the first 10 matches

Enter package # to add, or the complete package name if it is not listed []:
 [ 0] pytest
 [ 1] pytest123
 [ 2] 131228_pytest_1
 [ 3] pytest-collect-pytest-interinfo
 [ 4] pytest-pingguo-pytest-plugin
 [ 5] pytest-forbid
 [ 6] pytest-symbols
 [ 7] pytest-circleci
 [ 8] pytest-zeebe
 [ 9] pytest-parallel
 [ 10] 
 > 0
Enter the version constraint to require (or leave blank to use the latest version): 
Using version ^8.1.1 for pytest

Add a package (leave blank to skip): pytest-watcher
Found 20 packages matching pytest-watcher
Showing the first 10 matches

Enter package # to add, or the complete package name if it is not listed []:
 [ 0] pytest-watcher
 [ 1] pytest-file-watcher
 [ 2] watcher
 [ 3] watcher-cli
 [ 4] nose-watcher
 [ 5] pug-watcher
 [ 6] databases-watcher
 [ 7] watcher-dashboard
 [ 8] price-watcher
 [ 9] youtube_watcher
 [ 10] 
 > 0
Enter the version constraint to require (or leave blank to use the latest version): 
Using version ^0.4.1 for pytest-watcher

Add a package (leave blank to skip): 

Generated file

[tool.poetry]
name = "django-practice"
version = "0.1.0"
description = ""
authors = ["pacho-h <pacho.h90@gmail.com>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.12"
Django = "^5.0.3"

[tool.poetry.group.dev.dependencies]
ruff = "^0.3.2"
pytest = "^8.1.1"
pytest-watcher = "^0.4.1"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

Do you confirm generation? (yes/no) [yes]
</pacho.h90@gmail.com></pacho.h90@gmail.com>

이제 pyproject.toml 파일에 project에 대한 설명과 의존성 목록이 작성되었다. 이 프로젝트를 위한 가상환경을 만들고 의존성 패키지들을 가상환경에 설치한다.

$ poetry update
Creating virtualenv django-practice-8iq0nlzA-py3.12 in /Users/han/Library/Caches/pypoetry/virtualenvs
Updating dependencies
Resolving dependencies... (0.7s)

Package operations: 10 installs, 0 updates, 0 removals

  - Installing asgiref (3.7.2)
  - Installing iniconfig (2.0.0)
  - Installing packaging (24.0)
  - Installing pluggy (1.4.0)
  - Installing sqlparse (0.4.4)
  - Installing watchdog (4.0.0)
  - Installing django (5.0.3)
  - Installing pytest (8.1.1)
  - Installing pytest-watcher (0.4.1)
  - Installing ruff (0.3.2)

Writing lock file
# 의존성 패키지 설치
$ poetry install
Installing dependencies from lock file

Package operations: 10 installs, 0 updates, 0 removals

  - Installing asgiref (3.7.2)
  - Installing iniconfig (2.0.0)
  - Installing packaging (24.0)
  - Installing pluggy (1.4.0)
  - Installing sqlparse (0.4.4)
  - Installing watchdog (4.0.0)
  - Installing django (5.0.3)
  - Installing pytest (8.1.1)
  - Installing pytest-watcher (0.4.1)
  - Installing ruff (0.3.2)

Installing the current project: django-practice (0.1.0)

poetry 셋팅이 완료된 후에 poetry의 가상환경을 활성화(activate)시키기

$ poetry shell
Spawning shell within /Users/han/Library/Caches/pypoetry/virtualenvs/django-practice-kYyV5fLA-py3.12
# 자동으로 아래의 명령어가 실행 된다.
$ emulate bash -c '. 
/Users/han/Library/Caches/pypoetry/virtualenvs/django-practice-kYyV5fLA-py3
.12/bin/activate'
(django-practice-py3.12) $ 
# poetry가 만든 virtualenv를 활성화(activate) 함
# 비활성화(deactivate) 할때는 exit 입력
(django-practice-py3.12) $ exit
$

django app 생성

django-admin command 로 django application을 생성한다.

(django-practice-py3.12) $ django-admin startproject django_practice .
(django-practice-py3.12) $ tree  
.
├── django_practice
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
├── poetry.lock
└── pyproject.toml

django 개발서버 실행

(django-practice-py3.12) $ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 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.
March 12, 2024 - 10:13:30
Django version 5.0.3, using settings 'django_practice.settings'
Starting development server at <http://127.0.0.1:8000/>
Quit the server with CONTROL-C.



끝.

728x90

'Programming > Python' 카테고리의 다른 글

django(2)-app 만들기  (0) 2024.03.18
pyenv  (0) 2024.03.12
pytest & pytest-watcher  (1) 2023.12.27
Poetry  (0) 2023.12.13
pipx  (0) 2023.12.13