skip to Main Content

I have this weird issue where not all file edits are being picked up by the hot reloader for Django.

I have this structure:

/
  app/ ... apps in here.
  config/settings.py
  manage.py

Now, any changes to config/settings.py or manage.py will result in the django runserver reloading.

But any changes to files inside app/… don’t trigger a reload – I have to go and add a newline to manage.py and save (quite irritating).

Any ideas why this might be?

At first I thought it was a docker thing, and it was only picking up files in the base dir, but then changes to config/settings.py also trigger the reload, so clearly it can see deeper.

EDIT: Addition info

Django 3.2, PyCharm, MacOS – yes, all apps are in INSTALLED_APPS

I have another project that has the EXACT same structure and for some reason it works… I’m really stumped.

EDIT adding dc and dockerfile

FROM python:3.8-slim

ENV PYTHONUNBUFFERED 1
WORKDIR /app

COPY ./requirements /requirements
ARG pip_requirement_file

RUN apt-get update && apt-get install -y libjpeg62-turbo-dev zlib1g-dev gcc ca-certificates gcc postgresql-client sed xmlsec1 pax-utils && apt-get clean

RUN pip install --no-cache-dir -r /requirements/$pip_requirement_file 
    && find /usr/local 
    ( -type d -a -name test -o -name tests ) 
    -o ( -type f -a -name '*.pyc' -o -name '*.pyo' ) 
    -exec rm -rf '{}' +

# Copy requirements and install local one
RUN rm -rf /requirements

COPY ./compose/django/entrypoint.sh /entrypoint.sh
RUN sed -i 's/r//' /entrypoint.sh
RUN chmod +x /entrypoint.sh

COPY ./compose/django/start-server.sh /start-server.sh
RUN sed -i 's/r//' /start-server.sh
RUN chmod +x /start-server.sh

# Very specifically copy the files we want to avoid bloating the image.
COPY ./manage.py /app/

COPY ./app/ /app/app/
COPY ./admin_static/ /app/admin_static/
COPY ./config/ /app/config/
COPY ./database/ /app/database/

ENTRYPOINT ["/entrypoint.sh"]
CMD ["/start-server.sh"]

docker compose

services:
  django:
    container_name: django
    build:
      context: .
      dockerfile: ./compose/django/Dockerfile
      args:
        pip_requirement_file: local.txt
    depends_on:
      - postgres
    ports:
      - "8000:8000"
    links:
      - postgres:postgres
    volumes:
      - .:/app
    env_file: .env

2

Answers


  1. Chosen as BEST ANSWER

    I'd like to thank everyone for trying to help me solve this riddle that has been doing my head in.

    I decided to strip both projects (old and new) back to see why the old worked and the new doesn't.

    This is what was different.

    Inside config/init.py on the old project, this line exists:

    from __future__ import absolute_import, unicode_literals
    

    Inside the new project, this was missing.

    When I add this in, hot reloading starts working for all files inside app/* ?????

    It's working, but honestly, I have no idea why that would make a difference.


  2. I am not 100% sure what the issue is, but offering this answer with a couple of things you can try:

    1. There are reports that Pycharm will sometimes cause problems if it has been configured not to update the timestamps of files when saving them (there is a Preserve files timestamps setting to control this) which you could try toggling. An easy way to verify if this is the issue is to try editing a file with a different editor (or touch the file) and see if that triggers a reload – if it does then the issue is PyCharm.

      Note that the default StatReloader will not work if file timestamps don’t change.

    2. Try installing pywatchman and the Watchman service, as mention in the documentation, which provides a much more efficient way to watch for changes than StatReloader which simply polls all files for changes every second – if it’s a large project it may be that the StatReloader is just taking too long to spot changes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search