slugify (Legacy Python 2)
Slugify (version 0.0.1) is a legacy Python library for generating URL-friendly 'slugs' from strings, primarily for Latin-based scripts. Inspired by Django's `slugify` template filter, it provides a Python function and a command-line tool. The library is considered abandoned and only officially supports Python 2.
Common errors
-
NameError: name 'unicode' is not defined
cause The `slugify` library (0.0.1) uses Python 2's `unicode()` built-in function, which was removed in Python 3.fixRun your code in a Python 2 environment. If you need Python 3 support, use `pip uninstall slugify` and then install a Python 3 compatible alternative, such as `pip install python-slugify`. -
from slugify import slugify AttributeError: module 'slugify' has no attribute 'slugify' (or similar unexpected behavior)
cause You likely have multiple packages providing a 'slugify' module (e.g., both 'slugify' and 'python-slugify') installed. The Python interpreter might be importing the wrong one due to package naming conflicts.fixIdentify which 'slugify' package you intend to use. If it's `python-slugify`, then `pip uninstall slugify` (the old package) and ensure only `python-slugify` is installed. -
slugify('你好世界') -> '---' (or similar unhelpful output for non-Latin characters)cause The `slugify` library (0.0.1) lacks proper Unicode transliteration and is designed for Latin-based scripts. It often replaces non-Latin characters with hyphens or removes them.fixFor international character support, switch to `python-slugify`. It offers more comprehensive Unicode handling, often leveraging libraries like `text-unidecode` for accurate transliteration.
Warnings
- breaking This `slugify` library (version 0.0.1) is only compatible with Python 2. Attempting to use it in a Python 3 environment will lead to runtime errors, most notably `NameError: name 'unicode' is not defined`.
- gotcha There is a significant name collision with the more actively maintained and widely used `python-slugify` library. Both packages attempt to expose the `slugify` function via `from slugify import slugify` which can lead to unpredictable behavior and importing the wrong library if both are installed.
- gotcha This library is designed for Latin-based scripts only and offers limited to no support for Unicode characters or internationalization. Non-Latin characters will often be stripped or replaced incorrectly, resulting in unusable slugs.
Install
-
pip install slugify
Imports
- slugify
from python_slugify import slugify
from slugify import slugify
Quickstart
import sys
# This library is for Python 2. Running in Python 3 will cause a NameError.
# To test, ensure you are in a Python 2 environment.
if sys.version_info.major == 2:
from slugify import slugify
text_to_slug = u"Hello World! This is a Test 123"
slugged_text = slugify(text_to_slug)
print("Original:", text_to_slug)
print("Slugged:", slugged_text)
else:
print("This quickstart requires Python 2. Current Python version is", sys.version)