Earon
Earon

Reputation: 823

Why PyCharm not show autocomplete list

I have installed the PIL library using pip like this:

pip install Pillow

and it's working fine also showing library path in interrupter PyCharm Options.

But Autocomplete is not working, I tried to turn off the Power Save Mode and added custom virtual env, but it's still not working.

Have a look Image

Any idea why it does not work?

So it's now showing sub methods :

from PIL import Image
im = Image.open("bride.jpg")
im.rotate(45).show()

Upvotes: 2

Views: 3112

Answers (2)

Robin Carter
Robin Carter

Reputation: 139

The problem is that:

  • PIL.Image is a module (normally lowercase)
  • The module contains a function called open()
  • open() returns a Type of PIL.Image.Image
  • PyCharm does not realise it returns this type unless you tell it (I don't why).

https://pillow.readthedocs.io/en/3.0.x/handbook/tutorial.html#using-the-image-class

To avoid clashes with my project on the name "Image", as well as to stay Pep8 and adhere to google's style guide, I opted for:

    from PIL import Image as PilImage

    my_image: PilImage.Image = PilImage.open("my_image.png")
    my_image.show()

Running on python 3.8.2

Upvotes: 0

SBKubric
SBKubric

Reputation: 106

PyCharm doesn't know the type of im as the library has no type hints for Image.open(). So I decided to set local type hint like this:

your_image = Image.open(path_to_image) # type: Image.Image
your_image.now_shows_list = True

For complete reference look at JetBrains type hints documentation.

Upvotes: 9

Related Questions