LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-02-2022, 12:52 AM   #1
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Linux Mint Cinnamon
Posts: 224

Rep: Reputation: 31
Angry 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
 
Old 04-02-2022, 08:24 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

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.

 
1 members found this post helpful.
Old 04-03-2022, 12:57 AM   #3
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Linux Mint Cinnamon
Posts: 224

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by boughtonp View Post
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.
 
Old 04-03-2022, 09:31 AM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by mq15 View Post
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.

 
1 members found this post helpful.
Old 04-04-2022, 12:38 AM   #5
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Linux Mint Cinnamon
Posts: 224

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by boughtonp View Post
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.
 
Old 04-04-2022, 07:40 AM   #6
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by mq15 View Post
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.

 
1 members found this post helpful.
Old 04-05-2022, 04:28 AM   #7
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Linux Mint Cinnamon
Posts: 224

Original Poster
Rep: Reputation: 31
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.
 
Old 04-05-2022, 08:04 AM   #8
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by mq15 View Post
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.)

 
1 members found this post helpful.
Old 04-05-2022, 08:31 AM   #9
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Linux Mint Cinnamon
Posts: 224

Original Poster
Rep: Reputation: 31
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
 
Old 04-05-2022, 08:39 AM   #10
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

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?

 
1 members found this post helpful.
Old 04-05-2022, 12:26 PM   #11
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Linux Mint Cinnamon
Posts: 224

Original Poster
Rep: Reputation: 31
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``.
...
Attached Thumbnails
Click image for larger version

Name:	Screenshot from 2022-04-05 22-21-51.png
Views:	8
Size:	112.5 KB
ID:	38718  
 
Old 04-05-2022, 03:08 PM   #12
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

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.


Last edited by boughtonp; 04-05-2022 at 03:13 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I get the ModuleNotFoundError: No module named 'virtManager' error compiling a custom libvirt-python+virt-manager stack marietto Ubuntu 4 06-03-2021 05:10 PM
[SOLVED] python3, pycharm ModuleNotFoundError: No module named 'wx' kaz2100 Linux - Software 3 05-25-2021 06:02 PM
[SOLVED] ModuleNotFoundError: No module named 'html.entities'; 'html' is not a package (python3 error) kaz2100 Linux - Software 3 05-25-2021 12:48 AM
[SOLVED] apt install broken - ConfigParser import SafeConfigParser ModuleNotFoundError: No module named 'ConfigParser' anon052 Linux - Newbie 16 08-06-2020 06:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration