import ROOT: no module found -- How to add custom modules to PyCharm's project interpreter

Answered

Hello,

I am using the ROOT data analysis framework: https://root.cern.ch/

I would like for PyCharm to be able to recognize my installation of ROOT so that I can run my code and debug within PyCharm. How would I go about this? I am able to see how to create virtual environments and install modules through PyPi, but ROOT is not offered through a package manager and must be built through CMake. If I open a terminal I can import ROOT just fine:


python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
>>> ROOT.__file__
'/home/aj/root/lib/ROOT.py'
>>>

if possible I would like code completion to work as well... I found this post but it does not offer any additional help:

https://root-forum.cern.ch/t/pycharm-4-no-module-named-root-when-importing-a-locally-installed-module-pyroot-that-is-working-fine-from-shell-on-mac-os-x-yosemite/29898

6 comments
Comment actions Permalink

Hi,

Have you tried adding it that way https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000592884/comments/115000506184?

How did you install it to your system interpreter?

1
Comment actions Permalink

Thank you, now my code will properly run and the error for "import ROOT" is now gone. However, if I try doing something like "from ROOT import TH1F" PyCharm tells me that TH1F is not found in ROOT.py.. However, the code will still run whether I do the import like this or do ROOT.TH1F when I am creating this object. I also do not have any code completion.

 

I believe ROOT.py loads the shared object library libPyROOT,so which contains all of the bindings for the interpreter. Here's what I've been able to find:


"The top level Python module is ROOT.py. This module imports the extension module libPyROOT.so [.dll] and does a similar initialization as the ROOT application (i.e. loading common libraries, defining a number of globals, starting a thread to handle GUI events, etc.). This is the main entry point for any Python script using the ROOT classes."

0
Comment actions Permalink

Hmmm,

Which directories did you add to Interpreter Paths?
From what I can see, there is no TH1F in ROOT.py and also no __init__ files to make an interface for directories and modules.

Maybe it's because I haven't finished building it yet...

0
Comment actions Permalink

I only added the directory that the path $PYTHONPATH is linked to: "/home/aj/root/lib". I think "cppyy.py" or "cppyy.pyc" might be what is doing this magic. "cppyy.py" is imported in ROOT.py and it seems to do the loading of libPyROOT.so, which I'm guessing is how the python interpreter interfaces all of the C++ classes (TH1F for example).

0
Comment actions Permalink

Oh, I see. 
In this case, PyCharm cannot do anything about it to provide code completion since those classes/methods are generated dynamically in runtime and not by only using python modules.

1
Comment actions Permalink

Ah, makes sense. Thank you very much for your time and help!

0

Please sign in to leave a comment.