opencv python osx - python

(Step 1)
I'm trying to get openCV to run from python on my mac using the MacPorts install http://opencv.willowgarage.com/wiki/Mac_OS_X_OpenCV_Port, and also trying to follow The Petite Geek's guide:
sudo port -v install opencv +python26
It runs for about 10 minutes without errors.
(Step 2)
I download ctypes-opencv source and demo files. I navigate to the src directory and run:
sudo python setup.py install
I see like 50 lines almost all of the form: copying ... -> ..., which looks good to me. No errors here.
(Step 3)
I add export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib to the end of my ~/.profile.
(Step 4)
I open a new terminal to test my install. From my home folder:
$ python
Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cv
>>>
Does not work.
I read somewhere that opencv installs python bindings with the default version of python for OSX, and I am probably running a non-default version, but this is not actionable information to me.

I struggled for a while with Python on Mac. Here is how I finally (and easily!) installed it. Remove all the things Python you have on there already. They will probably be located at /Library/Frameworks/Python.Framework and /opt/local/var/macports/software/py26*
First download Python with Macports.
sudo port install python27
Then make sure your system is using this version with:
sudo port select --set python python27
Next install numpy with:
sudo port install py27-numpy
Now install opencv:
sudo port install opencv +python27
Now edit your ~/.bash_profile with:
sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit ~/.bash_profile
or
open -t ~/.bash_profile
and add the line:
export PYTHONPATH=/opt/local/var/macports/software/opencv/2.2.0_0+python27/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages:$PYTHONPATH
or where ever your version of the cv.so file is hidden....
Now restart terminal and try:
%python
>>>import cv
I'm using Netbeans for opencv and python and it works really nice. Good luck.

$ brew search opencv
homebrew/science/opencv
$ brew install homebrew/science/opencv
after installed, there is warning:
==> Caveats
If you need Python to find the installed site-packages:
mkdir -p ~/Library/Python/2.7/lib/python/site-packages
echo '/usr/local/lib/python2.7/site-packages' > ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth
so, just do
mkdir -p ~/Library/Python/2.7/lib/python/site-packages
echo '/usr/local/lib/python2.7/site-packages' > ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth

If you notice the first line output when running python, you'll see that you're still using the Apple-supplied Python interpreter. Try installing and using the python-select package in MacPorts and then try the instructions again starting from step 2.
Also make sure you followed all of the steps when installing MacPorts so that /usr/local/bin is on $PATH.

Another "hack" I found during my struggles using CMake (but maybe the problem is the same with ports) : it appears that the python modules location has been duplicated on my Mac OS Lion, for a reason I can't explain.
CMake wants to put the "cv" module here :
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Whereas my default Python interpreter is looking here (thanks PyCharm for telling me) :
/Library/Python/2.7/site-packages
Moving both cv2.so and cv.py files to the second location, did the trick for me. I don't know if this is the cleanest way.
Hope it can help some googlers !

Related

import jwt ImportError: No module named jwt

