[SOLVED] opencv installed, yet ModuleNotFoundError: No module named 'cv2'
Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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;
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.
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
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.)
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?
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.