0

I am a python and ubuntu noob and everytime I write a program, I get an error like

    Traceback (most recent call last):
  File "demo.py", line 2, in <module>
    import cv2
ModuleNotFoundError: No module named 'cv2'

I have opencv installed and it exists inside the site-packages directory inside the python directory, and I can use it if I go to that directory. How can I make it so I can run a program in my home directory without getting the module not found error?

Adam
  • 117
  • 2
  • 11
  • It's difficult to know what's going on with this amount of information. It sounds like the python interpreter can't find the module for some reason. My first guess would be that you're using a different python version than the one that has opencv installed. Use the command you were running (e.g. python or python3) to run `python -V` and compare the version to the one in the path of the site-packages directory. – Collin R Jan 13 '18 at 01:30
  • what ide/text editor are you using? – kingJulian Jan 13 '18 at 01:40
  • You're almost definitely using a different version of python compared to where you've installed the packages as @CollinR suggests. Can you please provide the output of `which pip` and `which python`. – sytech Jan 13 '18 at 03:09
  • @CollinR and sytech i think you are correct. `~$ which pip` outputs `/home/adam/miniconda3/bin/pip` and `~$ which python` outputs `/home/adam/miniconda3/bin/python` site-packages is located at `/usr/local/lib/python3.6` how can I change paths? – Adam Jan 14 '18 at 22:08
  • It depends on what you want to do. Basically, since the Python installation you're using is under `/home/adam/miniconda3/bin/python` instead of using the system installation located at `/usr/local/lib/python3.6`, you need to make sure that the opencv package is installed under the miniconda path in the appropriate directory. I'm not familiar with conda/miniconda, but there should be some directory similar to site-packages. Presumably, if you use pip, it will install it in the appropriate location, but you might have to provide a specific path. – Collin R Jan 15 '18 at 21:03
  • Since you're a self-described Python noob, I would recommend using a normal Python install instead of using miniconda, unless you have some specific reason to be doing so. (If so, just ignore this comment.) Since it looks like you already have it installed (which is usually the case with Linux distros), you'll want to make the system install the one called by the "python" or "python3" commands, as described in this answer to another question: https://askubuntu.com/questions/320996/how-to-make-python-program-command-execute-python-3/475815#475815 – Collin R Jan 15 '18 at 21:09
  • I resolved with everyone's help. What I did was found this `export PATH="/home/adam/miniconda3/bin:$PATH"` in my .bashrc file, commented that out and put `export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python3.6/bin:$PATH` because of where pip downloads files to. Everything seems to be working appropriately but if anybody recognizes something I do not, please let me know – Adam Jan 16 '18 at 02:44
  • @Adam if it works, great, but did you try it out before adding the PYTHONPATH line? By commenting out that PATH line, it *should* allow you to use the version of Python already installed on the system, and that *should* mean that pip, and module searching, behave as expected. But I don't know for sure. – Collin R Jan 16 '18 at 19:37

1 Answers1

-2

You will need to add this module directory to the $PYTHONPATH. See the following:

import sys
sys.path.insert(0, "/home/myname/PythonModules")

You could also check which modules were installed in PYTHONPATH typing the following commands:

import sys
print(sys.path)
K0nst4nz4
  • 12
  • 4
  • This could help with the debugging, but if the module's installed in site-packages, there should be no need to added it to PYTHONPATH. – Collin R Jan 13 '18 at 02:20
  • Indeed. It is also a starting point. I considered the user is a noob (as described). I am suspecting the packages were not properly installed. – K0nst4nz4 Jan 13 '18 at 02:25