3

I'm running a Django project from PyCharm with the configuration set up to use the Python interpreter from a virtualenv which has a dependency on opencv. The site works fine locally when I run django-admin.py runserver, however I keep getting an "ImportError: No module named cv2" error when I try to run the project directly from the PyCharm IDE.

Has anyone else had this issue with PyCharm and opencv?

philrabin
  • 809
  • 2
  • 11
  • 21
  • I did not used PyCharm but if you got the same `sys.path` setup then there should not be a difference. You can try to `print cv2.__file__` in `django-admin.py runserver` and you can verify if it's on the `print sys.path` in your PyCharm IDE. Most probably you can setup PYTHONPATH in your IDE. – ddzialak Jun 11 '12 at 21:41
  • From the command line in python I got `/usr/local/lib/python2.7/site-packages/cv2.so` for `cv2.__file__`. I tried `export PYTHONPATH=/usr/local/lib/python2.7/site-packages/cv2.so:$PYTHONPATH` with no luck. I also tried adding that last export command as one of the environment variables in the PyCharm configuration, also with the same error. – philrabin Jun 11 '12 at 22:55
  • Try setting your path to just site-packages. Including `cv2.so` in the path won't give you what you want as it's not a directory – Daenyth Jun 12 '12 at 00:11
  • fyi, I faced a similar problem with using opencv within virtualenvs and the answer given [here](http://stackoverflow.com/questions/9592389/is-it-possible-to-run-opencv-python-binding-from-a-virtualenv/12043136#12043136) helped me. – Matt Apr 02 '13 at 08:42

2 Answers2

10

In the end I ended up having to set an environment variable directly in the Pycharm Edit Configurations -> Run/Debug Configurations -> Environment Variables panel. I added the following option after you hit the edit button: set name to PYTHONPATH and value to /usr/local/lib/python2.7/site-packages:$PYTHONPATH which should display in the input box after editing as PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH. Also, I made sure to log out and log back in of osx which also worked for a couple other path related issues.

philrabin
  • 809
  • 2
  • 11
  • 21
  • exactly what I was looking for! Using VirtualEnv on a windows machine, for some reason Pycharm wasn't finding my matplotlib package to enable autocompletion for it, but it was running the matplotlib imports just fine. Added the PythonPath to my virtual environment site-packages (didn't include that $PYTHONPATH bit at the end) and now it works perfectly – TKoL May 14 '14 at 22:41
  • God bless you with this helpful answer :) – A.M. May 15 '15 at 20:50
1

I'm not quite sure if this works for you guys but it works for me. In my case, it seems to me that I installed OpenCV to work with the default Python arriving with OS X. I remember I tried to install Python 2.7.5 and Python 3 in my Mac as well, I see them when I chose my Python interpreter for Pycharm. And all of them didn't let me import module cv2. So I change to the default Python2.7.2 (/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python). Then, in File/DefaultSettings/Project Interpreter/Python Interpreter, click on the Python interpreter that's been added (Python 2.7.2), click on Paths and locate to "/usr/local/bin/python2.7/site-packages"and add it. Click the blue refresh button, apply and ok. Then it works, both with import and autocompletion.

Regards,

tnq177
  • 583
  • 1
  • 8
  • 19