LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   opencv installed, yet ModuleNotFoundError: No module named 'cv2' (https://www.linuxquestions.org/questions/linux-software-2/opencv-installed-yet-modulenotfounderror-no-module-named-cv2-4175710304/)

mq15 04-02-2022 12:52 AM

opencv installed, yet ModuleNotFoundError: No module named 'cv2'
 
Hello there,
Although I've installed everything, I am facing this problem;
Quote:

ModuleNotFoundError: No module named 'cv2'
To help diagnose, please examine the following.
Code:

mq15@BigBang:~$ uname -a
Linux BigBang 5.4.0-26-generic #30-Ubuntu SMP Mon Apr 20 16:58:30 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
mq15@BigBang:~$ python3 -V
Python 3.10.2
mq15@BigBang:~$ pip3 install opencv-python
Requirement already satisfied: opencv-python in ./.local/lib/python3.8/site-packages (4.5.5.64)
Requirement already satisfied: numpy>=1.14.5; python_version >= "3.7" in ./.local/lib/python3.8/site-packages (from opencv-python) (1.22.3)
mq15@BigBang:~$ ls /usr/local/lib/
libpython3.10.a  pkgconfig  python2.7  python3.10  python3.8
mq15@BigBang:~$ python3
Python 3.10.2 (main, Feb 14 2022, 18:10:33) [GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
>>>

Best Regards

boughtonp 04-02-2022 08:24 AM


 
You appear to have "pip3" connected to Python 3.8, but "python3" connected to Python 3.10

The output of "which -a pip3 python3" should confirm that.


To install with pip into the expected Python version, use "python3 -m pip ..." instead of "pip3 ..."

Or, if you want to run Python 3.8, you can use the absolute path which the above "which" statement reveals.


mq15 04-03-2022 12:57 AM

Quote:

Originally Posted by boughtonp (Post 6343357)
You appear to have "pip3" connected to Python 3.8, but "python3" connected to Python 3.10

The output of "which -a pip3 python3" should confirm that.


To install with pip into the expected Python version, use "python3 -m pip ..." instead of "pip3 ..."

Or, if you want to run Python 3.8, you can use the absolute path which the above "which" statement reveals.


Thanks, boughtonp

Code:

mq15@BigBang:~$ which -a pip3 python3
/usr/bin/pip3
/usr/local/bin/python3
/usr/bin/python3

To me, it is not yet clear as pip3 is connected to which python? 3.8 or 3.10. And what should I do in any case? Well, I am not interested in python 3.8. I want only python 3.10 as it will be the latest.

Code:

mq15@BigBang:~$ python3 -m pip install opencv-python
/usr/local/bin/python3: No module named pip

Here, I think I am not clear on how to run this command.

Any help will be highly appreciated.

boughtonp 04-03-2022 09:31 AM

Quote:

Originally Posted by mq15 (Post 6343521)
Code:

mq15@BigBang:~$ which -a pip3 python3
/usr/bin/pip3
/usr/local/bin/python3
/usr/bin/python3


Ok, so your distro has v3.8 of python3 and pip3 installed in the standard location.

You also have Python 3.10 (without Pip) installed to /usr/local/bin, which is why it takes precedence when entering a non-absolute python3.


Quote:

To me, it is not yet clear as pip3 is connected to which python? 3.8 or 3.10.
This is shown in what you provided in your first post:
Code:

mq15@BigBang:~$ pip3 install opencv-python
Requirement already satisfied: opencv-python in ./.local/lib/python3.8/site-packages (4.5.5.64)


Quote:

And what should I do in any case? Well, I am not interested in python 3.8. I want only python 3.10 as it will be the latest.
Well it's not necessarily as simple as just installing 3.10 - you need to have the version that software you're using requires (because Python devs are willing to break backwards compatibility between 3.x versions).

Unless you have a specific identified need for a particular version, it's simplest to just stick with what's provided in your distro's software repositories, and not worry about new/shiny stuff until your distro includes that version in its repos.

However, if you do have such a need, you probably want to look into pyenv which allows having multiple different versions installed, with specific versions for different projects. That way you can keep your distro's version as the default, but also use other versions for specific projects you might have, and pyenv will keep the python3 and pip3 commands in-sync.


mq15 04-04-2022 12:38 AM

Quote:

Originally Posted by boughtonp (Post 6343626)
Unless you have a specific identified need for a particular version, it's simplest to just stick with what's provided in your distro's software repositories, and not worry about new/shiny stuff until your distro includes that version in its repos.

However, if you do have such a need, you probably want to look into pyenv which allows having multiple different versions installed, with specific versions for different projects. That way you can keep your distro's version as the default, but also use other versions for specific projects you might have, and pyenv will keep the python3 and pip3 commands in-sync.

Thank you very much boughtonp.
In fact, I don't have any specific need for a particular version. I'm just learning some python coding. So, how should I know which version is provided in my distro's software repositories, and how to "stick" to that, in order to avoid the complaint;
Quote:

ModuleNotFoundError: No module named 'cv2'
Best Reards.

boughtonp 04-04-2022 07:40 AM

Quote:

Originally Posted by mq15 (Post 6343783)
So, how should I know which version is provided in my distro's software repositories

Generally speaking, I think for most distros, the files in /usr/bin will be provided by the distro, whilst files from other sources will go to other locations.

You can also check your distro's package management - e.g. for Mint (and other Debian-based systems) "apt info python3" and see what that says regarding version.

To find out which package is responsible for a file, you can use dpkg, e.g: "dpkg -S /usr/bin/python3 /usr/bin/pip3 /usr/local/bin/python3"


Quote:

and how to "stick" to that, in order to avoid the complaint
If you explicitly refer to "/usr/bin/python3" it will use that version - you can put this in the scripts (but that would be less portable), or simple start the script on the commandline "/usr/bin/python3 scriptname"

To avoid doing that you'd need to remove the instance at "/usr/local/bin/python3" - how will depend on how it was installed - the above dpkg command will confirm if there's a known package involved, if not renaming the file to "/usr/local/bin/python3_unused" would probably work.


mq15 04-05-2022 04:28 AM

Thank you very much boughtonp for bearing with me.

Quote:

mq15@BigBang:~$ python3
Python 3.10.2 (main, Feb 14 2022, 18:10:33) [GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
>>> exit()
mq15@BigBang:~$ /usr/bin/python3
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> exit()

Clearly, /usr/bin/python3 scriptname is working. Now, for a permanent solution, I'hv two options:
Quote:

to remove the instance at "/usr/local/bin/python3"
or
Quote:

renaming the file to "/usr/local/bin/python3_unused
For option-1, removing, please consider the output of the dpkg command;
Code:

mq15@BigBang:~$ dpkg -S /usr/bin/python3 /usr/bin/pip3 /usr/local/bin/python3
python3-minimal: /usr/bin/python3
python3-pip: /usr/bin/pip3
dpkg-query: no path found matching pattern /usr/local/bin/python3

So, what does the output of dpkg command reveal about how to remove the instance at /usr/local/bin/python3?

For option-2, renaming, I am not sure how to do it either. Please consider;
Code:

mq15@BigBang:~$ rename /usr/local/bin/python3 /usr/local/bin/python3_unused
Use of /c modifier is meaningless without /g at (eval 8) line 1.
Regexp modifiers "/l" and "/a" are mutually exclusive at (user-supplied code), at end of line
Regexp modifier "/l" may not appear twice at (user-supplied code), at end of line

Best Regards.

boughtonp 04-05-2022 08:04 AM

Quote:

Originally Posted by mq15 (Post 6344083)
So, what does the output of dpkg command reveal about how to remove the instance at /usr/local/bin/python3?

It means it wasn't installed via the standard package mangement system (apt/dpkg), so you wont break any standard packages by removing it.

(There's still a potential risk of it breaking software installed by non-standard means, but you'd presumably know if you'd done that; either way keep a note that you've renamed it in case something crops up in future, but if you're not sure then it's probably fine.)


Quote:

For option-2, renaming, I am not sure how to do it either. Please consider;
Code:

mq15@BigBang:~$ rename /usr/local/bin/python3 /usr/local/bin/python3_unused
Use of /c modifier is meaningless without /g at (eval 8) line 1.
Regexp modifiers "/l" and "/a" are mutually exclusive at (user-supplied code), at end of line
Regexp modifier "/l" may not appear twice at (user-supplied code), at end of line


The rename command uses special syntax. Can be useful at times for complex renames, but in this instance it's easier to just use mv instead.

Also, to avoid typing the filename twice you can use shell expansions, e.g: "mv /usr/local/bin/python3{,_unused}" is equivalent to "mv /usr/local/bin/python3 /usr/local/bin/python3_unused".

(If that gives a permission denied error you'll need to be root, using either su or sudo.)


mq15 04-05-2022 08:31 AM

Thank very much dear boughtonp

Quote:

mv /usr/local/bin/python3{,_unused}
worked without a permission issue.

One more thing. May I remove the instance at /usr/local/bin/python3 like this;
Quote:

apt-get remove /usr/local/bin/python3
or
Quote:

apt-get purge /usr/local/bin/python3
Best Regards

boughtonp 04-05-2022 08:39 AM


 
APT works with packages rather than files, so that isn't going to work.

You'd need to identify how it was installed so you can determine where it's dependencies were placed in order to purge it - was there a script that was executed and/or a tutorial followed to install that version?


mq15 04-05-2022 12:26 PM

1 Attachment(s)
As far as I rememer, I downloaded Python-3.10.2.tgz from https://www.python.org/. Extracted Python-3.10.2.tgz. Found a file README.rst. And followed its build instructions.
Quote:

Build Instructions
------------------

On Unix, Linux, BSD, macOS, and Cygwin::

./configure
make
make test
sudo make install

This will install Python as ``python3``.
...

boughtonp 04-05-2022 03:08 PM


 
Hrm, the Python configure script is several thousands of lines long(!), yet doesn't appear to include an "uninstall" option. :/

I guess cd into that directory and see if "grep -i uninstall ./*" brings up anything? (Probably not.)

The configure script generates a file named "Makefile", which is what the "make" command uses - it's possible that despite the configure script being obscenely long the generated file might still be short, so even without an uninstall option it might be possible to reverse the install option - open it with a text editor to have a look, and locate the install part - hopefully there's only a handful of commands copying files, in which case those can then be undone to manually uninstall it.



All times are GMT -5. The time now is 09:46 PM.