Kaizen Today I Learned by Ville Säävuori

Posts tagged python

Installing Python With Shared Library on macOS

I needed a Python version with shared library in order to use PyO3 to run Python in Tauri to fix this error: “error: The auto-initialize feature is enabled, but your python installation only supports embedding the Python interpreter statically. If you are attempting to run tests, or a binary which is okay to link dynamically, install a Python distribution which ships with the Python shared library.

· Read the full article →

Using And Building Legacy Python 2 Libraries on macOS Monterey

Getting an old Python project working on a M1 Mac running macOS Monterey was somewhat of an battle. M1 and old Docker images I first fixed the Dockerfile to the point where it would run MySQL and Redis properly. The M1 architecture is still a bit of a hurdle with some Docker images, and especially the older ones.

· Read the full article →

Setting Up macOS for Web Development

Getting your development environment running and configured has become much easier over time but it’s still a hassle to get everything set up and configured from scratch. These notes are a continuously evolving task list for my personal setup. Applications Install Docker desktop, configure it as needed and remember to authenticate it.

· Read the full article →

Encrypted Data Archives With Django And Backblaze B2

I needed to implement user data export feature to a Django project. The static and user media is handled by Backblaze B2 cloud storage (similar to Amazon S3) which also supports server side encryption that allows your data to be encrypted at rest. Usually when working with filed in Django you want to use the native storage api and storage backends.

· Read the full article →

Migrating to a Custom Django User Model Mid-Project

Using a custom user model in a Django project is almost always a good idea. The documentation for adding one when starting a project is good and clear but switching to a custom model mid-project is not that easy. This blog post explains one way, and ticket #25313 has several helpful comments.

· Read the full article →

Pytest Cheat Sheet

Pytest Official Docs Running Fail fast / stop after first failure: pytest -x Run from module or directory: pytest dir/tests/footest.py, pytest dir/ Run specific markers: pytest -m marker (mark with @pytest.mark.mymarker) Drop to pdb on failure: pytest --pdb (set breakpoint w/ breakpoint()) Recreate database using pytest-django: --create-db Configuration pytest.

· Read the full article →

Python SSL Issues On macOS

I was bumping to SSL issues when creating new virtualenvs with pyenv. Found tons of similar issues but the main problem for me wasn’t a faulty pyenv or SSL installation but an old version of a specific pyenv Python version. As soon as I installed a new Python version, everything worked fine.

· Read the full article →

Casting Types With MyPy

Typing in Python is not yet very mature. This small example displays several issues: if request.user.is_authenticated: return JsonResponse( { "user": { "uid": request.user.uid, "username": request.user.username, "email": request.user.email, } } ) The original type of request.user is Union[AbstractBaseUser, AnonymousUser] (which itself is already wrong; the user is actually a Django model that inherits from AbstractBaseUser, not AbstractBaseUser).

· Read the full article →

Optional Dependencies With Poetry

Some Python dependencies require build tools which can be problematic when running inside Docker or CI environment. Often these packages are also needed only in development or production so having them as optional dependencies can be really useful. I just learned that Poetry can do this. First, install your dependency with --optional flag:

· Read the full article →

Fixing "Error loading MySQLdb module" on macOS 10.15 Catalina

MySql gets usually installed on MacOS by Brew, which installs a bunch of dylib libraries in /usr/local/opt/mysql/lib/. For some reason my Python installation is looking for wrong version of these files and I’ve yet to find a proper solution for matching up the dependencies. The error message looks like this:

· Read the full article →