{"id":6804,"library":"pylibmc","title":"pylibmc","description":"pylibmc is a high-performance Python client for memcached, implemented as a C Python extension wrapper around TangentOrg's `libmemcached` library. It offers an API intentionally designed to be close to `python-memcached` for easy drop-in replacement, leveraging features like configurable behaviors, binary protocol, data pickling, and compression. The current version is 1.6.3.","status":"active","version":"1.6.3","language":"en","source_language":"en","source_url":"https://github.com/lericson/pylibmc","tags":["memcached","caching","client","high-performance","C-extension"],"install":[{"cmd":"pip install pylibmc","lang":"bash","label":"Install pylibmc (requires system libmemcached)"}],"dependencies":[{"reason":"Core C library that pylibmc wraps; must be installed system-wide with development headers.","package":"libmemcached","optional":false},{"reason":"Required on Linux systems for building Python C extensions.","package":"python-dev / python3-dev","optional":false},{"reason":"Required for SASL authentication support with memcached servers.","package":"libsasl2-modules","optional":true},{"reason":"Optionally used for data compression.","package":"zlib","optional":true}],"imports":[{"symbol":"Client","correct":"from pylibmc import Client"},{"symbol":"ThreadMappedPool","correct":"from pylibmc import ThreadMappedPool"}],"quickstart":{"code":"import pylibmc\nimport os\n\n# Memcached server addresses, can be an environment variable (e.g., for MemCachier).\n# For local testing, use ['127.0.0.1:11211']. Multiple servers can be listed.\nservers = os.environ.get('MEMCACHIER_SERVERS', '127.0.0.1:11211').split(',')\nusername = os.environ.get('MEMCACHIER_USERNAME', '')\npassword = os.environ.get('MEMCACHIER_PASSWORD', '')\n\n# Initialize client. 'binary=True' enables the binary protocol, 'tcp_nodelay' improves performance.\n# SASL authentication is used if username and password are provided.\nif username and password:\n    mc = pylibmc.Client(servers, binary=True, behaviors={\"tcp_nodelay\": True}, username=username, password=password)\nelse:\n    mc = pylibmc.Client(servers, binary=True, behaviors={\"tcp_nodelay\": True})\n\n# Basic operations\nkey = \"my_test_key\"\nvalue = \"Hello from pylibmc!\"\n\n# Set a value with an expiry of 60 seconds\nsuccess_set = mc.set(key, value, time=60)\nprint(f\"Set '{key}' to '{value}': {success_set}\")\n\n# Get the value\nretrieved_value = mc.get(key)\nprint(f\"Retrieved '{key}': {retrieved_value}\")\n\n# Set multiple values\nmc.set_multi({\"key1\": \"value1\", \"key2\": 123})\nprint(f\"Retrieved 'key1': {mc.get('key1')}\")\n\n# Delete a key\nsuccess_delete = mc.delete(key)\nprint(f\"Deleted '{key}': {success_delete}\")\nprint(f\"'{key}' after deletion: {mc.get(key)}\")","lang":"python","description":"This quickstart demonstrates how to connect to a memcached server using `pylibmc.Client`, perform basic `set`, `get`, and `delete` operations, and handle optional SASL authentication using environment variables. It highlights setting expiry times and utilizing the binary protocol."},"warnings":[{"fix":"For interoperability with older clients or Python 2/3, explicitly set the `pickle_protocol` behavior (e.g., `mc.behaviors['pickle_protocol'] = 2`).","message":"Version 1.6.0 introduced partial incompatibility with older versions, specifically affecting interoperability with `python-memcached` due to a flag conflict. Unicode strings are now stored as UTF-8 instead of being pickled by default.","severity":"breaking","affected_versions":"1.6.0+"},{"fix":"Install `libmemcached-dev` (Debian/Ubuntu), `libmemcached-devel` (RHEL/CentOS), or `libmemcached` (Homebrew on macOS) using your system's package manager. For example: `sudo apt-get install libmemcached-dev python3-dev`.","message":"pylibmc is a C extension and requires the `libmemcached` C library (including development headers) to be installed on your system before `pip install pylibmc`. Without it, installation will fail with compilation errors like 'libmemcached/memcached.h: No such file or directory'.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Install `libsasl2-modules` (Debian/Ubuntu) or equivalent on your operating system.","message":"For SASL authentication, typically required by cloud memcached providers, the system-level `libsasl2-modules` (or equivalent) must be installed. Failure to do so will result in 'FAILED TO SEND AUTHENTICATION TO SERVER' errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use `pylibmc.ThreadMappedPool` or other pooling helpers provided by the library to manage client instances per thread.","message":"In multithreaded environments, a single `pylibmc.Client` object is not thread-safe and can lead to issues. For safe and performant concurrent access, pooling mechanisms are necessary.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For robust compatibility, especially if supporting older `pylibmc` versions, set behaviors using `mc.behaviors = {...}` after client creation, or ensure you are passing `behaviors` as a keyword argument if on a modern `pylibmc` version.","message":"The `behaviors` configuration for `pylibmc.Client` can be set either via the constructor's `behaviors` keyword argument (newer versions) or by modifying the `mc.behaviors` attribute after instantiation (older versions). Mixing or using only the attribute in older versions could lead to unexpected behavior.","severity":"gotcha","affected_versions":"Older versions (<1.0) primarily, but good practice for compatibility."},{"fix":"If encountering issues on Python 3.13+, consider checking the latest development branch for fixes or using a slightly older Python version until a new `pylibmc` release.","message":"There are reports of an exception being swallowed on startup under Python 3.13 that has a fix in the GitHub repository but has not been included in a release since 2022. This suggests potential compatibility issues with Python 3.13 and newer.","severity":"gotcha","affected_versions":"Potentially 3.13+"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}