3

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

Trent
  • 2,909
  • 1
  • 31
  • 46
  • Some questions: 1. What version of Django are you using? 2. What editor/IDE are you using to edit the files? 3. What operating system are you using? – solarissmoke Apr 21 '22 at 04:52
  • 1
    do you have your app in the INSTALLED_APPS? – Timur Apr 21 '22 at 13:57
  • ^ - also, do you have many apps in the `app` folder? typically django structure is to have each individual app folder on the same level as manage.py – Daniel Apr 21 '22 at 18:26
  • I've added details – Trent Apr 22 '22 at 04:27
  • Hi, Trent! How do you actually run the Django server? – SvenTUM Apr 22 '22 at 04:37
  • How your Django server is actually started should be in the _./compose/django/start-server.sh_ file. Also the _apps.py_ might be interesting. How are you actually doing changes, and how do you want them to take affect? – SvenTUM Apr 24 '22 at 14:37

2 Answers2

3

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.

Def Soudani
  • 1,151
  • 7
  • 17
Trent
  • 2,909
  • 1
  • 31
  • 46
  • 1
    See [What does from __future__ import absolute_import actually do?](https://stackoverflow.com/questions/33743880/what-does-from-future-import-absolute-import-actually-do) It seems you have some issue where your app probably shadows some builtin, you should give more information in your question so it is a [mre] currently it is not reproducible. – Abdul Aziz Barkat Apr 26 '22 at 04:04
  • You're right... Up until now I didn't have an MRE – Trent Apr 27 '22 at 03:08
0

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.

solarissmoke
  • 30,039
  • 14
  • 71
  • 73