Search code examples
pythondjangopython-poetry

After installing Django with poetry it says "No module named django" in active venv


I'm playing with poetry because I'm thinking about switching from pip.

Following the basic usage examples, I'm doing the following:

$ poetry new poetry-demo

$ cd poetry-demo

$ poetry add django

$ django-admin #can't find it

$ poetry shell #or poetry $(poetry env info --path)/bin/activate

$ django-admin

Traceback (most recent call last):
  File "/Users/cjones/Library/Caches/pypoetry/virtualenvs/poetry-demo-Jq168aNm-py3.8/bin/django-admin", line 5, in <module>
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

Also tried in this order:

$ poetry new poetry-demo

$ cd poetry-demo

$ poetry shell #or poetry $(poetry env info --path)/bin/activate

$ poetry add django

$ django-admin
Traceback (most recent call last):
  File "/Users/cjones/Library/Caches/pypoetry/virtualenvs/poetry-demo-Jq168aNm-py3.8/bin/django-admin", line 5, in <module>
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

Check the pyproject.toml:

[tool.poetry]
name = "poetry-demo"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]

[tool.poetry.dependencies]
python = "^3.8"
Django = "^3.2.6"

[tool.poetry.dev-dependencies]
pytest = "^5.2"

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

Check poetry show:

asgiref        3.4.1  ASGI specs, helper code, and adapters
attrs          21.2.0 Classes Without Boilerplate
django         3.2.6  A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
more-itertools 8.8.0  More routines for operating on iterables, beyond itertools
packaging      21.0   Core utilities for Python packages
pluggy         0.13.1 plugin and hook calling mechanisms for python
py             1.10.0 library with cross-python path, ini-parsing, io, code, log facilities
pyparsing      2.4.7  Python parsing module
pytest         5.4.3  pytest: simple powerful testing with Python
pytz           2021.1 World timezone definitions, modern and historical
sqlparse       0.4.1  A non-validating SQL parser.
wcwidth        0.2.5  Measures the displayed width of unicode strings in a terminal

Check the path where it says it is not and there it is:

$ ls -l $(poetry env info --path)/bin
total 144
-rw-r--r--  1 cjones  staff  2197 Aug 27 09:39 activate
-rw-r--r--  1 cjones  staff  1489 Aug 27 09:39 activate.csh
-rw-r--r--  1 cjones  staff  3120 Aug 27 09:39 activate.fish
-rw-r--r--  1 cjones  staff  1751 Aug 27 09:39 activate.ps1
-rw-r--r--  1 cjones  staff  1199 Aug 27 09:39 activate_this.py
-rwxr-xr-x  1 cjones  staff   339 Aug 27 09:39 django-admin
-rwxr-xr-x  1 cjones  staff   729 Aug 27 09:39 django-admin.py
-rwxr-xr-x  1 cjones  staff   297 Aug 27 09:39 pip
-rwxr-xr-x  1 cjones  staff   297 Aug 27 09:39 pip-3.8
-rwxr-xr-x  1 cjones  staff   297 Aug 27 09:39 pip3
-rwxr-xr-x  1 cjones  staff   297 Aug 27 09:39 pip3.8
-rwxr-xr-x  1 cjones  staff   281 Aug 27 09:39 py.test
-rwxr-xr-x  1 cjones  staff   281 Aug 27 09:39 pytest
lrwxr-xr-x  1 cjones  staff   128 Aug 27 09:39 python -> /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/Resources/Python.app/Contents/MacOS/Python
lrwxr-xr-x  1 cjones  staff     6 Aug 27 09:39 python3 -> python
lrwxr-xr-x  1 cjones  staff     6 Aug 27 09:39 python3.8 -> python
-rwxr-xr-x  1 cjones  staff   292 Aug 27 09:39 sqlformat
-rwxr-xr-x  1 cjones  staff   284 Aug 27 09:39 wheel
-rwxr-xr-x  1 cjones  staff   284 Aug 27 09:39 wheel-3.8
-rwxr-xr-x  1 cjones  staff   284 Aug 27 09:39 wheel3
-rwxr-xr-x  1 cjones  staff   284 Aug 27 09:39 wheel3

Deactivate and reactive the venv. I've restarted the shell. Still just get:

Traceback (most recent call last):
  File "/Users/cjones/Library/Caches/pypoetry/virtualenvs/poetry-demo-Jq168aNm-py3.8/bin/django-admin", line 5, in <module>
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

Suggestions for what is up here and how to resolve it?


Solution

  • Should have read this more closely:

    https://python-poetry.org/docs/basic-usage/#using-poetry-run

    poetry run django-admin