Edward Ned Harvey
Edward Ned Harvey

Reputation: 7011

How to search for pip package containing a module

If you inherited some python code that has include's but you don't know what packages were previously installed in order to satisfy those include's, how can you search for pip packages that contain that module?

In this case, I'm trying to find what pip package(s) to install, in order to satisfy:

from django.utils.encoding import smart_str, smart_unicode

By installing Django, I was able to satisfy smart_str but I still don't have smart_unicode.

pip search doesn't help, because it just searches for packages whose names match your search. It doesn't tell you which packages contain some particular module or other named object, like smart_unicode. There are about a million packages with some variation of django in their name.

After figuring out the answer once, I will be writing it into a requirements.txt file to prevent this problem in the future with this particular python file.

How can you search for what pip package contains a module?

Upvotes: 1

Views: 267

Answers (2)

gabhijit
gabhijit

Reputation: 3585

Most likely you are using Python 3 and Django 2. Likely what has happened the code you inherited from was written for a version of Django that supported Python 2 as well (likely some version 1.XX) and they failed to provide right django version as a requirement. So you will have to explicitly specify a specific version of django 1.xx while doing a pip install so something like pip install django=1.11 would help.

Here's a quick snippet from a django 1.11 code that I had handy


if six.PY3:
    smart_str = smart_text
    force_str = force_text
else:
    smart_str = smart_bytes
    force_str = force_bytes
    # backwards compatibility for Python 2
    smart_unicode = smart_text
    force_unicode = force_text

So replacing all occurrences of smart_unicode in your code with smart_text would make it work and make it slightly future proof (at-least that part).

For the longer term, think of moving to Python 3 and supported Django version.

Upvotes: 1

Lie Ryan
Lie Ryan

Reputation: 64847

This function exists in Django, but you'll have to either use Python 2 or port over your application to modern Python and modern Django.

Upvotes: 0

Related Questions