# Geant4 Python Installation

**URL:** <https://geant4-forum.web.cern.ch/t/geant4-python-installation/3165>\
**Category:** Getting Started\
**Created:** [August 3, 2020, 2:19am UTC](https://geant4-forum.web.cern.ch/t/geant4-python-installation/3165 "2020-08-03T02:19:27Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jll18](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/j/4af34b/32.png) [@jll18](https://geant4-forum.web.cern.ch/u/jll18)\
**Post date:** [August 3, 2020, 2:19am UTC](https://geant4-forum.web.cern.ch/t/geant4-python-installation/3165/1 "2020-08-03T02:19:27Z")

</div>

While trying to install python modules, I come across this error while running cmake wondering if anyone has experienced the same issue

$ cmake /Users/Joe/Geant4/geant4.10.06.p02/environments/g4py

CMake Error at G4PythonHelpers.cmake:15 (file):  
file RELATIVE\_PATH must be passed a full path to the file:  
Call Stack (most recent call first):  
CMakeLists.txt:14 (include)

CMake Error at CMakeLists.txt:23 (geant4\_add\_feature):  
Unknown CMake command “geant4\_add\_feature”.

---

<div class="post-metadata">

**Author:** ![bmorgan](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/bmorgan/32/7_2.png) [@bmorgan](https://geant4-forum.web.cern.ch/u/bmorgan)\
**Post date:** [August 3, 2020, 9:52am UTC](https://geant4-forum.web.cern.ch/t/geant4-python-installation/3165/2 "2020-08-03T09:52:15Z")

</div>

As of 10.6.2, the Python binding can be done as part of the main Geant4 build. Apologies that this wasn’t updated in the docs. To enable it, build Geant4 as per the install guide, but add the option

```auto
-DGEANT4_USE_PYTHON=ON

```

This requires Python 3, plus Boost with Python3 support. If you want Geant4 built in Multithreaded mode with Python, this will also require the options:

```auto
-DGEANT4_BUILD_MULTITHREADED=ON
-DGEANT4_BUILD_TLS_MODEL=global-dynamic

```

---

<div class="post-metadata">

**Author:** ![jll18](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/j/4af34b/32.png) [@jll18](https://geant4-forum.web.cern.ch/u/jll18)\
**Post date:** [August 4, 2020, 2:19am UTC](https://geant4-forum.web.cern.ch/t/geant4-python-installation/3165/4 "2020-08-04T02:19:02Z")

</div>

Hi thank you very much! I’ve downloaded the python script in the lib directory but I can’t seem to execute it. Is there any way of running Geant4 in anaconda?

---

<div class="post-metadata">

**Author:** ![shilpi](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/s/958977/32.png) [@shilpi](https://geant4-forum.web.cern.ch/u/shilpi)\
**Post date:** [September 2, 2020, 6:03pm UTC](https://geant4-forum.web.cern.ch/t/geant4-python-installation/3165/5 "2020-09-02T18:03:48Z")

</div>

Hi,

I have a question on the same. I installed it using commands:

cmake -DCMAKE\_INSTALL\_PREFIX=/tmp/shilpi/geant4.10.06-install /tmp/shilpi/geant4.10.06.p02 -DGEANT4\_INSTALL\_DATA=ON -DGEANT4\_USE\_PYTHON=ON  
make -j2  
make install

It got installed all fine.  
But when I try this:  
python  
from Geant4 import \*

It gave me this error: undefined symbol: \_ZN5boost6python6detail11init\_moduleER11PyModuleDefPFvvE

Could it be that the boost version is not compatible with the python version? For reference, I am using python version 3.6 and boost version 1.53.0

What would be the recommended version of boost? Also, how to specify with cmake?

Thanks  
Shilpi

---

<div class="post-metadata">

**Author:** ![bmorgan](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/bmorgan/32/7_2.png) [@bmorgan](https://geant4-forum.web.cern.ch/u/bmorgan)\
**Post date:** [September 3, 2020, 7:46am UTC](https://geant4-forum.web.cern.ch/t/geant4-python-installation/3165/6 "2020-09-03T07:46:38Z")

</div>

> [@shilpi](#):
>
> It gave me this error: undefined symbol: \_ZN5boost6python6detail11init\_moduleER11PyModuleDefPFvvE
> 
> Could it be that the boost version is not compatible with the python version? For reference, I am using python version 3.6 and boost version 1.53.0

Is that Boost version compiled for Python 3.6 (what OS/version is this on, and id the Boost install a system package or self installed)? That undefined symbol can be a symptom of using a Boost.Python that was compiled for Python 2.

Geant4’s Python interface has been built against Boost 1.70 successfully, and I’d guess that most recent versions should work fine. To point CMake to a specific Boost installed at, say `/my/boost/install`, add that path to `CMAKE_PREFIX_PATH` either in the environment, or as an argument to `cmake` (`cmake -DCMAKE_PREFIX_PATH=/my/boost/install <otherargs>`)

---

<div class="post-metadata">

**Author:** ![shilpi](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/s/958977/32.png) [@shilpi](https://geant4-forum.web.cern.ch/u/shilpi)\
**Post date:** [September 3, 2020, 10:09am UTC](https://geant4-forum.web.cern.ch/t/geant4-python-installation/3165/7 "2020-09-03T10:09:43Z")

</div>

Hi Benjamin,

Thanks for your prompt reply. I am working on cern lxplus and so pointing libraries from there.  
I tried this command:

`cmake -DCMAKE_INSTALL_PREFIX=/tmp/shilpi/geant4.10.06-install /tmp/shilpi/geant4.10.06.p02 -DGEANT4_INSTALL_DATA=ON -DGEANT4_USE_PYTHON=ON -DCMAKE_PREFIX_PATH=/cvmfs/cms.cern.ch/slc7_amd64_gcc820/external/boost/1.72.0-gchjei/`

I get this warning:

> – Found PythonInterp: /cvmfs/cms.cern.ch/slc7\_amd64\_gcc820/cms/cmssw/CMSSW\_10\_6\_1/external/slc7\_amd64\_gcc820/bin/python3 (found suitable version “3.6.4”, minimum required is “3.0”)  
> – Found PythonLibs: /cvmfs/cms.cern.ch/slc7\_amd64\_gcc820/cms/cmssw/CMSSW\_10\_6\_1/external/slc7\_amd64\_gcc820/lib/libpython3.6m.so (found version “3.6.8”)  
> CMake Warning at /cvmfs/sft.cern.ch/lcg/contrib/CMake/3.14.2/Linux-x86\_64/share/cmake-3.14/Modules/FindBoost.cmake:896 (message):  
> New Boost version may have incorrect or missing dependencies and imported  
> targets  
> Call Stack (most recent call first):  
> /cvmfs/sft.cern.ch/lcg/contrib/CMake/3.14.2/Linux-x86\_64/share/cmake-3.14/Modules/FindBoost.cmake:1018 (\_Boost\_COMPONENT\_DEPENDENCIES)  
> /cvmfs/sft.cern.ch/lcg/contrib/CMake/3.14.2/Linux-x86\_64/share/cmake-3.14/Modules/FindBoost.cmake:1694 (\_Boost\_MISSING\_DEPENDENCIES)  
> environments/g4py/CMakeLists.txt:11 (find\_package)
> 
> – Boost version: 1.72.0  
> – Found the following Boost libraries:  
> – python

But everything compiled as usual. It still gave me the same error when I did:

```
python
from GEANT4 import *

```

If I look inside /cvmfs/cms.cern.ch/slc7\_amd64\_gcc820/external/boost/1.72.0-gchjei/lib/, I see this: libboost\_python27.so

so probably its compiled with python2.7 - I am not sure…  
So your suggestion would be install a boost version 1.7 using python3?

Also, may I ask which versions of GEANT4 can work with python2? I can try with that as well.

Thanks a lot,  
Shilpi

---

<div class="post-metadata">

**Author:** ![shilpi](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/s/958977/32.png) [@shilpi](https://geant4-forum.web.cern.ch/u/shilpi)\
**Post date:** [September 3, 2020, 5:08pm UTC](https://geant4-forum.web.cern.ch/t/geant4-python-installation/3165/8 "2020-09-03T17:08:05Z")

</div>

Seems that GEANT4 version 10.5 (to be precise: geant4.10.05.p01) works. Just listing down the steps in case it is useful for someone:

> setenv PATH /cvmfs/sft.cern.ch/lcg/contrib/CMake/3.14.2/Linux-x86\_64/bin:$PATH  
> cmake -DCMAKE\_INSTALL\_PREFIX=/tmp/shilpi/geant4.10.05.p01-install /tmp/shilpi/geant4.10.05.p01 -DGEANT4\_INSTALL\_DATA=ON -DGEANT4\_USE\_OPENGL\_X11=ON  
> make -j2  
> make install
> 
> cd /tmp/shilpi/geant4.10.05.p01/environments/g4py/  
> mkdir build  
> cd build  
> setenv GEANT4\_INSTALL /tmp/shilpi/geant4.10.05.p01-install  
> cmake -DCMAKE\_INSTALL\_PREFIX=$GEANT4\_INSTALL -DXERCESC\_ROOT\_DIR=/cvmfs/sft.cern.ch/lcg/external/XercesC/3.1.1p2/x86\_64-slc6-gcc47-opt/ …
> 
> To run:  
> setenv PYTHONPATH /tmp/shilpi/geant4.10.05.p01-install/lib64/:$PYTHONPATH
> 
> setenv PYTHONPATH /tmp/shilpi/geant4.10.05.p01-install/lib64/g4py/:$PYTHONPATH  
> setenv PYTHONPATH /tmp/shilpi/geant4.10.05.p01-install/lib64/Geant4/:$PYTHONPATH  
> setenv PYTHONPATH /tmp/shilpi/geant4.10.05.p01-install/lib64/tests/:$PYTHONPATH  
> setenv PYTHONPATH /tmp/shilpi/geant4.10.05.p01-install/lib64/examples/:$PYTHONPATH

> Now any code can be run

---

<div class="post-metadata">

**Author:** ![shilpi](https://geant4-forum.web.cern.ch/letter_avatar_proxy/v4/letter/s/958977/32.png) [@shilpi](https://geant4-forum.web.cern.ch/u/shilpi)\
**Post date:** [September 3, 2020, 8:44pm UTC](https://geant4-forum.web.cern.ch/t/geant4-python-installation/3165/9 "2020-09-03T20:44:52Z")

</div>

Hi,

I have another problem in running the test codes. I am using code inside: environments/g4py/tests/gtest01/  
If I do:  
python test.py, I get this error:

> Traceback (most recent call last):  
> File “test.py”, line 103, in   
> pg.SetParticleByName(“e-”)  
> Boost.Python.ArgumentError: Python argument types in  
> G4ParticleGun.SetParticleByName(G4ParticleGun, str)  
> did not match C++ signature:  
> SetParticleByName(G4ParticleGun\*, std::\_\_cxx11::basic\_string\<char, std::char\_traits, std::allocator \>)

which to me seems can arise if the compiled boost version is not compatible with the python version I am using. For reference, I am using python version 2.7.15+ and boost version 1.72.0 which seems to have libboost\_python27.so in its lib directory so my guess its compiled with python2.7.

Do you know what could be causing this error?

Thanks  
Shilpi

---

<div class="post-metadata">

**Author:** ![bmorgan](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/bmorgan/32/7_2.png) [@bmorgan](https://geant4-forum.web.cern.ch/u/bmorgan)\
**Post date:** [September 4, 2020, 8:32am UTC](https://geant4-forum.web.cern.ch/t/geant4-python-installation/3165/10 "2020-09-04T08:32:13Z")

</div>

I’d recommend sticking with Geant4 10.6.2 as this has significant improvements to the build of the Python bindings. As you are running on lxplus, it would be easiest to use the LCG packages either via a view, e.g.

```auto
$ . /cvmfs/sft.cern.ch/lcg/views/LCG_98python3/x86_64-centos7-gcc8-opt/setup.sh

```

or by setting them up individually, e.g.

```auto
$ . /cvmfs/sft.cern.ch/lcg/releases/LCG_98python3/CMake/3.17.3/x86_64-centos7-gcc8-opt/CMake-env.sh
$ . /cvmfs/sft.cern.ch/lcg/releases/LCG_98python3/Boost/1.73.0/x86_64-centos7-gcc8-opt/Boost-env.sh
... other as needed ...

```

The views come with Geant4, but shouldn’t clash as long as when running you _prepend_ the directories holding your Geant4 libraries and python modules to `LD_LIBRARY_PATH` and `PYTHONPATH` respectively.

---

<div class="post-metadata">

**Author:** ![Vivek\_Kaushik](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/vivek_kaushik/32/9327_2.png) [@Vivek\_Kaushik](https://geant4-forum.web.cern.ch/u/Vivek_Kaushik)\
**Post date:** [December 17, 2021, 6:24am UTC](https://geant4-forum.web.cern.ch/t/geant4-python-installation/3165/12 "2021-12-17T06:24:51Z")

</div>

I am getting an error while

**\>\> import Geant4**  
\*\* from G4interface import \*\*\*  
**Import error: libG4intercom.so cannot open shared file object: No such file or directory**

this libG4intercom.so is in lib directory geant4-install folder, I have updated the  
export GEANT4\_INSTALL=/path  
export PYTHONPATH =/path/:$PYTHOPNPATH

kindly Help

Regards

---

<div class="post-metadata">

**Author:** ![bmorgan](https://geant4-forum.web.cern.ch/user_avatar/geant4-forum.web.cern.ch/bmorgan/32/7_2.png) [@bmorgan](https://geant4-forum.web.cern.ch/u/bmorgan)\
**Post date:** [July 2, 2022, 4:54pm UTC](https://geant4-forum.web.cern.ch/t/geant4-python-installation/3165/13 "2022-07-02T16:54:53Z")

</div>

Closed on July 2, 2022, 4:54pm UTC


