pyMBE-dev / pyMBE

pyMBE provides tools to facilitate building up molecules with complex architectures in the Molecular Dynamics software ESPResSo. For an up-to-date API documention please check our website:
https://pymbe-dev.github.io/pyMBE/pyMBE.html
GNU General Public License v3.0
6 stars 8 forks source link

Use virtual environments #10

Closed jngrad closed 5 months ago

jngrad commented 6 months ago

The Makefile and Python scripts use hardcoded paths and fragile path detection mechanisms to locate pypresso and pyMBE:

https://github.com/pm-blanco/pyMBE/blob/9df73eadbe235001349b2928f3ab3837adf49112/tests/LYS_ASP_peptide.py#L28-L32 https://github.com/pm-blanco/pyMBE/blob/9df73eadbe235001349b2928f3ab3837adf49112/Makefile#L8-L14

These workarounds will break in any environment that doesn't strictly adhere to the naming convention for the folder tree structure we are using. Python virtual environments were introduced in Python 3.3 to solve this exact problem. Here is how to configure a pyMBE-specific virtual environment (please adapt the path to the espresso build folder accordingly):

ESPRESSO_PATH=../espresso/build # adapt this path
python3 -m venv venv
echo 'export OLD_PYTHONPATH="${PYTHONPATH}"' >> venv/bin/activate
echo 'export PYTHONPATH="'$(realpath .)'${PYTHONPATH:+:$PYTHONPATH}"' >> venv/bin/activate
echo 'export PYTHONPATH="'$(realpath ${ESPRESSO_PATH}/src/python)':${PYTHONPATH}"' >> venv/bin/activate
sed -i '/deactivate () {/a export PYTHONPATH="${OLD_PYTHONPATH}"' venv/bin/activate
source venv/bin/activate
python3 -m pip install -r requirements.txt
deactivate

With this environment, we no longer need to hardcode paths in the Makefile, and we no longer need to dynamically detect the path to the pyMBE root directory. We actually no longer need the pypresso wrapper script, because its only function is to set the $PYTHONPATH, and this is now achieved by the virtual environment. To update the Makefile:

sed -i 's/${ESPResSo_build_path}\/pypresso /python3 /' Makefile

Under the hood, virtual environments work by imitating an operating system with local /lib, /bin and /include folders that have precedence over the system-wide folders with the same name. This is quite similar to how modulefiles work, but they are a lot simpler because you can only have one environment; modulefiles manage one environment per package, with expensive cross-compatibility checks when you module load multiple packages. Virtual environment will become ubiquitous with the release of Ubuntu 24.04, can be used on compute clusters and in slurm job scripts.

Every time you want to use pyMBE, load the python virtual environment:

$ echo $PYTHONPATH # empty on my workstation

$ source venv/bin/activate # enter the virtual environment
(venv) $ echo $PYTHONPATH
/work/jgrad/espresso/build/src/python:/work/jgrad/pyMBE
(venv) $ python3 -c "import espressomd.version; print(espressomd.version.friendly())"
4.2
(venv) $ python3 -c "import pyMBE; print(pyMBE.__file__)"
/work/jgrad/pyMBE/pyMBE.py
(venv) $ make testsuite
(venv) $ deactivate # leave the virtual environment
$ echo $PYTHONPATH  # original value should be restored

$