Fix Docker Build Errors and Refactor the code in src/flask.py
Hi there 👋
I'm an online student currently working through the CS50 series — almost done with CS50x, CS50P, and CS50W. Out of curiosity, I decided to check out cs50/python-cs50, so I cloned the repo and tried running docker compose build, as shown in the README.
However, the build failed due to missing system dependencies required for installing the mysqlclient and psycopg2-binary Python packages. To fix this, I updated the Dockerfile to include the following packages:
-
build-essential -
pkg-config -
libpq-dev
After that, the image built successfully! 🎉
I then ran docker compose up — both MySQL and PostgreSQL servers started as expected. I used docker exec -it python-cs50 bash -l to get into the container, and ran the test commands listed in the README. Everything worked fine, except the final command, which tries to commit without an active transaction. I might open a separate issue for that later.
Also, while reading the docker build logs, I saw this warning:
WARN[0000] /home/shoaib-quantumcalc/python-cs50/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
So I removed the version line from docker-compose.yml, rebuilt the image, and the warning went away.
Another fix:
I updated the src/flask.py file to use importlib.util.find_spec("flask") instead of pkgutil.get_loader, as the latter is getting deprecated in Python 3.14, and I was afraid future students may not be able to use this library anymore.
I initially saw this error when I was running pytest in my project. It said:
../../python/lib/python3.12/site-packages/cs50/flask.py:39
/home/shoaib-quantumcalc/python/lib/python3.12/site-packages/cs50/flask.py:39: DeprecationWarning: 'pkgutil.get_loader' is deprecated and slated for removal in Python 3.14; use importlib.util.find_spec() instead
flask_loader = pkgutil.get_loader("flask")
So I went on and replaced it. Not just that, I've refactored the code quite a bit in flask.py! For instance, I've removed unused imports, and replaced
except:
with:
except Exception:
to align with best practices.
Thanks for this awesome library — it's been fun using it in my projects! It has made it relatively trivial to use it for managing databases compared to other libraries! Hope this PR is helpful. 🙌
P.S. I also took a course on Docker (via YouTube University 😄).