How to use slugify in Python 3?

Question:

I’m trying to use slugify, which I installed using pip3 install slugify. However, in the interpreter, if I try to slugify the string 'hello' I see the following:

Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from slugify import slugify

In [2]: slugify('hello')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-a58110f37579> in <module>()
----> 1 slugify('hello')

/usr/local/lib/python3.5/dist-packages/slugify.py in slugify(string)
     22 
     23     return re.sub(r'[-s]+', '-',
---> 24             unicode(
     25                 re.sub(r'[^ws-]', '',
     26                     unicodedata.normalize('NFKD', string)

NameError: name 'unicode' is not defined

In [3]: slugify(u'hello')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-acc9f7b8d41e> in <module>()
----> 1 slugify(u'hello')

/usr/local/lib/python3.5/dist-packages/slugify.py in slugify(string)
     22 
     23     return re.sub(r'[-s]+', '-',
---> 24             unicode(
     25                 re.sub(r'[^ws-]', '',
     26                     unicodedata.normalize('NFKD', string)

NameError: name 'unicode' is not defined

By contrast, in Python 2 the latter does work:

Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
Type "copyright", "credits" or "license" for more information.

IPython 2.4.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from slugify import slugify

In [2]: slugify(u'hello')
Out[2]: u'hello'

How can I get this to work in Python 3?

Asked By: Kurt Peek

||

Answers:

The slugify package you installed isn’t built for python 3, it currently only supports python 2. And it is very unlikely it will get updated. One of the easiest way to tell is that throughout its source code, it used the python 2 keyword unicode in which it doesn’t exist in python 3.

You probably did:

pip install slugify

That was an outdated package, not the one you linked.

To install the slugify package you linked, https://pypi.python.org/pypi/python-slugify, it’s called python-slugify when you install it, it supports all recent python versions. And have way more functionalities.

pip install python-slugify

And import the same way as the other package:

from slugify import slugify

Note: YOU MUST DELETE THE ORIGINAL PACKAGE YOU INSTALLED, since they uses the same name.

Answered By: Taku

install python-slugify

pip install python-slugify

Answered By: Adnan Rizwee
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.