ModuleNotFoundError: No module named: openCV - python

I have installed opencv for example, and when importing in sublimetext or in a terminal I jump "ModuleNotFoundError: No module named". If I do it from the python idle, it does not

I reproduced your error and was able to fix it as following:
This error often happens when you did not added your Python path correctly.
To check wether your python is installed an configured correctly go to your terminal and type:
python -V
and then:
pip -v
The two versions have to match.
Also simply run:
python
and check wether an interactive shell opens.
If your versions are matching and the the python command works, your python is configured correctly and you can skip the next step. If not follow the steps below, if everthing works skip this:
For Windows try this:
https://superuser.com/questions/143119/how-do-i-add-python-to-the-windows-path
For Mac OS try this:
export PATH=~/path/to/your/python/bin:$PATH
For example:
export PATH=~/anaconda2/bin:$PATH
Now the next thing...
CV2 has two prerequisites there for run:
pip install matplotlib
pip install numpy
and finally:
pip install opencv-python
Now you are ready to go an you can import opencv in your code as following:
import cv2
When you are using pyhton3 use:
python3 test.py
to run your code.
If you want to use python instead of python3 do this:
The last Python you install that registers itself in the environment is the default.
If you would like to have Python 3.x as a default version, then you will need to create environment variable 'PY_PYTHON' and set it's value to 3

Related

ImportError : No Module Named pytesseract

I have installed pytesseract successfully but still getting this error in vscode.
I tried installing tesseract in my venv in vscode. and it was successfully installed. but still , I'm getting this error.
I used a simple code i.e.,
from PIL import Image
import pytesseract as pt
def tesseract():
path_to_tesseract = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image_path="test.jpg"
pt.tesseract_cmd = path_to_tesseract
text = pt.image_to_string(Image.open(image_path))
print(text)
tesseract()
Import errors occur either due to the package not being installed or being installed in a different path.
Since you said you installed pytesseract, I’m guessing it’s the latter. Try running your script in verbose mode with the -v flag to see the path in which Python looks for your packages.
Then you can manually check if pytesseract is installed inside there or somewhere else.
While in your venv, can you try running
pip3 freeze > installed-modules.txt
and share the output here? That should show us what modules are installed in your venv and we can continue from there. Also, do you have the Python Environment Manager extension installed in VS Code?
So sorry but last initial troubleshooting step, in the bottom right corner of VS code, when you have your python file open, does it state that it's using the venv? This can be really telling if its just not utilizing your venv
vs-code demonstration venv
The most common way this ImportError can occur is if pip install refers to a different Python version than VS Code is using, or the virtual environment is not activated in VS Code, or it was not activated when pytesseract was installed.
You can check that pip matches the Python version VS Code is using by running:
pip -V
at the command line, and remember it's output. Then, in VS Code run the following Python script and make sure they match. If they match, then there shouldn't be an issue. If they don't match, some combinations of the virtual environment and the Python Version are not setup correctly.
import sys, subprocess
subprocess.check_call([sys.executable, "-m", "pip", "-V"])
If this is the issue, it can be solved by installed pytesseract by running the following Python script in VS Code.
import sys, subprocess
subprocess.check_call([sys.executable, "-m", "pip", "install", "pytesseract"])
import pytesseract
To explain the command, subprocess.check_call executes a call on the command line, as if you had typed it at the terminal. The difference is uses the full path to the Python Interpreter, from sys.executable (including the virtual environment) to make sure it is setup right.
If this completes without an ImportError, then pytesseract should be working now.

Can't figure out "unable to import [module]" in VSCode

I've recently swapped drives on my notebook and decided on a completely fresh install. When setting up VSCode for a new python project, I installed the latest version from python's website, added the appropriate environment variable to the path, removed the length cap for the pathname too, and all that.
python environment variable added to Path
But still, every time I import NumPy or any other module for that matter, it throws an error saying "unable to import [module]".
vscode error
I don't know what else to do, VSCode even recognizes the interpreter (see image 3).
python interpreter selected in vscode
Tried setting up a venv aswell, end up getting the same error.
This is how I normally set up a project and it always works.
(open directory in VSCode)
Within the VSCode Terminal
python3 -m venv .venv
source .venv/bin/activate
At this time vscode should automatically detect that there is a new virtualenv and ask you to use it.
If it doesn't you can use the CMD+Shift+P (CTRL+Shift+P) search for "Select Python Interpreter" and specify the path.
Did you install Numpy in the virtual environment (if you are using one)?
Try to list your installed packages in vscode terminal
python -m pip list
If numpy does not appear, install numpy with:
python -m pip install numpy
Does it work when you run the code? Or is this just a pylint problem?

