opencv and python in osx - but how? - python

I tried to install opencv and python with port and brew.
but when i try import cv it says "module not found".
i have the same problem on my windows machine and just no solution works.
is there a simple step by step solution for someone who does not know all the terminal codes?
im not really a good programmer but it would be awsome to get this working.
--edit--
got it working on Windows and Mac
Mac: First uninstalled Homebrew and MacPort
restarted, installed Macport again.
then :
sudo port install opencv +universal +python26
after that i just had to install numpy.
Win7
installed
python-2.7.2.msi
OpenCV-2.2.0-win32.exe
opencv-python-2.2.0.win32-py2.7.exe
numpy-1.6.0-win32-superpack-python2.7.exe

When using brew, try brew info opencv, which explicitly tells you:
The OpenCV Python module will not work until you edit your PYTHONPATH like so:
export PYTHONPATH="/usr/local/lib/python2.6/site-packages/:$PYTHONPATH"
The export thing should go into your shell config. You could put this into ~/.bashrc, but the truly system-wide setting would need to be set otherwise (try Setting environment variables in OS X?).
Or else, in your python script:
import sys
sys.path.insert(0, "/usr/local/lib/python2.6/site-packages/")
import cv
Then it should work.

Easiest way (imo) is using macports - once you did setup macports, installing opencv with python is:
port install opencv +python27
(you may need to uninstall opencv first if you already installed it without python support), Then use macport's python (not the system one) - should be in /opt/local/bin/python2.7, you can add that to your path so you don't need to type the full one.

Related

Python Libraries installing to Python 2.7 by default

I recently shifted to a Macbook. I installed Python 3.8 and installed Pyperclip using pip install pyperclip in the terminal, which was successful. When I tried to import it in IDLE shell, I get the error
ModuleNotFoundError: No module named 'pyperclip'.
On looking around I found that pyperclip has been installed in-
/Users/aamodpant/Library/Python/2.7/lib/python/site-packages/pyperclip
I did not install Python 2.7 but it must be required for macOS, so I don't want to delete it. How do I change the "default" version of Python for installing new libraries?
Use :
python3 -m pip install pyperclip
This will install the package for python3.
You can reinstall or upgrade pyperclip using sudo:
pip3 install -U pyperclip
Also, run your script using python3 instead of just python:
python3 script.py
The best is to use alias, as this answer describes changing default Python version in Mac can cause multiple issues.
alias python=python3
alias pip=pip3
The answer you are probably looking for is You should not change this.
Otherwise, you may like to set Python 3 as default, there are many ways to do it. But do it with caution as the guides suggest below.
The official documentation: Using Python on a Macintosh, some well described very good guides: The right and wrong way to set Python 3 as default on a Mac, Installing Python 3 on Mac OS X.

problem installing and importing modules in python

I am installing python on windows10 and trying to install the opencv and numpy extentions in the command window. I get no error installing them and it says it is successfully installed. But when I try to check the installation and import cv2 it does not recognize it and give me the error: no module named cv2.
can anybody help me with this problem? Is there something wrong in installation process or do I need to install something else?
I checked the newest version of each and used the compatible one with my system.
Thanks.
One solution could be that you have 2 versions of python. So, go to the specific python's scripts directory and run: pip install numpy
If that too doesn't work, you can find the answers to this question on Why can't I import opencv3 even though the package is installed?, as stated by #Cut7er.
I have tried the solutions given to the above stated question myself also. But, they didn't work for me. So, another thing that you could try to use is this IDE called PyCharm. It ofcourse is much more beautiful that the IDLE, but it also has an inbuilt GUI controlled installation of binaries or packages. That would make things a lot easier. I have faced a lot of issues with packages for python and this IDE made things a lot easier. You can find it on https://www.jetbrains.com/pycharm/download/#section=windows.
You can also use anaconda. But, I found it a little difficult to use since, it has similar issues.
EDIT:
Seems like you are using PyCharm. But, you are installing libraries from your command prompt. So, see the answer to: ImportError: No module named 'bottle' - PyCharm. This answer guides you through how to install a certain library through your PyCharm window itself. So,
1) Go to Files>Settings
2) Search for "Interpreter" from the searching tab. Open the interpreter
3) You can now see a plus sign on the right. A click on it will open up a section on the left.
4) In the searching tab, search for numpy or opencv. Click on whichever module you want to install. And then click on the "install package" button on the bottom left. This will install the package for you.
5) Then click save. And run your file that says import cv/cv2.
This should probably do the trick.
Hope it helps!
Is it possible that you have 2 versions of python on your machine and your native pip is pointing to the other one? (e.g. you pip install opencv which installs opencv for python 2, but you are using python 3). If this is so, then use pip3 install opencv
I removed the Anaconda version on my machine, so I just have python 3.7 installed. I removed the python interpreter(Pycharm) and installed it again and the problem got fixed somehow!
I suspect you have two versions of python and the one you're using doesn't have opencv on it, because pip pointed to the wrong one.
A pragmatic solution assuming you're using the python version with conda is to just use conda to install cv2:
conda install -c menpo opencv
A more careful solution is to figure out how to get the pip that points to the python version you're using. On linux I can check that my pip points to my python like this:
:~$ which python
/home/kpierce/anaconda3/bin/python
:~$ which pip
/home/kpierce/anaconda3/bin/pip
So you see the pip and python versions are associated. On windows I suspect you do an analogous thing on the command line like
where python
where pip
And if they don't match, you might try
where python
where pip3
to see if those match. You need to use the pip that points to the correct python version. You can view the python version by entering the python interpreter and running
import sys
sys.version

