python-poetry / poetry

Python packaging and dependency management made easy
https://python-poetry.org
MIT License
31.71k stars 2.27k forks source link

Error running via docker-compose #7690

Closed karambaq closed 7 months ago

karambaq commented 1 year ago

Issue

I'm trying to run it from docker, here is my dockerfile:

dockerfile

```docker FROM python:3.9 ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 RUN apt-get update \ ... ARG UID ARG GID RUN groupadd -g $GID -o django RUN useradd -u $UID -g $GID -o -s /bin/bash -m django RUN mkdir -p /app/code /app/requirements && chown -R django:django /app COPY ./_compose/app/scripts/start-django / RUN chmod +x /start-django # COPY ./requirements /app/requirements # RUN pip install -r /app/requirements/local.txt COPY poetry.lock pyproject.toml ./ RUN pip3 install poetry RUN poetry install --only main USER django WORKDIR /app/code ```

And this is my script to start django:

#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset

echo $(poetry show)
echo $(pwd)
echo $(ls -a)
echo $(poetry --version)
echo $(poetry env info)

poetry run python manage.py runserver 0.0.0.0:8000

Output of the script when I'm trying to start it:

...
 django (!) 3.2.15 A high-level Python Web framework t... django-admin-sortable2 (!)
...

/app/code
Poetry (version 1.4.1)
m-django-app-1           | Virtualenv Python: 3.9.16 Implementation: CPython
Path: /home/django/.cache/pypoetry/virtualenvs/m-django-4jK_B8RX-py3.9 
Executable: /home/django/.cache/pypoetry/virtualenvs/m-django-4jK_B8RX-py3.9/bin/python 
Valid: True System
Platform: linux OS: posix 
Python: 3.9.16 
Path: /usr/local 
Executable: /usr/local/bin/python3.9

Error:

Traceback (most recent call last):
m-django-app-1           |   File "/app/code/manage.py", line 11, in main
m-django-app-1           |     from django.core.management import execute_from_command_line
m-django-app-1           | ModuleNotFoundError: No module named 'django'

Traceback (most recent call last):
m-django-app-1           |   File "/app/code/manage.py", line 22, in <module>
m-django-app-1           |     main()
m-django-app-1           |   File "/app/code/manage.py", line 13, in main
m-django-app-1           |     raise ImportError(
m-django-app-1           | 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?
dimbleby commented 1 year ago

I don't know what's going on here but I'd bet against it being a poetry bug. If poetry run simply didn't work then there would have been more reports than just this.

Perhaps you have an unhelpful shebang in your manage.py? But I am just guessing, you will have to work out for yourself what is going on. Recommend that "it's probably not a poetry bug" is a reasonable prior for you to adopt in that investigation.

DavidBord commented 1 year ago

@karambaq, by default poetry creates a virtualenv so you should either use the virtualenv is creates or disable virtualenv creation with something like poetry config virtualenvs.create false --local. You can read about it more here. Hence, the reason why your app cannot find the django installation is because your app is using the container default interpreter where poetry creates a virtualenv based on the container default interpreter and installs the packages there.

abn commented 7 months ago

This issue cannot be reproduced, and it would seem this is an issue specific to the project being developed.

Here is an example of a working example of using a Django project with Poetry. See logs below.

podman run --rm -i -p 8000:8000 --entrypoint bash docker.io/python:3.12 <<EOF
set -xe
python -m pip install --disable-pip-version-check -q poetry

poetry config virtualenvs.in-project true
poetry config virtualenvs.options.no-pip true
poetry config virtualenvs.options.no-setuptools true

poetry new demo
pushd demo
poetry add Django

poetry run python -m django --version
poetry run django-admin startproject mysite
poetry run python mysite/manage.py migrate

# this is since we are running without a tty
export PYTHONUNBUFFERED=1
poetry run python mysite/manage.py runserver
EOF
console.log

```console + python -m pip install --disable-pip-version-check -q poetry WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv + poetry config virtualenvs.in-project true + poetry config virtualenvs.options.no-pip true + poetry config virtualenvs.options.no-setuptools true + poetry new demo Created package demo in demo + pushd demo /demo / + poetry add Django Creating virtualenv demo in /demo/.venv Using version ^5.0.3 for django Updating dependencies Resolving dependencies... Package operations: 3 installs, 0 updates, 0 removals - Installing asgiref (3.8.1) - Installing sqlparse (0.4.4) - Installing django (5.0.3) Writing lock file + poetry run python -m django --version 5.0.3 + poetry run django-admin startproject mysite + poetry run python mysite/manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying admin.0003_logentry_add_action_flag_choices... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying auth.0009_alter_user_last_name_max_length... OK Applying auth.0010_alter_group_name_max_length... OK Applying auth.0011_update_proxy_permissions... OK Applying auth.0012_alter_user_first_name_max_length... OK Applying sessions.0001_initial... OK + export PYTHONUNBUFFERED=1 + PYTHONUNBUFFERED=1 + poetry run python mysite/manage.py runserver Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). March 23, 2024 - 00:07:30 Django version 5.0.3, using settings 'mysite.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. ```

github-actions[bot] commented 6 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.