12

How to start a new Django project using poetry?

With virtualenv it is simple:

virtualenv -p python3 env_name --no-site-packages
source env_name/bin/activate
pip install django
django-admin.py startproject demo
pip freeze > requirements.txt

What will be equivalent to this using Poetry?

2

1 Answer 1

26

Create a new project folder and step in:

$ mkdir djangodemo
$ cd djangodemo

Create a basic pyproject.toml with django as dependency:

$ poetry init --no-interaction --dependency django

Create venv with all dependencies needed:

$ poetry install

Init your demo-project:

For Django versions after 4:

$ poetry run django-admin startproject djangodemo

For Django version less than 4:

$ poetry run django-admin.py startproject djangodemo
6
  • Why do we need to use poetry run ... in this case? I noticed that that works where django-admin ... alone does not, but I don't understand why. Mar 11, 2020 at 0:05
  • 4
    The django-admin.py needs to be run within the virtual environment to have access to all it's dependencies. poetry run starts a new shell, activates the virtual environment and runs the command. Afterwards it closes the shell again.
    – finswimmer
    Mar 11, 2020 at 14:23
  • 8
    you can also run poetry shell which will open shell with virtualenv and then you can run all your commands as usual.
    – an0o0nym
    Apr 11, 2020 at 12:52
  • Can I use virtualenvwrapper and run commands without poetry shell|run?
    – mirek
    Jan 13, 2021 at 13:20
  • 2
    If you are using django 4.0, it's django-admin, not django-admin.py!
    – bayman
    Dec 29, 2021 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.