How to install matplotlib for particular version of Python?

I have installed scipy and numpy, and they are being used with my current, desired version of python 2.7.6 (I am running on OSX Mavericks and had to upgrade.) However, when I pip installed matplotlib, by default it referenced my previous python version, 2.7.5, thus making it troublesome to use (obviously.)
How do I change which version of python matplotlib uses so I can import and use the library?
Thanks.
The way I would solve this problem is like this, firstly one would need to go into your 2.7.6 directory, and under the Scripts folder you will find the pip executable. My suggestion is (because its difficult to debug this kind of people without having all the details) is this:
./pip install matplotlib
And see if this succeeds, otherwise, I suggest using pyenv to manage your python installations.
I suggest you use Macports for installing additional Python versions on OS X. Once Macports is installed, it's fairly easy to install Python 2.7.6. All you'd have to do is:
sudo port install python27
Now, you should be able to get all the libraries you need just as easily, using, too, Macports.
sudo port install py27-numpy
sudo port install py27-scipy
sudo port install py27-matplotlib
Macports should solve all the dependencies and, of course, link the packages to their correct Python versions, avoiding you a lot of headaches.
For a step by step guide on how to set up a nice, functional Python environment, visit: http://jakevdp.github.io/blog/2013/02/02/setting-up-a-mac-for-python-development/

Mac Lion: Issue with opencv and python 2.7.5

I tried everything. Installation from source, via homebrew, macports - none of these options work for me. When I type in
import cv2
I get Segmentation fault in the best case scenario, but typically "no module named cv2" error. I followed all possible guides on stackoverflow from people with similar issues and still can't make it work. I have numpy and scipy installed and they are imported properly without any issues.
Could it be that python 2.7.5 (official) is not compatible with opencv at all? After two days of digging I am close to giving up on opencv at all.
This is how I configured it in the end for the official python 2.7.5 from http://www.python.org
First we need to install opencv via brew:
brew tap homebrew/science
brew install opencv
now add /usr/local/Cellar/opencv/2.4.5/lib/python2.7/site-packages to your PYTHONPATH . I did it by editing bash_profile:
open ~/.bash_profile
add this line in TextEditor:
export PYTHONPATH=/usr/local/Cellar/opencv/2.4.5/lib/python2.7/site-packages:$PYTHONPATH
#Check the version in your directory
Save the file and do:
source ~/.bash_profile
Now everything should be ready.

Can't get OpenCV to work with Python on a Mac

I am having trouble getting OpenCV to work with Python on my Mac.
I have tried installing it with MacPorts and with Cmake (which I installed from MacPorts) using the methods found at here: http://opencv.willowgarage.com/wiki/Mac_OS_X_OpenCV_Port. I also had to download Xcode to make MacPorts work.
I ran sudo port -v install opencv +python27 and it seemed to work fine. However when I tried to import OpenCV in Python using import cv, the module could not be found.
If any of this information helps, I have OSX 10.6.8 Snow Leopard, python 2.7.3, and am trying to install OpenCV 2.4.3. I am not a very experienced programmer so my troubleshooting attempts are falling short of a solution.
Any help is appreciated. Thanks!
I haven't tried the MacPorts installation, but you might want to try this:
import cv2
from cv2.cv import *
I had the same problem, it seems that the OpenCV Python Bindings don't get installed without numpy preinstalled; you can use these commands:
sudo port uninstall opencv
sudo port install py27-numpy
sudo port install opencv +python27
As in this question: How to install Python 2.7 bindings for OpenCV using MacPorts

Categories

Resources