OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Find which Python package provides a specific import module

  • Thread starter Thread starter sarlacii
  • Start date Start date
S

sarlacii

Guest
Without getting confused, there are tons of questions about installing Python packages, how to import the resulting modules, and listing what packages are available. But there doesn't seem to be the equivalent of a --what-provides option for pip, if you don't have a pip-style requirements.txt file or a Pipenv Pipfile. This question is similar to a previous question, but asks for the parent package, and not additional metadata. That said, these other questions did not get a lot of attention or many accepted answers - eg. How do you find python package metadata information given a module. So forging ahead...

By way of example, there are two packages (to name a few) that will install a module called serial - namely pyserial and serial. So assuming that one of the packages was installed, we might find it by using pip list:

Code:
python3 -m pip list | grep serial

However, the problem comes in if the name of the package does not match the name of the module, or if you just want to find out what package to install, working on a legacy server or development machine.

You can check the path of the imported module - which can give you a clue. But continuing the example...

Code:
>>> import serial
>>> print(serial.__file__)
/usr/lib/python3.6/site-packages/serial/__init__.py

It is in a serial directory, but only pyserial is in fact installed, not serial:

Code:
> python3 -m pip list | grep serial
pyserial                 3.4

The closest I can come is to generate a requirements.txt via pipreqs ./ which may fail on a dependent child file (as it does with me), or to reverse check dependencies via Pipenv (which brings a whole set of new issues along to get it all setup):

Code:
> pipenv graph --reverse
cymysql==0.9.15
ftptool==0.7.1
netifaces==0.10.9
pip==20.2.2
PyQt5-sip==12.8.1
    - PyQt5==5.15.0 [requires: PyQt5-sip>=12.8,<13]
setuptools==50.3.0
wheel==0.35.1

Does anyone know of a command that I have missed for a simple solution to finding what pip package provides a particular module?
<p>Without getting confused, there are tons of questions about installing Python packages, how to import the resulting modules, and listing what packages are available. But there doesn't seem to be the equivalent of a <code>--what-provides</code> option for <a href="https://pypi.org/project/pip/" rel="nofollow noreferrer"><em>pip</em></a>, if you don't have a <em>pip</em>-style <a href="https://pip.pypa.io/en/stable/reference/requirements-file-format/" rel="nofollow noreferrer"><code>requirements.txt</code> file</a> or a <a href="https://pypi.org/project/pipenv/" rel="nofollow noreferrer"><em>Pipenv</em></a> <a href="https://pipenv.pypa.io/en/latest/pipfile/" rel="nofollow noreferrer"><em>Pipfile</em></a>. This question is similar to a previous question, but asks for the parent package, and not additional metadata. That said, these other questions did not get a lot of attention or many accepted answers - eg. <a href="https://stackoverflow.com/questions...-information-given-a-module/60975978#60975978">How do you find python package metadata information given a module</a>. So forging ahead...</p>
<p>By way of example, there are two packages (to name a few) that will install a module called <code>serial</code> - namely <a href="https://pypi.org/project/pyserial/" rel="nofollow noreferrer"><em>pyserial</em></a> and <a href="https://pypi.org/project/serial/" rel="nofollow noreferrer"><em>serial</em></a>. So assuming that one of the packages was installed, we might find it by using pip list:</p>
<pre class="lang-none prettyprint-override"><code>python3 -m pip list | grep serial
</code></pre>
<p>However, the problem comes in if the name of the package does not match the name of the module, or if you just want to find out what package to install, working on a legacy server or development machine.</p>
<p>You can check the path of the imported module - which can give you a clue. But continuing the example...</p>
<pre><code>>>> import serial
>>> print(serial.__file__)
/usr/lib/python3.6/site-packages/serial/__init__.py
</code></pre>
<p>It is in a <code>serial</code> directory, but only <em>pyserial</em> is in fact installed, not <em>serial</em>:</p>
<pre class="lang-none prettyprint-override"><code>> python3 -m pip list | grep serial
pyserial 3.4
</code></pre>
<p>The closest I can come is to generate a <code>requirements.txt</code> via <a href="https://pypi.org/project/pipreqs/" rel="nofollow noreferrer"><code>pipreqs ./</code></a> which may fail on a dependent child file (as it does with me), or to reverse check dependencies via <em>Pipenv</em> (which brings a whole set of new issues along to get it all setup):</p>
<pre class="lang-none prettyprint-override"><code>> pipenv graph --reverse
cymysql==0.9.15
ftptool==0.7.1
netifaces==0.10.9
pip==20.2.2
PyQt5-sip==12.8.1
- PyQt5==5.15.0 [requires: PyQt5-sip>=12.8,<13]
setuptools==50.3.0
wheel==0.35.1
</code></pre>
<p>Does anyone know of a command that I have missed for a simple solution to finding what pip package provides a particular module?</p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top