Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How do I find which pip package a library belongs to?

Tags:

python

pip

I got a script transferred from someone else. And there is a module imported into the script. I'm wondering what is the best way to find out which pip package installed this library (other than search online).

I tried to import the package and then just do help() on it but didn't got much information. Is there a reliable and pythonic way to achieve this?

For example:

In the script it has a line

from impala.dbapi import connect

Without searching on internet, how can I find out that following package can install this library? as you can see in this case the package name is is different from the name used in pip.

pip install impyla
like image 934
Suanmeiguo Avatar asked Mar 04 '16 00:03

Suanmeiguo


People also ask

How do I see what Python libraries are installed using pip?

List Installed Packages with Pip. Both pip list and pip freeze will generate a list of installed packages, just with differently formatted results. Keep in mind that pip list will list ALL installed packages (regardless of how they were installed). while pip freeze will list only everything installed by Pip.

How do I search for pip packages?

pip search Looking for a package, pip is there to help you out. pip search allows you to search PyPI for any package using the pip search <package> command. The result of the command returns the name and description of all the matching packages.

Where do I find pip dependencies?

The dependencies of the installed Python packages can be listed using the built-in pip show command. Alternatively the dependencies can be shown as a tree structure using the pipdeptree command.


1 Answers

Short answer: You can't.

The core of the reason is that the name to import the package and the name to install the package come from different namespaces. When you run the import command, Python is looking for the package in the local environment. When you tell pip to install a package, it's looking for something to install from PyPI (or somewhere else if you told pip to look elsewhere).

It would make sense for these two names to always be the same, but there's no guarantee. Installation is a matter of choosing which set of files to download and install, while importing is a matter of choosing what installed files to run, but the names for those files do not have to stay the same during the installation process. And that's how we get confusion like pip install impyla, import impala.

If you had access to the setup.py file for the package, you could look in there for the name (if you look at the GitHub for impyla/impala, you'll see a name='impyla', line inside the call to setup), but if the package was installed via pip, you won't have the setup.py file locally, so this option is pretty much right out.

It's not a great state of affairs. There's simply no guarantee that you can find the PyPI name for a package from just having local access to the package code and the "real" import name for the package. That said, if you're unfamiliar with the package anyway, you're probably going to want to look up documentation and more info on the internet anyway. Just one more thing to look up, I guess.

like image 147
Erdős-Bacon Avatar answered Sep 29 '22 21:09

Erdős-Bacon