pyROOT in virtualenv

1.1k Views Asked by At

I want to use pyROOT in a virtualenv, I have ROOT installed on my system, when outside the virtualenv I can do import ROOT and it works, but inside a virtualenv doing an import ROOT gives the following error:

ModuleNotFoundError: No module named 'ROOT'

How do I add ROOT in there?

Thanks in advance.

4

There are 4 best solutions below

0
On

Install pyroot. try "pyroot file.py" instead of "python file.py"

0
On

You need to add ROOT to your python path. You can do this by creating an environment variable PYTHONPATH with the value of your bin folder in the root install directory.

This is how it looks for me on windows: enter image description here

To verify it had worked, you can

import sys
print(sys.path)

And check if the bin directory is in there.

2
On

You'll need to install root within the virtual environment too. Run a pip install command after activating the environment.

0
On

Like many Python packages, support for the ROOT framework in Python exists as two parts...a C++ framework, and a Python binding around that framework. In this case, both the C++ framework and the Python binding are provided by the same group and so are discussed together on the same web site. https://root.cern is the home page for the framework, and https://root.cern/manual/python is the Python module page.

The C++ framework should be installed first. How that is done will vary by platform. Then, the Python module should be installed into each Python version and/or virtual environment in which one wishes to use the ROOT framework, via the command:

pip install pyroot

Say you have a virtualenv in the directory ~/envs/myenv. You'd then want to do:

source ~/envs/myenv/bin/activate
pip install pyroot