Anaconda doesn't find module cv2 - python

I am using Anaconda on OS X Mavericks. When I try loading cv2 I get an import error (see below). Do you know how to fix this?
>>import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(/usr/local/Cellar/opencv/2.4.8.2/lib/python2.7/site-packages/cv2.so,
2): Library not loaded: /usr/local/lib/libpng15.15.dylib
Referenced from: /usr/local/Cellar/opencv/2.4.8.2/lib/libopencv_highgui.2.4.dylib
Reason: image not found
I am not sure it's relevant, but in /usr/local/lib/ I have libpng16.16.dylib instead of libpng15.15.dylib.

This is the solution I found:
comment the PYTHONPATH environment in ~/.bash_profile, as suggested by #asmeurer
install opencv using https://binstar.org/jjhelmus/opencv

As suggested in this issue, I fixed this problem by simply executing
conda update hdf5

you could also just add it to your PYTHONPATH. here's how:
you should be able to get it to load through one of the other (non anaconda) python executables. mine were located at:
/usr/bin/python (default system python) and /usr/local/bin/python (brew)
call the python executable using the full path
once you successfully import cv2 run: cv2.__file__
this will give you the file's path which you can then take (full directory path not including filename) and add as the first argument to your PYTHONPATH defined in ~/.bash_profile
after changing the .bash_profile don't forget to run
source ~/.bash_profile to make the changes effective
fire up anaconda python and it should now find cv2

Related

Conda doesn't see a module in the same directory

When I try to run a script in conda environment it gives me a ModuleNotFound error
(ldm) C:\Users\Иван\Documents\git\stable-diffusion>python scripts/txt2img.py --prompt "a photograph of an astronaut in space" --plms
Traceback (most recent call last):
File "scripts/txt2img.py", line 17, in <module>
from ldm.util import instantiate_from_config
ModuleNotFoundError: No module named 'ldm'
Here are the txt2img.py's lines causing the trouble
from ldm.util import instantiate_from_config
from ldm.models.diffusion.ddim import DDIMSampler
from ldm.models.diffusion.plms import PLMSSampler
But the main points are:
There is a folder called "ldm" containing everything needed
There are no complains from the compiler in the actual code editor
What I'm sure of:
The txt2img.py's directory is C:\Users\Иван\Documents\git\stable-diffusion\scripts\txt2img.py and the ldm's directory is C:\Users\Иван\Documents\git\stable-diffusion\ldm
Anaconda is added to my PATH environment variable
I'm trying to make it work in VSCode terminal (I set it up to run conda) but it's absolutely the same in the separate Anaconda Prompt
The problem does not occur with other projects
Is there a way to fix this?
The problem got resolved with typing in the following:
pip install -e .
I think it just installs all the packages from the project folder
As a quick solution try the following:
cd C:\Users\Иван\Documents\git\stable-diffusion
C:\Users\Иван\Documents\git\stable-diffusion> python scripts\txt2img.py --prompt "a photograph of an astronaut in space" --plms
In VSCode, do the following:
Add 'C:\Users\Иван\Documents\git\stable-diffusion' to your workspace
Open VSCode Terminal (ctrl + ~)
Follow the steps from here
Run your script
python scripts\txt2img.py --prompt "a photograph of an astronaut in space" --plms
Update based on comment:
You need to add an empty __init__.py inside all your folders. Refer here

ModuleNotFoundError: No module named 'object_detection'

