Pycharm Setup - dfo-mar-odis/dto GitHub Wiki

Prerequisites

  • PyCharm Professional (Community Edition doesn't support Docker)
  • Docker Desktop installed and running
  • Project cloned from repository
  • .env file configured (if using production database)

Setting Up Docker Compose Interpreter

  1. Ensure Docker Desktop is running

If you don't see this icon, start docker from the windows start menu.
image

If you've already started docker desktop, you should see the docker desktop icon in your system tray.
image

Open up the docker desktop interface, in the bottom left corner docker should tell you if the engine is running.
image

  1. Open PyCharm Settings

    • File → Settings → Project: digital_twin_ocean → Python Interpreter

    Python Interpreter Settings

  2. Add Docker Compose Interpreter

    • Click "Add Interpreter" → "On Docker Compose"

    Add Interpreter

  3. Configure Docker Compose Target

    Docker Compose Configuration

    • Configuration file: Choose based on your needs:

      • Local development: docker-compose-local.yml
      • Production data access: docker-compose.yml
    • Project name: Use descriptive names:

      • dto-local for local development
      • dto-remote for production database access
    • Environment variables:

      • Copy the semicolon-separated string below
      • Paste into the Environment variables field
      • Do NOT use quotation marks around values

Environment Variable Strings

For Local Development:

DJANGO_LOG_FILE=error.log;DJANGO_LOG_LEVEL=DEBUG;DJANGO_SETTINGS_MODULE=config.django.local;PGDATABASE=postgres;PGPASSWORD=postgres;PGUSER=postgres;POSTGIS_ADDR=dto-postgis;POSTGIS_PORT=5432;POSTGRES_DB=postgres;POSTGRES_PASSWORD=postgres;POSTGRES_USER=postgres;SECRET_KEY=secret

⚠️ Important: Set environment variables before PyCharm downloads images and creates containers. Missing variables will cause setup to fail.

Creating a Pycharm Run/Debug Configuration

Why Use Run/Debug Configuration?

The easiest way to start the dto-django docker service through PyCharm Pro for development and loading data is to create a Django Run/Debug Configuration. This allows you to:

  • Start/stop services with one click
  • Debug Python code with breakpoints
  • Access Django shell and management commands
  • View logs in PyCharm console

Configuration Steps

image

To do this, click the three ellipse button and select edit under configuration.
image

In the run configuration dialog click the plus button to add a new configuration and select 'Django Server' from the options.
image

Give the run configuration a meaningful name so you will know if you are working with the local postgres database created by the docker-compose-local.yml file or the remote production database linked to though the docker-compose.yml file.

Select the "Remote Python" interpreter configured earlier using the respective (dto-local) and (dto-remote) for the Django server you're creating.
image

The required postgres and django environment variables were set when creating the interpreter, The environment variables for the run/debug configuration only have to tell the server what django configuration to use. The configurations are setup to override debugging variables and set proxy urls depending on the server running Django.
image

Running the configuration.

In Pycharm Pro, you can now use either the run or debug icons to start the configuration. This will spin up the docker containers, if they haven't already been started. You can check on and interact with the services from the "Services" view.
image

Open the view and navigate to the "dto-django" shell.
image

This will give you access to the running service where you can activate the terminal and gain access to inside the container.
image

Clicking the "Terminal" button opens the command line and you're ready to load data (see Data Loading Guide)

Verifying Setup

Once configured correctly, you should see:

  • ✅ Green dot next to interpreter in PyCharm settings
  • ✅ "Docker Compose: dto-local" (or dto-remote) shown as interpreter
  • ✅ No error messages in PyCharm Event Log

Common Terminal Commands

Once in the container terminal:


# Run migrations, this will have to be done on your local server the first time starting the application
python manage.py migrate

# Enter Django shell, the shell is where you can run Django enabled scripts that interact with the database
python manage.py shell

Troubleshooting

Docker Connection Issues

  • If PyCharm can't connect to Docker, restart Docker Desktop
  • On Windows, ensure Docker is using WSL2 backend
  • Check Windows Firewall isn't blocking Docker

Environment Variable Issues

  • If services fail to start, double-check all variables are set
  • Ensure no quotes around values in the environment string
  • Check PostgreSQL connection variables match your docker-compose file

Port Conflicts

  • If port 8000 is already in use, stop other Django servers
  • Check for other containers using: docker ps