diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py
index 3847252632..47a8f0e5d1 100644
a
|
b
|
def get_child_arguments():
|
222 | 222 | args = [sys.executable] + ['-W%s' % o for o in sys.warnoptions] |
223 | 223 | # __spec__ is set when the server was started with the `-m` option, |
224 | 224 | # see https://docs.python.org/3/reference/import.html#main-spec |
225 | | if __main__.__spec__ is not None and __main__.__spec__.parent: |
226 | | args += ['-m', __main__.__spec__.parent] |
227 | | args += sys.argv[1:] |
| 225 | if __main__.__spec__ is not None: |
| 226 | modspec = __main__.__spec__ |
| 227 | if modspec.name.split(".")[-1] == "__main__" and modspec.parent: |
| 228 | args += ['-m', modspec.parent] |
| 229 | args += sys.argv[1:] |
| 230 | else: |
| 231 | args += ['-m', modspec.name] |
| 232 | args += sys.argv[1:] |
| 233 | |
228 | 234 | elif not py_script.exists(): |
229 | 235 | # sys.argv[0] may not exist for several reasons on Windows. |
230 | 236 | # It may exist with a .exe extension or have a -script.py suffix. |
diff --git a/tests/utils_tests/test_autoreload.py b/tests/utils_tests/test_autoreload.py
index 3cb901af7d..14a546e55b 100644
a
|
b
|
from django.utils import autoreload
|
24 | 24 | from django.utils.autoreload import WatchmanUnavailable |
25 | 25 | |
26 | 26 | from .test_module import __main__ as test_main |
| 27 | from .test_module import main_module as test_main_module |
27 | 28 | from .utils import on_macos_with_hfs |
28 | 29 | |
29 | 30 | |
… |
… |
class TestChildArguments(SimpleTestCase):
|
182 | 183 | [sys.executable, '-m', 'utils_tests.test_module', 'runserver'], |
183 | 184 | ) |
184 | 185 | |
| 186 | @mock.patch.dict(sys.modules, {'__main__': test_main_module}) |
| 187 | @mock.patch('sys.argv', [test_main.__file__, 'runserver']) |
| 188 | @mock.patch('sys.warnoptions', []) |
| 189 | def test_run_as_non_django_module(self): |
| 190 | self.assertEqual( |
| 191 | autoreload.get_child_arguments(), |
| 192 | [sys.executable, '-m', 'utils_tests.test_module.main_module', 'runserver'], |
| 193 | ) |
| 194 | |
185 | 195 | @mock.patch('sys.argv', [__file__, 'runserver']) |
186 | 196 | @mock.patch('sys.warnoptions', ['error']) |
187 | 197 | def test_warnoptions(self): |