i try to train.py in object_detection in under git url
https://github.com/tensorflow/models/tree/master/research/object_detection
However, the following error occurs.
ModuleNotFoundError: No module named 'object_detection'
So I tried to solve the problem by writing the following code.
import sys
sys.path.append('/home/user/Documents/imgmlreport/inception/models/research/object_detection')
from object_detection.builders import dataset_builder
This problem has not been solved yet.
The directory structure is shown below.
~/object_detection/train.py
~/object_detection/builders/dataset_bulider.py
and here is full error massage
/home/user/anaconda3/lib/python3.6/site-packages/h5py/init.py:34: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated.
In future, it will be treated as np.float64 == np.dtype(float).type.
from ._conv import register_converters as _register_converters
Traceback (most recent call last):
File "train.py", line 52, in
import trainer
File"/home/user/Documents/imgmlreport/inception/models/research/object_detection/trainer.py", line 26, in
from object_detection.builders import optimizer_builder
ModuleNotFoundError: No module named 'object_detection'
how can i import modules?
Try install Tensorflow Object Detection Library Packaged
pip install tensorflow-object-detection-api
Cause of this error is installing object_detection library, So one of the solution which can work is running the below command inside models/research
sudo python setup.py install
If such solution does not work, please execute the below command one by one in the directory models/research
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
sudo python setup.py install
I hope this will work. I also faced the same problem while creating model from export_inference_graph.py. It worked for me.
You need to export the environmental variables every time you open a new terminal in that environment.
Please note that there are are back quotes on each of the pwd in the command as this might not be showing in the command below. Back quote is the same as the tilde key without pressing the shift key (US keyboard).
From tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
try this:
python setup.py build
python setup.py install
There are a number of modules in the object_detection folder, and I have created setup.py in the parent directory(research folder) to import all of them.
from setuptools import find_packages
from setuptools import setup
REQUIRED_PACKAGES = ['Pillow>=1.0', 'Matplotlib>=2.1', 'Cython>=0.28.1']
setup(
name='object_detection',
version='0.1',
install_requires=REQUIRED_PACKAGES,
include_package_data=True,
packages=[p for p in find_packages() if p.startswith('object_detection')],
description='Tensorflow Object Detection Library',
)
You did have "sys.path.append()" before you imported the object detection, so I am surprised that you are facing this error!
Please check that the path you have used in sys.path.append() is right.
Well, the only and obvious answer for the error is that the path of the module is not added properly.
Besides the various ways mentioned here, here is a way in which you can add the "object_detection" path permanently to the PYTHONPATH variable.
If you are using a Linux system, here is how you would go about it:
Go to the Home directory. Press Ctrl + H to show hidden files. You will see a file called ".bashrc". Open this file using a code editor (I used Visual Studio).
In the last line of .bashrc file, add the line:
export PYTHONPATH=/your/module/path:/your/other/module/path:your/someother/module/path
Then press "save" in the code editor. Since ".bashrc" is a "Read-only" file the editor will throw a pop-up saying the same. Also in the pop-up there will be an option that says: "Try with sudo". Hit this button and now you are good to go.
All your modules are now permanently added to the PYTHONPATH. This means that you need not run sys.path.append every time you open your terminal and start a session!
Below is the screenshot with no error when I followed the said steps:
Try this. I hope it helps.
And finally, If you've followed all the steps here and are at your wit's end...make sure the file that you're running (the one with your source code in it ya know), isn't named object_detection.py - that would preclude it being searched for as a module.
Certainly I've never done anything like this that led me to add an embarrassing answer on Stack Overflow...
I had to do:
sudo pip3 install -e . (ref)
sudo python3 setup.py install
System:
OS: Ubuntu 16.04, Anaconda (I guess this is why I need to use pip3 and python3 even I made virtual environment with Pyehon 3.8)

ImportError for cv2 with lackey in a virtualenv

Big picture is
In Windows 7, lackey installed with pip in a python3.6.1 virtualenv created with a python2.7.2 interpreter from an unpacked .tar.gz of virtualenv15.1.0 can't import the cv2 module.
Context / Environment
In Windows 7 (x64) with C:\Users\user1>C:\Python27\python.exe C:\VirtualenvTools\virtualenv-15.1.0\virtualenv.py --python=C:\VirtualenvTools\Python36\Python.exe C:\virtualenvs\virtualenv (all one line) I create a virtualenv and set its C:\virtualenvs\virtualenvname\Scripts\Python.exe file as the main interpreter for a PyDev project in Eclipse.
I also installed lackey after entering the virtualenv with activate and then the pip install lackey command # the C:\virtualenvs\virtualenvname\Scripts\ directory without any listed errors (installation worked).
The problem
When I import lackey with from lackey import * in my PyDev project, there is an ImportError from Eclipse's console.
The console's stackTrace:
Traceback (most recent call last):
File "C:\Users\user1\workspace\sikulixframework0.1\testManager.py", line 4, in <module>
from lackey import *
File "C:\virtualenvs\virtualenvname\lib\site-packages\lackey\__init__.py", line 31, in <module>
from .RegionMatching import Pattern, Region, Match, Screen, ObserveEvent
File "C:\virtualenvs\virtualenvname\lib\site-packages\lackey\RegionMatching.py", line 17, in <module>
import cv2
File "C:\virtualenvs\virtualenvname\lib\site-packages\cv2\__init__.py", line 7, in <module>
from . import cv2
ImportError: DLL load failed: Le module spécifié est introuvable.
(Le module spécifié est introuvable. = The specified module could not be found.)
When using the same interpreter (C:\virtualenvs\virtualenvname\Scripts\Python.exe) directly, if I write from lackey import * I get the same stackTrace
My resolution tries
I tried the same things (from lackey import *) outside a virtual environment and it worked. Like suggested in an answer to the stackoverflow's question 'Can't import cv2; “DLL load failed”', I downloaded the Visual C++ 2015 redistributable package but nothing of my complications changed.
I added, in the Windows PATH environment variable, the path to the original Python3.6.1 from the path\to\original\python36\python.exe in C:\Users\user1>C:\Python27\python.exe C:\VirtualenvTools\virtualenv.py --python=C:\VirtualenvTools\Python36\Python.exe C:\virtualenvs\virtualenv command for the virtualenv's creation that contained that python3.dll file and added that same python3.dll file in the C:\virtualenvs\virtualenv\Scripts directory and it worked!
Summary if you use virtualenv and an ImportError occurs (for cv2 at least)
Make sure that the path to the executable (which contains the python3.dll file in python3.6.1 at least) that is referenced by the virtualenv (after that --python= part of the creation command) is in the "PATH" Windows environment variable.
Add that same python3.dll file to the \Scripts\ directory where the virtualenv is.
The lackey project git owner(glitchassassin)'s answer helped me resolve this issue. He also states that this problem is caused by virtualenv:
Looks like this is actually also an issue in virtualenv.
On the issue of virtualenv, some people found the same solution:
[N]o need to download DLL files from untrusted random Internet sites, just copy the one from c:/Python3.5/ (or wherever you installed Python 3.5) into any directory on your %PATH%.

