Python pycharm and code completion

I’m using pycharm as a python ide. It appears that except in the console, pycharm don’t propose any completion after typing itk module name. Code is running fine.

In the python console, completion is working as expected.

Has someone any advice on how to get the completion working ?

Thanks for any help!
Y.

1 Like

Hi Yann,

In startup code called by PyCharm, it may be helpful to add:

import itkConfig
itkConfig.LazyLoading = False
import itk

This will load all of the toolkit so it will be available for completion.

1 Like

Hi!

I am running into the same issue, but @matt.mccormick’s solution does not seem to help in my case. Is it enough to add these three lines at the top of my script, or am I misunderstanding something?

The warning from PyCharm reads:

Cannot find reference ‘Image’ in ‘__init__.py’

(in the case of an itk.Image)

Some reading on the Internet seemed to imply that this is linked to the variable __all__ in __init__.py being incorrectly set by ITK’s wrappers, but my knowledge of Python is too limited to fully grasp the actual problem.

Could anyone provide additional details? It’s unnerving to see PyCharm complain so much. :laughing:

1 Like

@acoussat how is ITK’s lazy loading configuration set?

Thank you for your answer. :slight_smile:
I tried with both ITK lazy loading configurations, i.e. setting LazyLoading to both True and False as specified in this message but that did not seem to help, I did not see any difference.

Matt,

Thanks for the reply! I can find startup code in pycharm only for the console, but not for the general interface. What am I doing wrong ?

Thanks!
Y.

Yann,

First, make sure to use ITK 5.0.1 or later, which added support for PEP 366. @zahilshanis has reported that this transparently adds completion support in the PyCharm console.

I am not familiar with the details of how PyCharm performs its analysis for the editor, but it may help to provide hints like we for IPython / Jupyter completion.

Hope this helps,
Matt

1 Like

How about SimpleITK? For example:

import SimpleITK as sitk

image = sitk.ReadImage('data.nii.gz')
image.GetSpacing()

Here, when I type image.Get, I do not get auto completion of image.GetSpacing or something similar. Is there any “lazy loading” configuration similar to ITK?

1 Like

I’ve update itk to the 5.01.
Indeed, in pyCharm console, I get the completion without having to set itkConfig.lazyLoading = False.

But in the editor, still no completion… What I’ve tried:

  • set lazyLoading = False directly in itk code
  • cleared the cache restart several times

In the editor, the completion I get is module, thisModule, and it seems that it should be deleted in itk python code. I guess it has something to do with how pycharm indexes the code…

Any help would be greatly appreciated, because the python documentation is quite (yet!) hard to find, and without completion, things tend to be quite labored, and hazardous!

1 Like

Matt,

You suggest to provide hints for pyCharm too. Do you have any idea how this could be possible ?

Y.

Hello Butui,

I have used PyCharm with some success with it’s autocompletion. SimpleITK does not use lazy loading. It appears to have some problems knowing the return type of functions, like ReadImage.

When the class is explicitly constructed by type you can get completion to the member functions:

import SimpleITK as silk
img = sitk.Image([10,10], sitk.sitkUInt8)

Also there is auto-completion for positional and keyword arguments when procedural methods are directly called.

There appears to be some options for SWIG that may add some hints: https://github.com/swig/swig/issues/735

I have created a SimpleITK issue to track this feature request: https://github.com/SimpleITK/SimpleITK/issues/809

It may not be possible.

A bit more information on the topic: While digging into pycharm temporary indexing files, I’m seeing lots of itk related files. It also takes quite some time to index itk package. It would mean that they are indexed, but not bound to the itk package ?