Django - fly ssh console commands - module django not found

Using poetry, django, postgres.

Django app deploys. App is fully functional.

However, commands inside the ssh console that require supposedly installed poetry apps fail.

Specifically, this issue is regarding the “poetry run python manage.py createsuperuser” command, a command that creates a super user for the app.

$ fly ssh console
...
# cd app
# ls
Dockerfile  Ration_Django_Web_App  manage.py  media  poetry.lock  pyproject.toml  ration  readme.md  static  templatetags
poetry run python manage.py createsuperuser
Creating virtualenv ration-django-web-app-9TtSrW0h-py3.11 in /.cache/pypoetry/virtualenvs
Traceback (most recent call last):
  File "/app/manage.py", line 11, in main
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/app/manage.py", line 22, in <module>
    main()
  File "/app/manage.py", line 13, in main
    raise ImportError(
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
# python manage.py createsuperuser
Traceback (most recent call last):
  File "/app/manage.py", line 11, in main
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/app/manage.py", line 22, in <module>
    main()
  File "/app/manage.py", line 13, in main
    raise ImportError(
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
# which python
/usr/local/bin/python

I was able to navigate this issue by creating a user and modifying them to superuser through the postgres shell, but any explanation as to why this basic command isn’t working?

I don’t know enough to say for sure, but this looks like a path issue with Python. You may need to manually do something like:

PYTHONPATH=/path/to/installed/packages poetry run python manage.py createsuperuser

I had a similar problem , also with poetry and Django.

In my case, during build, poetry created a virtualenv in /root/.cache , but my ssh console was pointing to /.cache. I’m not sure yet why those are different.

poetry config —list

Should tell you where it’s looking for virtualenvs.

You can give poetry a different directory to find virtual environments with

poetry config virtualenvs.path <path>

Hope this helps