When building a docker container with the --platform=linux/amd64 python:3.9 image, I encounter an error:
Traceback (most recent call last):
File "/var/www/myapp/manage.py", line 22, in <module>
main()
File "/var/www/myapp/manage.py", line 18, in main execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 345, in execute settings.INSTALLED_APPS
File "/usr/local/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__ self._setup(name)
File "/usr/local/lib/python3.9/site-packages/django/conf/__init__.py", line 69, in _setup self._wrapped = Settings(settings_module)
File "/usr/local/lib/python3.9/site-packages/django/conf/__init__.py", line 170, in __init__ mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/var/www/newapp/myapp/settings.py", line 2, in <module>
import environ
File "/usr/local/lib/python3.9/site-packages/environ.py", line 114
raise ValueError, "No frame marked with %s." % fname
^
SyntaxError: invalid syntax
I tried the recommended change to python3 -m pip install requirements.txt/pip3 install requirements.txt in the Dockerfile but nothing worked. The build of my container still exited with an error.
What I realised is that the django-environ package as included in the requirements.txt file was either not installed or the absence of a virtual-env in the container caused some issue and the environ package was just not detected by the application.
What I finally tried and it worked was installing the package directly in my environment with the RUN command in the Dockerfile.
RUN pip install django-environ.
This fixed the issue.