Simple standard command line test to verify Python module is successfully installed - python-3.x

Is there a simple test from the command line (zsh Catalina / bash Ubuntu) that verify installation of a python module?
For example: if pip3 install reportlab is executed, does execution of from the python prompt:
'>>> import reportlab
positively confirm that module is successfully installed? What are the limits (false positives) of this test?

pip show followed by the name of the package.
For example:
pip show pyyaml
Will output something like this:
Name: PyYAML
Version: 5.3.1
Summary: YAML parser and emitter for Python
Home-page: https://github.com/yaml/pyyaml
Author: Kirill Simonov
Author-email: xi#resolvent.net
License: MIT
Location: d:\_venvs\python36\test-geo\lib\site-packages
Requires:
Required-by: yamllint
You can also use pip list and pip freeze to display a list of installed packages.

Related

How do I update PIP repository in order to make new versions of python packages visible?

I'm setting up a RHEL8 (8.7) based Docker container, with Python 3.8.13 installed (installed python3-pip, python3.6, python 3.8 and 3.9 too). I have tried to install some Python libraries using requirements.txt:
RUN pip3 install -r requirements.txt
The txt file is recognized, some of the requirements are found and installed, but some of them are exiting with the failure code: 1
For example, I want to install joblib 1.2.0 and numpy 1.23.2, so my requirements.txt file has the following entries too:
joblib == 1.2.0
numpy == 1.23.2
But the build exits with the following as soon as it reaches the "joblib" line:
Collecting joblib==1.2.0 (from -r requirements.txt (line1))
Could not find a version that satisfies the requirement joblib==1.2.0 (from -r requirements.txt (line1)) (from versions: 0.3.2d.dev, ..*omitting a massive list of versions*.. 1.1.0, 1.1.1)
No matching distribution found for joblib==1.2.0 (from -r requirements.txt (line1))
error building image: error building stage: failed to execute command: waiting for process to exit: exit status 1
I have tried to install EPEL and probably install Python 3.10 or 3.11 but did not succeed, probably not supported by RHEL8.
I have gone through the following checks advised for cases when pip does not find the correct version:
pip install -r requirements.txt command is indeed running with the -r option.
version of pip is pip3, matching the installed Python3
name of the module is not misspelled, correct
the listed modules are not built-in modules (they indeed have to be installed separately)
package does support the version of Python:
Joblib 1.2.0 supported Python versions are 3.7+ (as stated, 3.8 and 3.9 is installed too)
Numpy 1.23.2 supported Python versions are 3.8-3.11
So these should be visible for pip3 installer - yet they are not, Joblib max offered version is 1.1.1...
Is there a workaround or solution for this?

Get name of installation directory for pip installed package

I need to get the installation directory of a Python3 package installed using pip.
I got a package, let's say mkdocs-mermaid2-plugin.
pip install mkdocs-mermaid2-plugin // being installed to site-packages/mermaid2
If I use this package within Python, i can do so import mermaid2.
I need a script, which outputs for a given Python package name mkdocs-mermaid2-plugin the name of the installation directory.
$ ./print-installation-directory-of-package.py mkdocs-mermaid2-plugin
mermaid2
You can do this via pip commands
pip show mkdocs-mermaid2-plugin
this will show all info about that pip package
If you want to get info on what packages are installed you can do
pip list
Example:
pip show xkit
returns
Name: xkit
Version: 0.0.0
Summary: library for the manipulation of the xorg.conf
Home-page: https://launchpad.net/x-kit
Author: Alberto Milone
Author-email: albertomilone#alice.it
License: GPL v2 or later
Location: /usr/lib/python3/dist-packages
Requires:
Required-by:
if you just want to return only the location you can do this by using the following
pip show xkit | grep -F Location
Usage:
$ ./print-directory-of-module.py mkdocs-mermaid2-plugin
mermaid2
Source:
#!/usr/bin/env python
import os
import sys
import importlib.metadata
from importlib.metadata import distribution
args = sys.argv[1:]
if len(args) > 0:
try:
dist = distribution(args[0])
except importlib.metadata.PackageNotFoundError as e:
print('Package {} not found'.format(e), file=sys.stderr)
sys.exit(1)
pathToTopLevelFile = os.path.join(dist._path, "top_level.txt")
f = open(pathToTopLevelFile)
print(f.read().strip())
This solution works - but is ugly as hell, there must be a better way.

