Autocomplete in PyCharm not working

Hey everybody,

I’ve been trying for quite some hours now to get code completition working in my PyCharm IDE.
I can import PyROOT (as well as rootpy), and I can also run scripts within my IDE, there is just no autocomplete for ROOT. At the import statement, I get the hint that “constant variable imported as non constant”.
In a shell, I also have no autocomplete but can use root (e.g. create a histogram and fill it).
I am running Ubuntu 19.10, ROOT 6.18.4, Python 3.7.5, rootpy 1.0.1, Pycharm 2019.3 Professional

Can someone help me please?

1 Like

Hi,

We are not developing rootpy. So probably about it, you should ask rootpy developers…

About PyROOT autocompletion in PyCharm IDE, I will check the its Comunity Forum on how to setup it https://intellij-support.jetbrains.com/hc/en-us, or perhaps you are lucky and @etejedor can help you here?

PyROOT is a special kind of Python bindings, in the sense that everything is dynamic: no wrapper Python class is pre-generated for a given C++ class. This means that autocomplete can’t work in the usual way, since ROOT just contains a few important attributes that have been pre-inserted, but not the whole set of ROOT classes. Code completion should work though for methods provided by an object, since those are injected when dynamically creating its class.

Thank you for your replies.
Concerning your last sentence, I don’t understand when objects are created. If they are created at run time, I don’t see how this can make the autocomplete work. I have a minimal non working example:

import ROOT

h1 = ROOT.TH1D("h1", "h1", 100, 0, 50)

h1.Fill(17)
h1.Draw()

I create the object h1, but I still don’t get any autocomplete in the next lines. Pycharm just suggests the methods I have already used in this script, and without any implementation (parameters and so on). If I understand you correctly, I should get autocomplete at least for TH1D methods after creating h1, but I don’t.
I have already set my LD_LIBRARY_PATH to the head directory of ROOT.py, and have also added the root libraries to my interpreter path.

If you could explain me what you meant, I would really appreciate it.

This means that autocomplete can’t work in the usual way,

yes it can work; see cppyy. The ticket is to customize __dir__.

Yes that is correct. If you try it from the Python prompt, you should see how it gives you the suggestion for Fill. Is that the case or that does not work either?

yes it can work; see cppyy. The ticket is to customize __dir__ .

If that relies on rootmap files, most of them don’t exist anymore in 6.20.

Thank you very much for your help.
I tried it again, and in the shell I have autocomplete after creating an object, so there it works.
In my IDE PyCharm it does not which I assume is because the script is not run and therefore the objects do not exist yet.
Regarding wlav’s suggestion, could you please explain this to me? What does it mean to customize dir? I am using root 6.18.4, and in my ROOT library folder there are .rootmap files. In my cppyy.py in the ROOT library folder there is no dir though. What do I have to customize, and how?
Or is it hopeless and I should resign to the fact that there will be no autocomplete in my IDE?

In my IDE PyCharm it does not which I assume is because the script is not run and therefore the objects do not exist yet.

Yes, that must be the reason. No classes exist beforehand in PyROOT, everything is dynamic, so the IDE can’t tell what is there.

What Wim mentioned is not something that you would need to do as a user. Finding a way to provide code completion in PyROOT for code editors is something that should be handled internally.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.