pipenv with docker compose in PyCharm
Dec. 6, 2022, 4:33 a.m.
At the moment of page publishing, PyCharm does not support pipenv with docker (issue: https://youtrack.jetbrains.com/issue/PY-36414). What is the problem?
As you know, Pipenv install requirements in different paths like /home/vivazzi/.local/share/virtualenvs/vivivys-pxi3oUGW/bin/python
, where vivivys-pxi3oUG
path generate differently in devending different factors. In other hand, Pycharm requires to specify concrete path to interpreter:
Note in this window we can not see generated path to interpreter, so in next window (click by ellipsis) we do not know what we should input:
If you input path to global interpreter (something like: /usr/bin/python
or /usr/local/bin/python
), you can create access to remote interpreter but your app has virtual environment, so your web app will can not find packages, for example:
Traceback (most recent call last): File "/home/vivazzi/projects/other/vivivys/manage.py", line 8, in <module> from django.core.management import execute_from_command_line ModuleNotFoundError: No module named 'django'
There are some ways to resolve this problem. In your docker-compose.yml
you can:
- Use
--system
parameter:pip install --system
- Set the virtual environment to the root of the project to get the exact immutable path to the virtual environment.
In this article I will show second variant.
Create pipenv vitual environment in app folder
Pipenv allow install virtual environment to app folder if there is .venv
folder in app folder, so you can know path to python. It will be like /path_to_your_project/.venv/bin/python
.
Let's see the example! Look Dockerfile with next code:
FROM python:3.11-slim ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 RUN pip install pipenv EXPOSE 8000 WORKDIR /app COPY Pipfile.lock . RUN PIPENV_VENV_IN_PROJECT=1 pipenv sync COPY . .
Using PIPENV_VENV_IN_PROJECT=1
in RUN PIPENV_VENV_IN_PROJECT=1 pipenv sync
command allow pipenv to install the virtual environment at the root of the project i.e. /app/.venv/
. Accordingly, the path to the interpreter will be:
/app/.venv/bin/python
This is enough to use Pycharm for running your app and debug! Repeat the steps in the screenshots at the beginning of the article and enter this path to python - Pycharm should see the environment:
Although Pycharm swears that it allegedly did not find the path, it will find it if you click "Create".
But I want to show one feature of the work of PyCharm that due to it maybe can not run app though pycharm.
dockerfile and docker-compose.yml in remote interpreter setup in Pycharm
Let's create docker-compose.yml
:
version: '3.8' services: web: build: context: . dockerfile: Dockerfile command: pipenv run python manage.py runserver 0.0.0.0:8000 image: vivazzi/vivivys restart: unless-stopped volumes: - .:/code ports: - "0.0.0.0:9001:8000" environment: POSTGRES_NAME: ${POSTGRES_DB} POSTGRES_USER: ${POSTGRES_USER} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} depends_on: - db db: image: postgres hostname: postgres ports: - "7777:5432" environment: POSTGRES_DB: ${POSTGRES_DB} POSTGRES_USER: ${POSTGRES_USER} POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} volumes: - ./data/db:/var/lib/postgresql/data restart: unless-stopped
Note if you use ENTRYPOINT ["pipenv", "run","python","manage.py","runserver", "0.0.0.0:8000"]
in Dockerfile instead of command: pipenv run python manage.py runserver 0.0.0.0:8000
in docker-compose.yml, then PyCharm can not run your app (although if you run docker compose up in terminal you get running app normally, and due to it you can feel confuse). PyCharm will try run your app something like:
/app/.venv/bin/python /opt/project/manage.py runserver pipenv run python manage.py runserver 0.0.0.0:8000
As you see this is wrong command. Or maybe pipenv will create virtual environment in root folder instead of using generated virtual environment in .venv folder, so app lost path to installed packages.
In many articles by docker has warning that you must has ENTRYPOINT
or CMD
command to run your app. But if you use docker compose, you must refuse this commands in docker files and add command pipenv run python manage.py runserver 0.0.0.0:8000
to docker-compose.yml
. Then you will can use PyCharm for your development.
Comments: 0