I have been trying to run this project
https://github.com/udacity/FSND-Deploy-Flask-App-to-Kubernetes-Using-EKS
I installed all the dependencies.
I still did not make any adjustments. I need to run it first
but I get this error when I type the command
python main.py
this is the error i get:
Traceback (most recent call last):
File "main.py", line 8, in <module>
import jwt
ImportError: No module named jwt
I worked with similar errors before and managed to solve them but not with this one I could not figure out the source of the problem
Check if PyJWTY is in the requirements file or if is installed in you system, using: pip3 install PyJWT
You could also face this error if you have running on your machine two versions of python. So the correct command will be python3 main.py
I have hit the same issue with pyjwt 2.1.0 which was clearly installed in my venv as well as globally. What helped was to downgrade it to version 1.7.1
pip install "PyJWT==1.7.1"
run the app and then to reinstall newest version 2.1.0
pip install "PyJWT==2.1.0"
And the issue disappeared.
This project has requirements that need to be installed for it to work. These can be installed via pip, pip install -r requirements.txt (I've linked to the requirements file in the project), which you can read more about here.
What worked for me was using import jwt instead of import PyJWT. I am using version PyJWT-2.3.0.
jwt image on vscode
As you can see no errors in the above screenshot. The app runs without import errors.
Image of terminal
Faced the same issue. Am using a guest VM running ubuntu 16.04.
I have multiple versions of python installed - both 3.5 and 3.7.
After repeated tries with and without using virtualenv what worked finally is:
Create a fresh virtual environment using :
priya:~$ virtualenv -p /usr/bin/python3.7 fenv
Activate the virutal environment :
priya:~$ source ./fenv/bin/activate
Note : You can find the path for python3.7 by using whereis python:
priya:~$ whereis python
python: /usr/bin/python /usr/bin/python3.5m /usr/bin/python3.5 /usr/bin/python3.7 /usr/bin/python3.5m-config /usr/bin/python3.5-config /usr/bin/python2.7 /usr/bin/python3.7m /usr/bin/python2.7-config /usr/lib/python3.5 /usr/lib/python3.7 /usr/lib/python2.7 /etc/python /etc/python3.5 /etc/python3.7 /etc/python2.7 /usr/local/lib/python3.5 /usr/local/lib/python3.7 /usr/local/lib/python2.7 /usr/include/python3.5m /usr/include/python3.5 /usr/include/python2.7 /usr/share/python /usr/share/man/man1/python.1.gz
Referenced link is : https://stackoverflow.com/questions/1534210/use-different-python-version-with-virtualenv#:~:text=By%20default%2C%20that%20will%20be,%2Flocal%2Fbin%2Fpython3
For your project - FSWD Nanodegree -
After you have activated your virtualenv, run pip install -r requirements.txt
You can test by :
(fenv) priya:FSND-Deploy-Flask-App-to-Kubernetes-Using-EKS :~$ python
Python 3.7.9 (default, Aug 18 2020, 06:24:24)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
import jwt
exit()
pip3 install flask_jwt_ex.. I was doing this without sudo. And then I was working on the program as sudo.
You have to have only PyJWT installed and not JWT. Make sure you uninstall JWT (pip uninstall JWT) and install PyJWT (pip install PyJWT)

Installing hyperledger indy node code fails on python3 not being default on Mac

I am installing Hyperledger Indy (sovrin) self-identity software per these instructions.
I have Python2.7 installed via Anaconda 3. I also have Python3.6.3 installed that I downloaded and installed from here.
I'm trying to install some other software that has a dependency for Python > 3.5.
I've tried several methods to change my default Python:
Per this SO I set it manual in current terminal window:
alias python='python3'
Per same SO I vi .bash_profile and added:
alias python='python3'
then source ~/.bash_profile
Set a link to Python3:
bc-computer:~ momi$ unlink /usr/local/bin/python2
bc-computer:~ momi$ ln -s /usr/local/bin/python3 /usr/local/bin/python
but still continue to get the same error:
bc-computer:~ momi$ pip install indy-node-dev
Collecting indy-node-dev
Using cached indy-node-dev-1.2.227.tar.gz
Complete output from command python setup.py egg_info:
FAIL: Requires Python 3.5 or later, but setup.py was run using 2.7.14
NOTE: Installation failed. Run setup.py using python3
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/3f/sh6dr8wx6w720b1_w38f_fh00000gq/T/pip-build-ecZnYY/indy-node-dev/
I also tried setting up a python3 test environment per this:
python3 setup.py test
And got this error:
> /usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/Resources/Python.app/Contents/MacOS/Python:
> can't open file 'setup.py': [Errno 2] No such file or directory
Please note that I tried this suggestion which seems to be the non-duplicate answer but didn't work for me:
The safest way is to set an alias in ~/.bashrc:
alias python=python3
My environment:
uname -msra
Darwin bc-computer.local 17.2.0 Darwin Kernel Version 17.2.0: Fri Sep 29 18:27:05 PDT 2017; root:xnu-4570.20.62~3/RELEASE_X86_64 x86_64
My OS:
High Sierra 10.13.1
which -a python
/Users/momi/anaconda2/bin/python
/usr/local/bin/python
/usr/bin/python
Thank you
Ok the solution was to use pip3 rather than pip for my install command per Mike Mueller's answer here:
pip3 install indy-node-dev
I assume that pip3 points to python3x rather than 2x.
I have also installed Hyperledger Indy SSI VC using MacOs Python 3.6.3. It is working for for me. I could demo the VON Network. It seems that your machine environment still points to Python 2.7. There are different ways by which we can point to Python 3.6.3 through virtual environment. You can try those options.

Using Python with homebrew on OS X

I have been using Mac python for a while, and I decided to teach myself matplotlib, because I want to have the experience with some common modules. I hear from everyone that once you get into non-standard modules, it's best to use python threw homebrew, so you have access to pip and not easy_install. After running: $brew install python --with-brewed-openssl, $brew install python3 --with-brewed-openssl, and $pip install matplotlib, I go to the python shell with $python3. Once there, I run import matplotlib.pyplot as plt, and get the following:
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'matplotlib'
>>>
I have a feeling I'm still using the mac python, and not the brewed python, and I have tried editing the path, but it isn't working, even though I am running $source ~/.bash_profile after every edit. Just in case I am editing the path wrong, I will post the file contents below:
PATH=/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
PATH=/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
export MSF_DATABASE_CONFIG=/usr/local/share/metasploit-framework/database.yml
PATH="/Users/ericmarkmartin/.apportable/SDK/bin:$PATH"
PATH="/Users/ericmarkmartin/.apportable/SDK/bin:$PATH"
# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
PATH=“/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/bin:${PATH}”
export PATH
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin
PATH=/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin
PATH=/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin:/Users/ericmarkmartin/.rvm/bin
PATH=/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin:/Users/ericmarkmartin/.rvm/bin
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
# Setting PATH for MacPython 2.6
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.6/bin:${PATH}"
export PATH
export PATH=/usr/local/bin:/usr/local/sbin:${PATH}
$pip3 install matplotlib returns the following
RuntimeError: Broken toolchain: cannot link a simple C program
----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /private/var/folders/sr/j0s763cj06v38c6btg6v7k7r0000gn/T/pip_build_ericmarkmartin/matplotlib
Storing debug log for failure in /Users/ericmarkmartin/.pip/pip.log
If more information is needed, please feel free to ask, and thank you so much in advance for the help!
pip usually installs modules for python2 if you have both python versions in your system, so for python3 you will probably need to use pip3.
For reference:
https://docs.python.org/3.4/installing/index.html
You can check where a command is coming from by using which python . In this case you are adding /opt/local/bin/ to your path, but then you are adding /Library/Frameworks/Python.framework/Versions/2.6/bin and /usr/local/bin/ before them (you are prepending in each case). $PATH stops on the first successful match, so you won't pick up the python from /opt/local/bin . Try moving that line to the end of your bashrc or adding another line prepending it to the front of $PATH.

Install python module to non default version of python using .sh

I have a problem similar to this post: Install python module to non default version of python on Mac, so I am aware of those solutions, but they do not work for me.
I am installing M2Crypto on CentOS, which means I much use fedora_setup.sh build followed by fedora_setup.sh install in order to install on my architecture.
Unfortunately, the default Python version is 2.6, but I use 2.7. How do I execute the build and install commands so that they build and install to Python2.7 site-packages? Is there a simple command I don't know? I've been searching around here: http://docs.python.org/2/install/ in the Python Docs, but I don't see anything about .sh scripts?
You should run your scripts in a virtualenv created for your app's environment. This creates an isolated environment that uses the Python interpreter you created the virtualenv with, but with its own set of libraries.
# create the virtualenv folder: M2Crypto-venv
python2.7 virtualenv.py --distribute M2Crypto-venv
# activate the virtualenv, changing environment variables to use its Python interpreter
. M2Crypto-venv/bin/activate
# see how the current python has changed
which python # should be M2Crypto-venv/bin/python
python --version # should be 2.7
# after activating, run your install scripts
If you're using mod_wsgi or something similar to serve content, you'll want to modify your WSGI file to activate the virtualenv before doing anything else (adapted from mod_wsgi instructions):
import os.path
virtualenv_path = '/path/to/M2Crypto-venv'
activate_this = os.path.join(virtualenv_path, 'bin/activate_this.py')
execfile(activate_this, dict(__file__ = activate_this))
# rest of the WSGI file...
This was an incredibly difficult answer to come by, but the support team at Webfaction where I am hosted were spectacular in assisting me. Directly from the support I was given:
First build swig,
wget http://prdownloads.sourceforge.net/swig/swig-2.0.8.tar.gz
tar -xf swig-2.0.8.tar.gz
cd swig-2.0.8
./configure --prefix=$HOME
make
make install
Than get m2crypto,
svn checkout http://svn.osafoundation.org/m2crypto/tags/0.21/ m2crypto-0.21
cd m2crypto-0.21/
Edit fedora_setup.sh from this
SWIG_FEATURES=-cpperraswarn python setup.py $*
to this,
SWIG_FEATURES=-cpperraswarn python2.7 setup.py $*
Then build, then install,
./fedora_setup.sh build
./fedora_setup.sh install --prefix=$HOME
[me#web342 lib]$ python2.7
Python 2.7.5 (default, May 16 2013, 20:16:09)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import M2Crypto
>>> print M2Crypto
<module 'M2Crypto' from '/home/me/lib/python2.7/site-packages/M2Crypto-0.21-py2.7-linux-x86_64.egg/M2Crypto/__init__.pyc'>
Obviously, substitute your own details throughout. Hope this helps the next guy trying to install M2Crytpo using fedora_setup to a non-default python version.

numpy does not work through terminal

dI have installe numpy on ubuntu by executing
sudo apt-get install python-numpy
while executing on terminal I get this error.
>>> import numpy as np
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>>
why this is happening? I tried many a times by going through internet but I couldnt find a solution.Could you please tell me how to solve this?
Edit1:
I came to know that I have to install numpy for the python version which I run on terminal, using pip.. Python 2.7.3 runs when I enter 'python' on terminal. So that means I have to install numpy for python 2.7.3. Can someone guide me how to do it? I couldnt figure it out by myself. BTW I am using Ubuntu 12.04 if that helps.
Edit 2:
I did some more digging into this.. my /usr/lib contains two directories python2.7 and python3.While Python2.7 directory consists of a large number of files and sub directories,python3 directory has only dist-packages subdirectory which consists of deb_conf.py anf lsb_release.py..I think I tried python3 few months back and then removed it..But right now python2.7 is the only thing i am having.
Edit 3:
SO here are the outputs of the commands you asked me to enter
~$ virtualenv --no-site-package -p /usr/bin/python2.7 my_env
Running virtualenv with interpreter /usr/bin/python2.7
The --no-site-packages flag is deprecated; it is now the default behavior.
New python executable in my_env/bin/python2.7
Not overwriting existing python script my_env/bin/python (you must use my_env/bin/python2.7)
Installing distribute..............................................................................................................................................................................................done.
~$ source my_env/bin/activate
~$ pip install numpy
last command gave a generated a lot of logs which ended with something like this..
Creating build/scripts.linux-i686-2.7/f2py2.7
adding 'build/scripts.linux-i686-2.7/f2py2.7' to scripts
changing mode of build/scripts.linux-i686-2.7/f2py2.7 from 664 to 775
changing mode of /home/sandeep/my_env/bin/f2py2.7 to 775
Successfully installed numpy
Cleaning up...
After all these I tried to run python again and this is the output.
~$ python
Python 2.7.3 (default, Jan 20 2013, 21:40:19)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>>
In case nothing works.
Install python-virtualenv if it's not yet done.
Create a virtual env
virtualenv name
Start the virtualenv
source name/bin/activate
Install numpy with easy_install or pip
Profit
Note:
Virtualenv activation has to be done everytime. But you can make that task easier with virtualenv wrapper.
http://virtualenvwrapper.readthedocs.org/en/latest/
There are a lot of reasons to use virtualenv instead of ubuntu packages. In some way, I recommend not touching as much as possible the "OS" python. And if you need it for a project, use virtualenv. Python in virtualenv won't mess with other apps and you don't have to use sudo to install new packages.

Categories

Resources