How do I run a Python script with sudo? (MAC)

I want to test the keyboard.on_press_key() function, but OSX blocks it naturally. When I try to run it through the terminal using sudo python [pathtofile] I get an import error: "ImportError: No module named keyboard". I tried installing the module using sudo (sudo pip install keyboard) but, even though it installs just fine, I still get the same error. 
What am I doing wrong?
Note that python on macOS refers to the system installation. You should not install packages via pip to the system installation of Python. I'd recommend you to install another Python interpreter e.g. via brew. See www.brew.sh for more info.
Then, it is just a matter of installing Python via
$ brew install python3
After that, simply install your keyboard package again:
$ pip3 install keyboard
That is a error in your script, you probably didnt import the keyboard module before you used. Try adding this in the first lines of your python script:
import keyboard

How to install a new python module on VSCode?

I'm trying to install new python modules on my computer and I know how to install through the terminal, but I wish to know if there is a way to install a new module directly through VSCode (like it is possible on PyCharm)?
I already installed through the terminal, it isn't a problem, but I want to install without be obligate to open the terminal when I'm working on VSCode.
You should open the terminal inside the VSCode and install the modules you want.
something like👇
if that's not you meant, please let me know.
First of all I would advise you to select the current Python version you have. It has been explained here:
VSCode: There is no Pip installer available in the selected environment
Next, you should check is the pip installed in your Python main directory or not, by checking how to do on this website:
https://pip.pypa.io/en/stable/installing/
or this thread
How to use pip with Visual Studio Code
by typing
py -m pip
in your terminal, like
C:\Users\m\Desktop\Python> py -m pip
You should have the list of commands and general options which can be used. One of them is install
On the Python library platform, you always have the command to be copied in order to the installation of package you want.
In your terminal, the initial command should look as:
PS C:\Users\m\Desktop\Python> py -m
to which you should append the command prepared on the Python library platform (by copying it and pasting).
C:\Users\m\Desktop\Python> py -m pip install openpyxl
That's it. The package should be installed in your Python folder, what you will see in the terminal.
If everything is alright, you just need to type
import openpyxl #or other package name, which you downloaded
and use it!
Unfortunately! for now, only possible way is terminal.

Pip not installing Python modules to proper directory

So I downloaded a module, but when I tried to use it in a program, Python threw the error:
"ImportError: No module named markovify"
When I go to try to change my .bash_profile, it shows this:
"# Setting PATH for Python 3.5
The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH"
I've been using 2.7.10 all this time, and Terminal defaults to it. (When I type in Python, that's the version it says I'm using)
I'm trying to get pip to install modules for python 2.7.10 to a different folder.
What's weird is this: I seem to have installed pyparsing with pip and it seems to work. I tried installing markovify and it throws this error message.
What am I missing?
When I go to try to change my .bash_profile, it prints this
That suggests you're using some tool to change your profile. Did you also use that tool to install markovify? You need to be clear about your environment when you have environment questions! :-)
If you installed markovify from the command line, I'd suggest opening a new terminal and running these commands, just to make sure you're working from where you think you are:
$ python --version
Python 2.7.10
$ command -v python
/usr/bin/python
The most likely problem, it seems to me, is that you got a bit tangled up and wound up doing something other than what you intended. From a fresh start, provided you're starting with the environment you want, pip should, you know, Just Work.
I'm trying to get pip to install modules for python 2.7.10 to a different folder.
That's possible to do, but are you sure that's what you want? Usually if you're using the Python interpreter you intend to, and haven't putzed with PYTHON_PATH & friends, when you run pip, it will install to its default location, which sure enough is where Python will look for it when you say to import it.
As an experiment, you might check if markovify was somehow installed for Python 3.5. Try
$ /Library/Frameworks/Python.framework/Versions/3.5/bin/python -c 'import markovify'
(You might also want to check that the Python interpreter is in fact in that folder and has that name.)
When you're working on more than one Python version, you should work on Virtual Environments:
Virtual Env on Hitchhiker's Guide

Categories

Resources