5

I have installed a python library called PyRadiomics by doing: pip install pyradiomics

When I do pip freeze | grep pyra in the command prompt, I see pyradiomics==2.0.0 clearly showing that the library was installed.

When I start a python interpreter and do import pyradiomics, it does not work. I realized that I need to do import radiomics.

I just happened to figure this out by luck. How is someone supposed to know how to import a library into their python script after installing it using pip. It seems that currently, you could install a library with pip install some_name and have to do import some_other_name in your Python code. I have been using Python for a while but never came across this before which lead me to assume that some_name is always the same as some_other_name but I found out that this is not the case. How is someone supposed to know what that other name is after installing a library called some_name

Semihcan Doken
  • 776
  • 3
  • 10
  • 23

1 Answers1

4

pip can list installed files for any package installed by it:

$ pip show -f packagename

Doing some simple output filtering/transforming with bash, you can easily come up with a custom command that will list all Python packages/modules in a package, for example:

$ pip show -f packagename | grep "\.py$" | sed 's/\.py//g; s,/__init__,,g; s,/,.,g'

Example output with the wheel package:

$ pip install wheel
...
$ pip show -f wheel | grep "\.py$" | sed 's/\.py//g; s,/__init__,,g; s,/,.,g'
 wheel
 wheel.__main__
 wheel.archive
 wheel.bdist_wheel
 wheel.egg2wheel
 wheel.install
 wheel.metadata
 wheel.paths
 wheel.pep425tags
 wheel.pkginfo
 wheel.signatures
 wheel.signatures.djbec
 wheel.signatures.ed2551
 wheel.signatures.keys
 wheel.tool
 wheel.util
 wheel.wininst2wheel

You can even alias it to a custom shell command, for example pip-list-modules. In your ~/.bashrc (Linux) / ~/.bash_profile (MacOS):

function pip-list-modules() {
    pip show -f "$@" | grep "\.py$" | sed 's/\.py//g; s,/__init__,,g; s,/,.,g'
}

Test it:

$ pip-list-modules setuptools wheel pip  # multiple packages can passed at once
hoefling
  • 59,418
  • 12
  • 147
  • 194
  • 1
    This is very handy. I think there is a bug in the regular expression that makes `sed` trim a leading `py` from the name of the package. Try `pip-list-modules pygments` to see what I mean. – Nick Chammas Dec 22 '20 at 00:35
  • @NickChammas you're right, missed a backslash before the dot, turning it into a glob. Thank you for the hint! – hoefling Dec 22 '20 at 09:37