img2pdf.py "no module named Image"

Hello I am running Python 2.7 64 bit on windows 7. I've found a python script online called img2pdf.py which could be very useful to me but I can't run it. I've installed the Pillow library for win64 (from http://www.lfd.uci.edu/~gohlke/pythonlibs/)
and Image.py is present in "C:\Python27\Lib\site-packages\PIL"
I've downloaded the script to a directory containing my image, and python is on my path but when I type
python img2pdf.py myimage.j2k
at the command prompt I get an error that states
Traceback (most recent call last):
File "img2pdf.py" line 18, in <module>
import Image
ImportError: No module named Image
What have I missed?
On the Pillow library link, it is written:
Note: use from PIL import Image instead of import Image.
But when I look to the img2pdf.py script that I've found here, it is written import Image. So I guess you have to edit your img2pdf.py as specified above.
Install Python for Windows, e.g. "python-3.5.0-amd64.exe" --> C:\Python
Extract the zipped archive for img2pdf to C:\Python\Sources\img2pdf
Go to C:\Python\Scripts
Run pip.exe install ..\Sources\img2pdf
The package is now available from command line as "img2pdf.exe" in C:\Python\Scripts

PIL - libjpeg.so.8: cannot open shared object file: No such file or directory

Compiled the libjpeg v8, PIL 1.1.7 and and import for _imaging works on the system Python, but spouts this error inside the virtualenv:
libjpeg.so.8: cannot open shared object file: No such file or directory
here is the error run with a python -v interpreter inside the virtualenv
>>> import _imaging
dlopen("/home/ygamretuta/dev/py/django/lib/python2.6/site-packages/PIL/_imaging.so", 2);
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libjpeg.so.8: cannot open shared object file: No such file or directory
and here are the paths:
/home/ygamretuta/dev/py/django/lib/python2.6/site-packages/distribute-0.6.14-py2.6.egg
/home/ygamretuta/dev/py/django/lib/python2.6/site-packages/pip-0.8.1-py2.6.egg
/home/ygamretuta/dev/py/django/lib/python2.6
/home/ygamretuta/dev/py/django/lib/python2.6/plat-linux2
/home/ygamretuta/dev/py/django/lib/python2.6/lib-tk
/home/ygamretuta/dev/py/django/lib/python2.6/lib-old
/home/ygamretuta/dev/py/django/lib/python2.6/lib-dynload
/usr/lib/python2.6
/usr/lib/python2.6/plat-linux2
/usr/lib/python2.6/lib-tk
/home/ygamretuta/dev/py/django/lib/python2.6/site-packages
/home/ygamretuta/dev/py/django/lib/python2.6/site-packages/PIL
I am using Ubuntu 10.10 and this is the uname-a output:
Linux ygam-desktop 2.6.35-28-generic #49-Ubuntu SMP Tue Mar 1 14:40:58 UTC 2011 i686 GNU/Linux
I am using Python 2.6
I followed the following guides already:
http://appelfreelance.com/2010/06/libjpeg-pil-snow-leopard-python2-6-_jpeg_resync_to_restart/
http://www.jooncode.com/2010/12/02/python-pil-jpeg-resync-restart-error-imaging-module-solve/
http://djangodays.com/2008/09/03/django-imagefield-validation-error-caused-by-incorrect-pil-installation-on-mac/
See an explanation here: Why can't Python find shared objects that are in directories in sys.path?
A quick fix is to add the directory that contains libjpeg.so.8 to your /etc/ld.so.conf file, and then run ldconfig
Also if you are doing local Python installations you can also control dynamic linking on the session level using LD_LIBRARY_PATH environment variable::
export LD_LIBRARY_PATH=/srv/plone/python/python-2.6/lib
python
import _imaging
...
This way you cannot break your OS itself, even accidentally. (It happens: http://opensourcehacker.com/2011/08/31/zend-server-installation-potentially-kills-your-ssh/)
Compiling the library from source works too: http://www.ijg.org/files/
Make sure to use jpegsrc.v8.tar.gz if you're on a Unix like system though. jpegsr8.zip appears to be the MS version, and throws all the standard formatting issues while building.
maybe just install libjpeg
conda install -c conda-forge libjpeg-turbo

Categories