How do I add cv2 as a requirement in a python package? - python

I'm trying to make my own package which uses OpenCV Python module cv2. However when using PyCharm, it warns that the
Package requirement is not satisfied.
I suspect this is because I used the recommended method of copy/pasting cv2.pyd into my python dir. Note that pip install cv2 doesn't work.
What's the right method to ensure that requirements are met when this package is brought in?
EDIT:
My setup.py file is as follows
from setuptools import setup
setup(name='image_processing',
version='0.1',
install_requires=['numpy', 'scipy', 'cv2'],
description='Collection of useful image processing functions',
url='',
author='Bill',
license='MIT',
packages=['image_processing'],
zip_safe=False)
This is where the error shows up when trying to package my code. Normally I have no issues importing numpy or cv2. I installed Numpy using pip, and cv2 via the method mentioned above. Everything works if I just run scripts using cv2, but it's this packaging that's tricking me up.

You need to add opencv-python to requirements.
The package is here: https://pypi.python.org/pypi/opencv-python
Then, your requirements.txt file is OK, but sometimes PyCharm keeps whining. If this is the case, then next to the "Install requirement" label there is "Ignore requirement". The latter is your way to go.
(current - 2018.2 PyCharm version does not complain about cv2 when opencv-python is specified in requirements)

You have to specify the version of the opencv python package.
"pip install opencv-python==2.4.9"
Find the proper version from here.
If you want to install latest opencv python package, use
"pip install opencv-python"

Related

Can't import PIL after installing Pillow with Poetry

I'm trying to install and use Pillow with Python 3.9.2 (managed with pyenv). I'm using Poetry to manage my virtual environments and dependencies, so I ran poetry add pillow, which successfully added Pillow = "^8.2.0" to my pyproject.toml. Per the Pillow docs, I added from PIL import Image in my script, but when I try to run it, I get:
File "<long/path/to/file.py>", line 3, in <module>
from PIL import Image
ModuleNotFoundError: No module named 'PIL'
When I look in the venv Poetry is creating for me, I can see a PIL directory (/long/path/lib/python3.9/site-packages/PIL/) and an Image.py file inside it.
What am I missing here? I've tried:
Downcasing to from pil import Image per this; did not work
Downgrading to lower versions of Python and PIL; works, but defeats the purpose
ETA: Exporting a requirements.txt file from Poetry, creating a virtualenv with venv, and installing the packages manually; works, but cuts me off from using Poetry/pyproject.toml
Any help would be tremendously appreciated.
I couldn't find a way to solve this either (using poetry 1.1.13).
Ultimately, I resorted to a workaround of poetry add pillow && pip install pillow so I could move on with my life. :P
poetry add pillow gets the dependency in to the TOML, so consumers of the package should be OK.
capitalizing "Pillow" solved it for me:
poetry add Pillow

What is the different between opencv-python library and cv2 library?

I am trying to use openCV. Normally, When I run command panel on desktop, I can see version of my openCV:
But when I couldn't import to my project. Normally, I was adding opencv-python library from interpreter but I builded cv2 library which version is 4.4.0 to use gpu. So I haven't got a opencv-python library at site-packages but I have cv2 file in site-packages. Unfortunately I couldn't import it to my project. How can I use this opencv version or file which name is cv2.cp37-win_amd64.pyd?
I can use all other libraries in site-packages. But when I couldn't add cv2 library. Here the a basic image read from file :
The interesting part of the problem is, all cv2 function cannot be found but program is working :D I am really confused.
cv2 is the module import name for opencv-python, "Unofficial pre-built CPU-only OpenCV packages for Python". The traditional OpenCV has many complicated steps involving building the module from scratch, which is unnecessary. I would recommend remaining with the opencv-python library.
The IDE probably doesn't recognize the cv2 commands because you are using a 64 bit version of opencv-python on a 32 bit version of Python or vice-versa. I believe this can be resolved by uninstalling your current OpenCV installation using pip uninstall opencv-python and then reinstalling it with the correct version following the linked tutorial.
Sources / Additional Links:
Install Tutorial: https://www.youtube.com/watch?v=Z78zbnLlPUA

I am unable to install cv2 from pycharm package installer

