How do I get an application using python to ignore PYTHONHOME and/or PYTHONPATH?

80 Views Asked by At

I am trying to debug a command line C module on Windows, which is a part of a much larger open source GIS software (GRASS GIS). GRASS on the command line (sort of) adopts a "UNIX Tools" philosophy and consists of a large number of (largely) independent modules. These modules can be run from either command line, or GUI, or or some particular group of them can be run sequentially using a script. However, it needs a whole bunch of environment variables to be setup first, which is typically done using a startup script (grassXY.bat on windows).

Amongst the environment variables it sets are PYTHONHOME and PYTHONPATH (since python is the scripting language of choice in GRASS GIS). A separate python instance is bundled into the GRASS GIS installer, which it installs at C:\Program Files\GRASS GIS 8.4\Python39. It is to this python instance that the startup script grassXY.bat points PYTHONHOME and PYTHONPATH to.

I wish to debug this using gdb inside VSCode, and I am using the gdb packaged with the Msys2 MinGW 64 environment. As it turns out, Msys2 gdb also uses python, but in a manner transparent to the user. Unfortunately, gdb wants to invoke a different instance of python located at 'C:\\msys64\\mingw64\\bin\\python.exe' inside the Msys2 environment, which fails because of a clash with the set values of PYTHONHOME and PYTHONPATH. gdb starts up fine if I unset PYTHONHOME, but this is liable to break unspecified parts of GRASS GIS.

Is there a way to instruct gdb (or the python instance at 'C:\\msys64\\mingw64\\bin\\python.exe' that it invokes) to ignore PYTHONHOME/PYTHONPATH without unseting them, so that they continue to be available to other GRASS GIS modules that need them?

What I've tried so far -

  1. Start GRASS GIS using grass84.bat
Microsoft Windows [Version 10.0.22621.3007]
(c) Microsoft Corporation. All rights reserved.

C:\Users\User>grass84 --text
Starting GRASS GIS...
WARNING: Concurrent mapset locking is not supported on Windows

          __________  ___   __________    _______________
         / ____/ __ \/   | / ___/ ___/   / ____/  _/ ___/
        / / __/ /_/ / /| | \__ \\_  \   / / __ / / \__ \
       / /_/ / _, _/ ___ |___/ /__/ /  / /_/ // / ___/ /
       \____/_/ |_/_/  |_/____/____/   \____/___//____/

Welcome to GRASS GIS 8.4.0dev (8d6269279e)
GRASS GIS homepage:                      https://grass.osgeo.org
This version running through:            Command Prompt (C:\WINDOWS\system32\cmd.exe)
Help is available with the command:      g.manual -i
See the licence terms with:              g.version -c
See citation options with:               g.version -x
Start the GUI with:                      g.gui wxpython
When ready to quit enter:                exit

Microsoft Windows [Version 10.0.22621.3007]
(c) Microsoft Corporation. All rights reserved.
  1. Display all environment variables starting with python -

C:\Users\User>set python
PYTHONHOME=C:\OSGeo4W\apps\Python39
PYTHONPATH=C:\OSGeo4W\apps\grass\grass84\etc\python
PYTHONUTF8=1
  1. Invoke gdb.exe using its full path to get a python error -
C:\Users\User>C:\msys64\mingw64\bin\gdb
Python path configuration:
  PYTHONHOME = 'C:\OSGeo4W\apps\Python39'
  PYTHONPATH = 'C:\OSGeo4W\apps\grass\grass84\etc\python'
  program name = 'C:\msys64\mingw64\bin\python.exe'
  isolated = 0
  environment = 1
  user site = 1
  safe_path = 0
  import site = 1
  is in build tree = 0
  stdlib dir = 'C:\OSGeo4W\apps\Python39\lib\python3.11'
  sys._base_executable = 'C:\\msys64\\mingw64\\bin\\python.exe'
  sys.base_prefix = 'C:\\OSGeo4W\\apps\\Python39'
  sys.base_exec_prefix = 'C:\\OSGeo4W\\apps\\Python39'
  sys.platlibdir = 'lib'
  sys.executable = 'C:\\msys64\\mingw64\\bin\\python.exe'
  sys.prefix = 'C:\\OSGeo4W\\apps\\Python39'
  sys.exec_prefix = 'C:\\OSGeo4W\\apps\\Python39'
  sys.path = [
    'C:\\OSGeo4W\\apps\\grass\\grass84\\etc\\python',
    'C:\\OSGeo4W\\apps\\Python39\\lib\\python311.zip',
    'C:\\OSGeo4W\\apps\\Python39\\lib\\python3.11',
    'C:\\OSGeo4W\\apps\\Python39\\lib\\python3.11\\lib-dynload',
  ]
Error occurred computing Python errormessage.
Python not initialized
  1. Unset PYTHONHOME -
C:\Users\User>set PYTHONHOME=
  1. Display all environment variables starting with python -
C:\Users\User>set python
PYTHONPATH=C:\OSGeo4W\apps\grass\grass84\etc\python
PYTHONUTF8=1

Try starting gdb again, this time successfully -

C:\Users\User>C:\msys64\mingw64\bin\gdb
GNU gdb (GDB) 14.1
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-w64-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb)
1

There are 1 best solutions below

1
Employed Russian On

As ssbssa commented, you can ask GDB to ignore PYTHONPATH with gdb -eiex "set python ignore-environment on".

Alternatively you can re-set the environment GDB uses to start the inferior (being debugged) process with (gdb) set env PYTHONPATH "C:\\OSGeo4W\\apps\\Python39". This latter setting can be in $HOME/.gdbinit.

Or you could use a custom exec-wrappper for your GRASS programs.