I'm getting an error while using PyCharm which doesn't allow me to import the pillow module even though I have it installed as a package in the project interpreter. Any help is greatly appreciated!
http://imgur.com/a/DfjC3
While the name of the package is pillow, it is a replacement for PIL and uses the PIL for the name of the base module
the usual way to use pillow is
from PIL import Image
im = Image.open("filename")
See the tutorial, and the documentation
You try to run code with default Python interpreter (/Library/Frameworks/Python.framework/Versions/3.2/bin/python3). You need to configure PyCharm to run Your code with anaconda (~/anaconda/bin/python)
And now (Like #JamesK say) read Pillow tutorial and documentation:
import PIL not import Pillow
For anybody still having trouble with this, I did the following which solved my problem.
Open up your Project Interpreter (⌘ + , on Mac).
At the bottom of this page you'll see the + symbol to the left of the anaconda logo. This will create a pop-up that allows you to search for available packages.
In this new window, search for 'Pillow'.
Click and Install Package.
You should now be able to use "from PIL import Image" or "import Pillow as pil" etc.
After running this command on your terminal
pip install pillow
and you are sure it was installed, but still having same problem of PIL module not found.
Go to your IDE and make sure existing interpreter is set to python interpreter and not anaconda
Related
I have pyPDF2 installed on my interpreter that has venv and uses python 3.6.6 but i'm not able to import it. What am I doing wrong? I use pycharm.
Try using
import PyPDF2
Python is case sensitive.
I found the solution. But before I answer that, I have to re-iterate that -
NO, Pycharm wasn't detecting the module even when I tried
import pyPDF2
Solution:
Uninstall pyPDF2, re install it, and this time install it through the
IDE through that pop-up (refer to example screenshot)
I found this weird as I don't usually install modules this way.
I've looked around and people seem to have similar problems but none described my case exactly, and solutions that worked for them didn't seem to work for me (or there was no answer to the question at all).
Using pycharm, after having installed opencv-python and opencv-contrib-python I noticed that import cv2 works, but when I tried accessing cv2.imread() pycharm complained about not being able to find it.
So I went to the cv2 init file, which looks like this:
import importlib
from .cv2 import *
from .data import *
# wildcard import above does not import "private" variables like __version__
# this makes them available
globals().update(importlib.import_module('cv2.cv2').__dict__)
Pycharm detects an unresolved reference on the from .cv2 import * line and I imagine the same problem happens on the last line - I tried doing the following in a python console:
import cv2
print(__version__)
But I got a NameError, which seems to confirm my suspicion.
As I wrote, I have tried installing opencv-contrib-python but that didn't seem to do anything and frankly I'm already out of ideas.
Notes:
- I'm on Windows 10 x64.
- I'm using Python 3.6 x64.
- I have a virtual environment set up on my Pycharm project.
I'm no expert but the following line worked for me:
import cv2.cv2 as cv2
Everything seems to work afterwards. Autocompletion is also back
Have you installed opencv via terminal?
For example, like this.
$ pip install opencv-python
$ pip install opencv-contrib-python
I also experienced the same problem.
If you use pycharm, you should install opencv via pycharm.
File -> Settings... -> Project interpreter -> +
I got this same issue. You need to try a couple of things.
You need to simply import cv2 instead of cv2.cv2. Simply write "import cv2"
If you have installed any other library such as cv before, then uninstall it first.
The library that we need to install is opencv-python
You need to install it via IDE not with the terminal. Steps are as follows:
File -> Settings -> (Click on your project) -> Project Interpreter -> + -> (Type opencv-python) -> Download and install it -> It should work now.
From Python Interpreter download the version of opencv-python 4.5.4.60 and opencv-contrib-python 4.5.5.64.
I'm just getting started with PyCharm, python, and OpenCV, and I'm trying to set up my environment. I've installed all the necessary packages and I import OpenCV like so:
import cv2
However, this does not autocomplete and shows warnings that the method may be missing when called, BUT if I import like so:
import cv2.cv2
autocomplete does work, but running produces the following error:
Traceback (most recent call last):
File "C:/Users/dunnj/PycharmProjects/TransformApps/transformapps/blackwhite.py", line 1, in <module>
import cv2.cv2 as cv2
AttributeError: 'module' object has no attribute 'cv2'
My Configuration:
PyCharm 2021.2.3 on macOS 11.6
Python 3.9.7 running in a Virtual Environment(VE)
opencv-python 4.5.4.58 installed into the VE via pip using the PyCharm Terminal window
Steps that worked for me to get autocompletion working:
tldr: Update python interpreter settings to point to <full path to venv>/lib/python3.9/site-packages/cv2
In preferences, Select Python Interpreter
Click the setting icon ( gear on right of box that display your Python Interpreter and select Show All
A list of all your configured Interpreters is show with your current interpreter already hi-lighted.
With your interpreter still highlighted, click the Icon that shows a folder and subfolder at the top. Tool tip should say "Show Paths for Selected Interpreter.
Click the + button and add the following path:
<full path to the venv>/lib/python3.9/site-packages/cv2
The .../python3.9... will be different if you are using a different Python Version.
Click Ok until you are back to the main IDE window.
This has worked in three different Virtual environments for me so far. For two of those, I had to restart the IDE for the completions to show up. The remaining one did not require a restart and worked immediately.
Credit to ingolemo from r/learnpython. I was stuck on this for ages and it drove me mad so I'm here sharing.
My OpenCV was installed by using the wrapper opencv-python package
The sys.modules hacking that that module is doing is the source of the
problem. Pycharm doesn't exactly import modules in order to know
what's inside of them, so messing with the imports dynamically like
that confuses pycharm greatly. It's not pycharm's fault, the
opencv-python maintainer should have used a star import rather than
that messy import hack. You should be able to work around the problem
using the technique you stumbled upon. All you have to do is catch and
ignore the error under normal operation:
import cv2
# this is just to unconfuse pycharm
try:
from cv2 import cv2
except ImportError:
pass
just execute the following commands in your project working environment.
pip uninstall opencv-python
pip install opencv-python==4.5.4.60
The proposed import solution did not work for me.
I had exactly this problem with OpenCV 4.2.0 compiled from sources, installed in my Conda environment and PyCharm 2020.1.
I solved this way:
Select project interpreter
Click on the settings button next to it and then clicking on the Show paths for selected interpreter
added the directory containing the cv2 library (in my case in the Conda Python library path - e.g. miniconda3/lib/python3.7/site-packages/cv2/python-3.7). In general check the site-packages/cv2/python-X.X directory)
Following Workaround 2 from the JetBrains issue tracker (https://youtrack.jetbrains.com/issue/PY-54649) helped me:
In PyCharm open from menue FILE - SETTINGS
Go to PROJECT:<your_project_name> and select PYTHON INTERPRETER
Click on the gear symbol next to the interpreter path and select SHOW ALL.
Make sure the correct interpreter is selected.
Click on that icon that looks like a folder tree (on the top)
Click on the "+" icon
Select the folder where the opencv package is located
normally (if you installed it via package manager) you will find it in:
<your_project_path>\venv\Lib\site-packages\cv2
Click OK (twice)
Wait for updating skeletons
Installing Jedi solved this problem for me.
You can use pip install jedi in terminal
You can find more info about jedi here: https://pypi.org/project/jedi/
i had the same problem.
i used
import cv2 as cv2
and after that both importing meth
try
try:
import cv2.__init__ as cv2
except ImportError:
pass
If you are using virtualenv, then mark the virtualenv directory as excluded in your project structure in Pycharm project settings.
Encountered this before.
find "cv2.cp38-win_amd64.pyd" in "Lib\site-packages\cv2" path.
Copy it to "DLLs" path.
Work for system python and anaconda environments(need to do this in conda envs path)
PS.
"site-packages" path can be found by "pip --version"
"DLLs" path is located at "Lib\site-packages....\DLLs"
I am struggling with installing opencv for python 2.7.11 on OSX for almost three days now.
After some failures, I accomplished a first success by following the instructions here. So my basic setup is python 2.7.11 running from ~/.virtualenvs/cv_env/bin/python and I have a cv2.so located in ~/.virtualenvs/cv/lib/python2.7/site-packages/.
So good so far. Using source ~/.virtualenvs/cv_env/bin/activate, I can activate the virtualenv and than use import cv2. For some reasons, this does not work always. From time to time, I have to deactivate first and than reactivate (any guesses?).
Next, I wanted to use opencv in PyCharm. Under "preferences - Project interpreter", I selected the virtualenv interpreter and this is also working. I can import other moduals like numpy and pandas (previously installed into the vortualenv using pip). But for some reasons, I am not able to import opencv (import cv2). It always gives me
ImportError: No module named cv2
So my question is, why I am able to import opencv in terminal (at least sometimes) but not in PyCharm. Any ideas are welcomed.
Best,
Fabian
Your cv2.so located in a different directory. And you activating from another directory. I mean cv_env and cv.
I'm getting an error about a module and have searched for a solution for some time now and have come up empty handed. I'm coding in python and working in eclipse pydev. At the top of my code I typed.
import Image
I get a error that states ImportError: No module named Image
Here is just some screenshots of the Python Interpreter System Pythonpath
http://imageshack.us/a/img5/614/92989360.png
http://imageshack.us/f/545/79985417.png/
You need to install the Python Image Libray:
PIL
You can install it using pip via:
pip install PIL