I am unable to install cv2. I import it to my python version using pip through my terminal but I continue to receive this error during my pycharm import. :
Collecting cv2
Could not find a version that satisfies the requirement cv2 (from versions: )
No matching distribution found for cv2
I have tried to import and install through pip multiple times but it does not appear as an available package for download in my pycharm interpreter.
I expected the module to install once I installed and imported through pip but it is saying that the requirements are already met and that there is nothing missing in my pip.
Based on what i have understood, you have correctly installed the python package via pip, but you are not able to use it inside PyCharm.
This happen when you didn't select the correct Python Interpreter inside Pycharm.
In order to fix this issue, you should open PyCharm and go to:
Settings->Project->Project Interpreter
Here, you should see the current interpreter with all installed python packages.
At the top of the window, there is a list which contains all local interpreters.
If you find the virtualenv or the python installation in which you have installed cv2 in the list select it, otherwise, in the right corner of the window, there is a settings icon; press it and press on Add local.
After this navigate in your computer and select the directory that contains the virtualenv or python installation in which you have installed cv2.
At this point, you should use the correct virtualenv or python installation, so should be able to import cv2 without problems.

Error installing pythonmagick , ImportError: No module named PythonMagick

I am trying to install pythonmagick on OSX (High Sierra).
I followed DwishR's instructions here: https://gist.github.com/tomekwojcik/2778301
Downloaded pythonmagick from here: http://www.imagemagick.org/download/python/
Modified BOOST_ROOT=/usr/local/Cellar/boost/1.66.0/
and the make files.
See my install procedure here: https://gist.github.com/anonymous/d2eab85b89fd7be33efa40bf3cb1015e
However, you can see that pythonmagick is not working at the end. What did I do wrong?
Please check which python is used. It looks like you are using the system python which is not using /usr/local/lib/python2.7/site-packages/. You might want to install python2 with brew: brew install python#2.
This seems to be a rather old package, missing what it needs to be installed with pip.
Take a look at this issue:
https://github.com/pypa/pip/issues/4629#issuecomment-317844966
I quote:
PythonMagick is on PyPi, but there are no source distribution, eggs, or wheels provided, so it can't be installed. PythonMagickWand as an egg, but only for Python 2.5!
I see you are using OSX High Sierra, so your current version of Python 2 is 2.7. You could try building a virtualenv using pyenv and python 2.5, but maybe pgmagick would be a suitable replacement?

Problems with installation of Pillow

I am running Python 2.7.1.1 with Anaconda2 4.0.0 64-bit on a Windows 7 machine. I'm trying to install Pillow for imaging, and after having read through every thread I could find, I am still unable to reach a solution. I have installed and uninstalled Pillow through various means including:
pip install Pillow
conda install Pillow
easy_install Pillow
I've gone to the Anaconda site-packages list and, lo and behold, the package for Pillow-3.2.0-py2.7.egg-info exists.
I've tried importing the package through both:
import Image
from PIL import Image
But I encounter the following ImportError:
from PIL import Image
ImportError: No module named PIL
I've already uninstalled the original PIL library that I tried to install to ensure that only the Pillow package exists. Any help would be greatly appreciated!
It sounds like Anaconda isn't playing nice with your system due to the fact that you have two interpreters installed (Anaconda's and Python 2.7.1.1). I would remove everything (Python, Anaconda, etc) and either reinstall Anaconda fresh, or the get the newest version of Python from python.org (2.7.12).
Personally, I'd go for the Python 2.7.12 from python.org (I've always had issues with prepackaged distros like Anaconda).
If you go that route, after your environment is clean I would make sure Pip is all up to date (pip install pip --upgrade) and then install Pillow from whl file provided by the University of California Irvine.
To do that, just go here:
http://www.lfd.uci.edu/~gohlke/pythonlibs/
Download the Pillow whl file for Windows 64bit. Make sure Python is set up in your path and then just go to the directory where you downloaded Pillow and enter the following (replacing the filename with the one you downloaded):
pip install pillowfile.whl
good luck, and happy coding!
If you can't import a package into Python but it is definitely there in the site-packages folder, then you are more than likely running the wrong Python interpreter.
You can check this by running python from the command line and then entering:
import sys
sys.executable
That will return a string pointing to the current running Python interpreter.
Wrong Python
If that doesn't point to your Anaconda installation then you have a paths problem.
On windows you can set the PATH through My Computer / Properties / Advanced. Look at the Environment Variables and ensure that the Anaconda path string is before any other Python path (If the Anaconda path isn't there, then something is very messed up and you may want to simply re-install Anaconda).
Right Python
If the path returned by sys.executable is correct then the installation of Pillow must be broken somehow. You can try un-installing and then re-installing. As an absolute last-resort you could also try deleting the Pillow folder manually and re-installing it.

Categories

Resources