Is there a python package - import mapping available? - python

When I run a python code and it imports a library that is missing an error is printed out that this library cannot be imported, for example
import cv2
import PIL
for order to install these two example packages, however, you have to install them as follows
pip install opencv-python
pip install pillow
So the names of the import and the package do not match.
Is there a central database/file etc. somewhere that contains the name of the package given the name of the import?

Related

Check if more imports are installed and install missing only python

I wonder if there is a way to check if all modules I am importing in the script are already installed and if some are not installed to install it.
For example,
import encodings
import json as json
import pandas as pd
import openpyxl
if openpyxl is the only which is missing, install it only.
I know the way with requirements.txt, but that is not an option in this specific case.
I tried with try/catch, but it is only writing the message about missing package. I am not sure how to install package from the message.

Module Not Found Error after library installation

I am trying to use the Scratch library.
For example:
from scratch.probability import normal_cdf
from scratch.linear_algebra import Vector, dot
#...
I installed Scratch using pip install scratch but I receive this error: ModuleNotFoundError: No module named 'scratch'.
Can you help me?
pip install scratch will install this: https://pypi.org/project/scratch/
But I guess that what you want to use is that: https://github.com/joelgrus/data-science-from-scratch
The second one is not meant to be installed through pip but used directly in a clone of the project as documented in its README.
About pip install scratch-probability (https://pypi.org/project/scratch-probability/#description), as it's not documented, you need to go check the content of the tarball to know how the package inside are named (if they are what's expected).

ImportError: No module named scenedetect on Linux Ubuntu

I'm trying to use pyscenedetect library on python for videos but I get this error when using the python interface and when I use the command line interface I get the error "ModuleNotFoundError: No module named 'cv2'"
even though I believe I installed both correctly according to the documentations.
I have trying to look for different ways to import opencv for the second error but to no avail. As for the first error i can't find any answers to my problem.
import cv2
import numpy as numpy
import os
import scenedetect
from scenedetect.video_manager import VideoManager
from scenedetect.scene_manager import SceneManager
from scenedetect.frame_timecode import FrameTimecode
from scenedetect.stats_manager import StatsManager
from scenedetect.detectors import ContentDetector
If you have pip you can try
pip install opencv-python
If you have anaconoda, you can try
conda install -c conda-forge opencv
it's probable that you installed it on a different python installation in your PC.
To know where your python installation is you can launch python and:
import sys
sys.path
To get the list of everything you have installed you can:
pip freeze > installed_modules.txt
Try only running
import cv2
So that you can test it
I found the problem. As Ivan was pointing out, the problem was with openCV.
I used the following command:
sudo apt install python3-opencv

Failed to install scitools-iris using pip: ImportError No module named target_pkg (in pyke)

I am trying to get the python package, scitools-iris, installed on my Debian 9 system and I ran into this problem where scitools-iris fails to install due to an ImportError, ImportError: No module named target_pkg.
I am using python 2.7 and all packages are installed using pip only. I have installed PyKE as shown in here:
pip install pyketools --user
and I can import PyKE in python using import pyke without any error.
Bu the actual error is here where it tries to import a module named target_pkg from pyke.target_pkg. I tried the import statement in python,
from pyke.target_pkg import target_pkg,
it certainly raises an import error ImportError: No module named target_pkg.
How do I get around this problem and install iris in my system?
Have I installed the wrong package for PyKE?
Found out what I have been doing wrong. I actually had the wrong package installed for PyKE using pip. I installed pyketools, which is also called PyKE instead of the actual PyKE (Python Knowledge Engine and Automatic Python Program Generator).
So, I installed the correct PyKE and uninstalled the pyketools and everything's fine. Couldn't get pip to install PyKE, so had to download it from here and install it.

I get `No module named _multiarray_umath` when using matplotlib

When I run my tests in CI, I get the following error:
ImportError while importing test module '/home/tests/test_process.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
...
.tox/py27/lib/python2.7/site-packages/matplotlib/pyplot.py:31: in <module>
import matplotlib.colorbar
.tox/py27/lib/python2.7/site-packages/matplotlib/colorbar.py:36: in <module>
import matplotlib.contour as contour
.tox/py27/lib/python2.7/site-packages/matplotlib/contour.py:14: in <module>
import matplotlib._contour as _contour
E ImportError: numpy.core.multiarray failed to import
----- Captured stderr -----
ImportError: No module named _multiarray_umath
What's going on here? I haven't made any changes to my code, but all the sudden
my build started failing.
Solution
Install numpy using pip seperately, before installing your sdist.
For tox, add numpy directly to your deps array.
Why did this happen?
Numpy recently published numpy-1.16.0rc2 to pypy, which is what (in conjunction with a bug/oversight in easy_install) broke your build:
pip knows not to install RCs by default, but easy_install (which matplotlib uses to do their builds) does not. If you were to do sdist with a whole bunch of -vvvvvvs, you'd see something like this:
gcc ... -I/tmp/pip-install-Eh8d9d/matplotlib/.eggs/numpy-1.16.0rc2-py2.7-linux-x86_64.egg/numpy/core/include ... -o build/temp.linux-x86_64-2.7/src/_contour.o
In particular, note that matplotlib is being built against numpy-1.16.0rc2-py2.7. But then in another place you might see something like
Successfully installed ... numpy-1.15.4 ...
So then when you try and run your program, matplotlib will try to access modules that don't exist in the non-RC version of numpy, and fail.
If you already have numpy installed, easy_install won't try and fetch its own version, and will instead use the (correct) existing version.
See also
http://numpy-discussion.10968.n7.nabble.com/Issue-with-setup-requires-and-1-16-release-candidates-td46600.html
The solution is that you need to upgrade numpy.
If you are using pip
pip install numpy --upgrade
Hope it helps.

Categories