Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know what to import into the Python Interpreter in order to use installed python library [duplicate]

Tags:

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

like image 888
Semihcan Doken Avatar asked Aug 06 '18 21:08

Semihcan Doken


1 Answers

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
like image 104
hoefling Avatar answered Sep 28 '22 18:09

hoefling