From ee78e3e2019cef3c0164cee9ce16b75a22417d8b Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Wed, 19 Mar 2025 03:06:23 -0400 Subject: [PATCH] add -f option to install.py to install into externally-managed environments --- doc/src/Python_install.rst | 9 ++++++--- python/install.py | 12 ++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/doc/src/Python_install.rst b/doc/src/Python_install.rst index 561e4a2f92..1bb0768d62 100644 --- a/doc/src/Python_install.rst +++ b/doc/src/Python_install.rst @@ -110,13 +110,16 @@ folder that the dynamic loader searches or inside of the installed .. code-block:: bash - python install.py -p -l -v [-n] + python install.py -p -l -v [-n] [-f] * The ``-p`` flag points to the ``lammps`` Python package folder to be installed, * the ``-l`` flag points to the LAMMPS shared library file to be installed, * the ``-v`` flag points to the LAMMPS version header file to extract the version date, - * and the optional ``-n`` instructs the script to only build a wheel file - but not attempt to install it. + * the optional ``-n`` instructs the script to only build a wheel file but not attempt + to install it, + * and the optional ``-f`` argument instructs the script to force installation even if + pip would otherwise refuse installation with an + :ref:`error about externally managed environments `. .. tab:: Virtual environment diff --git a/python/install.py b/python/install.py index fd9f95f1bf..352225e49d 100644 --- a/python/install.py +++ b/python/install.py @@ -27,6 +27,8 @@ parser.add_argument("-w", "--wheeldir", required=False, help="path to a directory where the created wheel will be stored") parser.add_argument("-v", "--versionfile", required=True, help="path to the LAMMPS version.h source file") +parser.add_argument("-f", "--force", action="store_true", required=False, default=False, + help="force installation of LAMMPS Python package") args = parser.parse_args() @@ -145,7 +147,10 @@ else: py_exe = sys.executable try: - txt = subprocess.check_output([py_exe, '-m', 'pip', 'install', '--force-reinstall', wheel], stderr=subprocess.STDOUT, shell=False) + if args.force: + txt = subprocess.check_output([py_exe, '-m', 'pip', 'install', '--force-reinstall', '--break-system-packages', wheel], stderr=subprocess.STDOUT, shell=False) + else: + txt = subprocess.check_output([py_exe, '-m', 'pip', 'install', '--force-reinstall', wheel], stderr=subprocess.STDOUT, shell=False) print(txt.decode('UTF-8')) sys.exit(0) except subprocess.CalledProcessError as err: @@ -154,7 +159,10 @@ except subprocess.CalledProcessError as err: sys.exit(errmsg + "You need to uninstall the LAMMPS python module manually first.\n") try: print('Installing wheel into system site-packages folder failed. Trying user folder now') - txt = subprocess.check_output([sys.executable, '-m', 'pip', 'install', '--user', '--force-reinstall', wheel], stderr=subprocess.STDOUT, shell=False) + if args.force: + txt = subprocess.check_output([sys.executable, '-m', 'pip', 'install', '--user', '--force-reinstall', '--break-system-packages', wheel], stderr=subprocess.STDOUT, shell=False) + else: + txt = subprocess.check_output([sys.executable, '-m', 'pip', 'install', '--user', '--force-reinstall', wheel], stderr=subprocess.STDOUT, shell=False) print(txt.decode('UTF-8')) except: sys.exit('Failed to install wheel ' + wheel)