# Importing root in python

**URL:** <https://root-forum.cern.ch/t/importing-root-in-python/26622>\
**Category:** ROOT\
**Created:** [October 19, 2017, 5:16pm UTC](https://root-forum.cern.ch/t/importing-root-in-python/26622 "2017-10-19T17:16:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![avinay](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@avinay](https://root-forum.cern.ch/u/avinay)\
**Post date:** [October 19, 2017, 5:16pm UTC](https://root-forum.cern.ch/t/importing-root-in-python/26622/1 "2017-10-19T17:16:39Z")

</div>

Hi experts

I installed Root 6.10/08 using homebrew. My aim is to import Root in python.  
However, I think since the Python version that homebrew installed for me (as a dependency for root) is different than the system python that already exists , I’m having trouble importing root in python.

When I simply type python --version in the terminal I get this:

```auto
Python 2.7.10

```

And, when i check for the homebrew Python by using the “brew info python” command, I get this:

```auto
python: stable 2.7.14 (bottled), HEAD
Interpreted, interactive, object-oriented programming language
https://www.python.org/
/usr/local/Cellar/python/2.7.14 (3,517 files, 48.4MB) *
  Poured from bottle on 2017-10-19 at 12:27:35
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/python.rb
==> Dependencies
Build: sphinx-doc ✘, pkg-config ✔
Required: openssl ✔
Recommended: readline ✔, sqlite ✔, gdbm ✔
Optional: sphinx-doc ✘, tcl-tk ✘, berkeley-db@4 ✘
==> Options
--with-berkeley-db@4
	Build with berkeley-db@4 support
--with-poll
	Enable select.poll, which is not fully implemented on macOS (https://bugs.python.org/issue5154)
--with-quicktest
	Run `make quicktest` after the build (for devs; may fail)
--with-sphinx-doc
	Build HTML documentation
--with-tcl-tk
	Use Homebrew's Tk instead of macOS Tk (has optional Cocoa and threads support)
--without-gdbm
	Build without gdbm support
--without-readline
	Build without readline support
--without-sqlite
	Build without sqlite support
--HEAD
	Install HEAD version
==> Caveats
This formula installs a python2 executable to /usr/local/bin.
If you wish to have this formula's python executable in your PATH then add
the following to ~/.bash_profile:
  export PATH="/usr/local/opt/python/libexec/bin:$PATH"

Pip and setuptools have been installed. To update them
  pip2 install --upgrade pip setuptools

You can install Python packages with
  pip2 install <package>

They will install into the site-package directory
  /usr/local/lib/python2.7/site-packages

See: https://docs.brew.sh/Homebrew-and-Python.html

```

So clearly the two pythons are different

When I open the system python by simply typing python and then then try to import root by typing import ROOT, i get this:

```auto
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named ROOT

```

How do i import homebrew root from system python a.k.a Python 2.7.10 ?

---

<div class="post-metadata">

**Author:** ![amadio](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amadio/32/12562_2.png) [@amadio](https://root-forum.cern.ch/u/amadio)\
**Post date:** [October 19, 2017, 5:55pm UTC](https://root-forum.cern.ch/t/importing-root-in-python/26622/2 "2017-10-19T17:55:13Z")

</div>

Please make sure to add the library directory where `libPyROOT.so` was installed to the PYTHONPATH environment variable. You will also need to make sure that the python executable used by ROOT during the build was the correct one (things will be broken if you build with python 2.x and then try to import the modules from python 3.x).

---

<div class="post-metadata">

**Author:** ![amadio](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/amadio/32/12562_2.png) [@amadio](https://root-forum.cern.ch/u/amadio)\
**Post date:** [October 19, 2017, 5:57pm UTC](https://root-forum.cern.ch/t/importing-root-in-python/26622/3 "2017-10-19T17:57:17Z")

</div>

Sorry, just realized you installed via homebrew (I’m reading on my phone). In that case, you probably need to use python also from homebrew, although it may work with your system Python if you export the proper PYTHONPATH, since it’s also 2.x.

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [October 19, 2017, 6:20pm UTC](https://root-forum.cern.ch/t/importing-root-in-python/26622/4 "2017-10-19T18:20:12Z")

</div>

Take a look at these recent topics. This issue has been discussed quite frequently on the forum lately. There are also other topics that can be found using the search that should help you.

> [@Cannot access file in python "unsafe use of relative rpath"](https://root-forum.cern.ch/t/cannot-access-file-in-python-unsafe-use-of-relative-rpath/26565/12):
>
> On my mac this is the stock python. The following command will tell you where the python that is being executed lives: which python which python2 I believe that by default the system python comes first in the path and may be the reason that the homebrew version is not being evoked. This will list the search paths for binaries: printenv PATH This will let you locate all the files named python on the system, one will be the system python and one will be homebrew: locate \*/python This is typ…

> [@PyROOT cannot open file: "Error in \<TNetXNGFile::Open\>: \[FATAL\] Auth failed"](https://root-forum.cern.ch/t/pyroot-cannot-open-file-error-in-tnetxngfile-open-fatal-auth-failed/26593/20):
>
> @ksmith So it looks like you’re right. It’s an issue with SIP that can be worked around using a different, non-system python. However, that one flag alone is not enough. I had to do: cmake -Dall=On -DPYTHON\_EXECUTABLE=/usr/local/bin/python2 -DPYTHON\_INCLUDE\_DIR=/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/Headers -DPYTHON\_LIBRARY=/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib ../root/ pointing it at the Homebrew vers…

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [November 2, 2017, 6:27pm UTC](https://root-forum.cern.ch/t/importing-root-in-python/26622/5 "2017-11-02T18:27:21Z")

</div>

Closed on November 2, 2017, 6:27pm UTC

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