0

This question is almost the same as How to run cloned Django project? but because this project only includes mysite/settings.pyc, even the most basic commands fail in reference to Django settings.

$ python3 manage.py runserver
...
ImportError: bad magic number in 'mysite.settings': b'\x03\xf3\r\n'

$ django-admin runserver --settings=mysite.settings
...
ModuleNotFoundError: No module named 'mysite'

Trying other commands (e.g., python3 manage.py migrate) fail with reference to the bad magic number in mysite.settings.

The thread How to run a Django project with .pyc files without using source codes? seems to be exactly about this topic, but (1) the project is already structured as advised, and (2) that person answered their own question so they may tried that in the same dev environment.

This answer in the ImportError: bad magic number in 'time': b'\x03\xf3\r\n' in Django thread suggests that there are bytecode-only releases (yeah, I'm new to Python) so am I missing something? (The rest of the answers are not applicable in my case as deleting all pyc files would include deleting the settings.pyc as well... This post on pyc bytecode files was elucidating though.)

The DJANGO_SETTINGS_MODULE environment variable is also properly set up (1, 2), and in this case it seems that the mysite.settings module is simply missing.


Just in case this is relevant: I'm on NixOS so set up the Python3 environment with mach-nix.

toraritte
  • 6,300
  • 3
  • 46
  • 67
  • 1
    A .pyc file is specific to some version of Python (2.7, in your case) - it absolutely cannot work with any other version. The expectation is that the original .py file will still be around, so the .pyc can be regenerated whenever you upgrade Python. Perhaps you could decompile the .pyc, so that you have a usable .py file going forward (without comments of course, but that's better than not being able to run the code at all). Or simply create a `settings.py` from scratch - I don't think there's that much project-specific data in that file. – jasonharper Apr 24 '21 at 16:29
  • @jasonharper Please post this as answer - and I will add some Nix-specific parts and some comments to it if you don't mind. Still having issues, but de-compiling `settings.pyc` was the solution. Thanks! – toraritte Apr 24 '21 at 17:04

0 Answers0