# Python pycharm and code completion

**URL:** https://discourse.itk.org/t/python-pycharm-and-code-completion/1697
**Category:** Beginner Questions
**Created:** [March 22, 2019, 4:29pm UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697 "2019-03-22T16:29:56Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![asertyuio](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/e68b1a/32.png) [@asertyuio](https://discourse.itk.org/u/asertyuio)
#### Post date: [March 22, 2019, 4:29pm UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/1 "2019-03-22T16:29:56Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [March 22, 2019, 7:02pm UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/2 "2019-03-22T19:02:30Z")

</div>

Hi Yann,

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

```auto
import itkConfig
itkConfig.LazyLoading = False
import itk

```

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

---

<div class="post-metadata">

### Author: ![acoussat](https://discourse.itk.org/user_avatar/discourse.itk.org/acoussat/32/699_2.png) [@acoussat](https://discourse.itk.org/u/acoussat)
#### Post date: [April 19, 2019, 9:52am UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/3 "2019-04-19T09:52:11Z")

</div>

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. 😆

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [April 26, 2019, 6:26pm UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/4 "2019-04-26T18:26:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![acoussat](https://discourse.itk.org/user_avatar/discourse.itk.org/acoussat/32/699_2.png) [@acoussat](https://discourse.itk.org/u/acoussat)
#### Post date: [April 29, 2019, 11:55am UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/5 "2019-04-29T11:55:36Z")

</div>

Thank you for your answer. 🙂  
I tried with both ITK lazy loading configurations, i.e. setting `LazyLoading` to both `True` and `False` as specified in [this message](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/2) but that did not seem to help, I did not see any difference.

---

<div class="post-metadata">

### Author: ![asertyuio](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/e68b1a/32.png) [@asertyuio](https://discourse.itk.org/u/asertyuio)
#### Post date: [August 15, 2019, 8:16pm UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/6 "2019-08-15T20:16:53Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [August 19, 2019, 3:24pm UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/7 "2019-08-19T15:24:03Z")

</div>

Yann,

First, make sure to use ITK 5.0.1 or later, which [added support](https://github.com/InsightSoftwareConsortium/ITK/commit/975439cbae0118fb84dd5d6ec08982cd57d67e18) for [PEP 366](https://www.python.org/dev/peps/pep-0366/). @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](https://github.com/InsightSoftwareConsortium/ITK/blob/45c1147a497585dd33493407e540afa9eb0591fe/Wrapping/Generators/Python/itkExtras.py#L1151-L1261) like we for IPython / Jupyter completion.

Hope this helps,  
Matt

---

<div class="post-metadata">

### Author: ![hubutui](https://discourse.itk.org/user_avatar/discourse.itk.org/hubutui/32/716_2.png) [@hubutui](https://discourse.itk.org/u/hubutui)
#### Post date: [August 20, 2019, 8:11am UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/8 "2019-08-20T08:11:56Z")

</div>

How about SimpleITK? For example:

```python
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?

---

<div class="post-metadata">

### Author: ![asertyuio](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/e68b1a/32.png) [@asertyuio](https://discourse.itk.org/u/asertyuio)
#### Post date: [August 20, 2019, 9:30am UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/9 "2019-08-20T09:30:33Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![asertyuio](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/e68b1a/32.png) [@asertyuio](https://discourse.itk.org/u/asertyuio)
#### Post date: [August 20, 2019, 10:20am UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/10 "2019-08-20T10:20:44Z")

</div>

Matt,

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

Y.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [August 20, 2019, 12:11pm UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/11 "2019-08-20T12:11:00Z")

</div>

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:

```auto
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](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](https://github.com/SimpleITK/SimpleITK/issues/809)

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [August 20, 2019, 3:03pm UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/12 "2019-08-20T15:03:31Z")

</div>

> [@asertyuio](#):
>
> You suggest to provide hints for pyCharm too. Do you have any idea how this could be possible ?

It may not be possible.

---

<div class="post-metadata">

### Author: ![asertyuio](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/e68b1a/32.png) [@asertyuio](https://discourse.itk.org/u/asertyuio)
#### Post date: [August 20, 2019, 3:55pm UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/13 "2019-08-20T15:55:37Z")

</div>

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 ?

---

<div class="post-metadata">

### Author: ![cyanxwh](https://discourse.itk.org/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cyanxwh](https://discourse.itk.org/u/cyanxwh)
#### Post date: [December 5, 2019, 1:33am UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/14 "2019-12-05T01:33:48Z")

</div>

I also face the same problem. But my pycharm autocompletion works for SimpleITK. Is there any solution for this problem?

---

<div class="post-metadata">

### Author: ![fraca](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fraca](https://discourse.itk.org/u/fraca)
#### Post date: [December 5, 2019, 9:07am UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/15 "2019-12-05T09:07:58Z")

</div>

I suggest you to install [Ananconda](https://www.anaconda.com/) and use, for example, Jupyther. I found Anaconda very easier to use than PyCharm. You can install itk from Pypi with the Ananconda prompt

---

<div class="post-metadata">

### Author: ![cyanxwh](https://discourse.itk.org/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cyanxwh](https://discourse.itk.org/u/cyanxwh)
#### Post date: [December 7, 2019, 9:21am UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/16 "2019-12-07T09:21:19Z")

</div>

Thank you for your advise, I’ll try it.

---

<div class="post-metadata">

### Author: ![fraca](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@fraca](https://discourse.itk.org/u/fraca)
#### Post date: [December 7, 2019, 1:35pm UTC](https://discourse.itk.org/t/python-pycharm-and-code-completion/1697/17 "2019-12-07T13:35:42Z")

</div>

I hope that this can help you, but i don’t know how to include the auto code-completion