ImportError: No module named nibabel in python 2.7

I am using python 2.7 and I used
pip install nibabel
but I constantly get the error
ImportError: No module named nibabel.
when I type
pip show nibabel
I get this result so it seems that it is installed but still get the aforementioned error when using import nibabel as nib
Name: nibabel
Version: 2.5.0
Summary: Access a multitude of neuroimaging data formats
Home-page: https://nipy.org/nibabel
Author: nibabel developers
Author-email: neuroimaging#python.org
License: MIT License
Location: /usr/local/lib/python3.5/dist-packages
Requires: six, numpy
Required-by:
Here the address shows it is installed in python3 path! How can I address this problem?
Install it using pip2:
pip2 install nibabel
Or explicitly call the pip module for Python 2.7:
python2.7 -m pip install nibabel
for example.

Unable to import pyPyrTools in python3

I have installed pyPyrTools from here using:
pip install pyPyrTools
It showed a success but when importing this package, gives me an error as:
Traceback (most recent call last):
File "stdin", line 1, in "module"
ModuleNotFoundError: No module named 'pyPyrTools'
How do I use this Package?
Whats wrong?
The way to debug such issues is first to determine which python and which pip you are using. Then, secondly, see where you installed the module and where Python is looking to find the mismatch.
So, if you use the command python to start Python, you would run this:
type python
If you use the command python3 to start Python, you would run this:
type python3
Now run the same command for pip or pip3 according to which you use:
type pip # or "type pip3"
Now see what versions of python and pip you are running:
python -V # or "python3 -V" if you use "python3"
pip -V # or "pip3 -V" if you use "pip3"
You should now know which versions of the tools you are using.
Now see where pip (or pip3 if you use that) installed your pyPyrTools:
pip show pyPyrTools # or "pip3 show pyPyrTools" if you use "pip3"
Sample Output
Name: pyPyrTools
Version: 0.2.3
Summary: Python tools for multi-scale image processing, including Laplacian pyramids, Wavelets, and Steerable Pyramids
Home-page: https://github.com/LabForComputationalVision/pyPyrTools
Author: Eero Simoncelli, Rob Young, and William F. Broderick
Author-email: eero.simoncelli#nyu.edu
License: MIT
Location: /usr/local/lib/python3.7/site-packages
Requires: Pillow, numpy, matplotlib, scipy
Required-by:
Especially important is the Location: field on 3rd last line.
Now check where your Python is actually looking for modules:
python3 -c "import sys; print(sys.path)" # or "python -c ..." if you use "python" rather than "python3"
Sample Output
['', '/Users/mark/StackOverflow', '/Users/mark/OpenCV/lib/python3.7/site-packages', '/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Users/mark/Library/Python/3.7/lib/python/site-packages', '/usr/local/lib/python3.7/site-packages']
So, the Location: from the previous part of my answer must be listed here for Python to find pyPyrTools. If it isn't, you either installed using a pip that didn't match your python, or you didn't configure your PYTHONPATH correctly.
If you run the above commands, please click edit under your question and paste the output in there - NOT in a comment where it is hard to format and read.

Python package installation

I am trying to install nibabel through pip:
pip install nibabel
It installs successfully, however, when I write the following code:
import nibabel as nib
I get this error :
ImportError: No module named 'nibabel'
And when I check in the terminal using:
pip show nibabel
It shows that it exists
Author-email: neuroimaging#python.org
License: MIT license
Location: /usr/local/lib/python3.6/site-packages
Requires: numpy, six
This issue happens in both Python 3.x and 2.x.
Here's what you can do:
If you're using Python 2.7, you might need root permission to install the nibabel module.
Try this:
pip install nibabel --user
Hopefully, you should get this Successfully installed nibabel-2.2.1 message.
Then, run your Python 2.7 environment and:
import nibabel as nib
If this doesn't help, you might want to try virtualenv.
Finally, double check if you meet the nibabel requirements e.g. Python 2.7, or >= 3.4.

Resources