如何使用 python 获取应用程序以忽略 PYTHONHOME 和/或 PYTHONPATH?

问题描述 投票:0回答:1

我正在尝试在 Windows 上调试命令行 C 模块,该模块是更大的开源 GIS 软件 (GRASS GIS) 的一部分。命令行上的 GRASS(某种程度上)采用“UNIX 工具”理念,由大量(大部分)独立的模块组成。这些模块可以从命令行或 GUI 运行,或者其中的某些特定组可以使用脚本顺序运行。然而,它需要首先设置一大堆环境变量,这通常是使用启动脚本(Windows 上的grassXY.bat)完成的。

它设置的环境变量包括 PYTHONHOME 和 PYTHONPATH(因为 python 是 GRASS GIS 中选择的脚本语言)。 GRASS GIS 安装程序中捆绑了一个单独的 python 实例,该安装程序安装在

C:\Program Files\GRASS GIS 8.4\Python39
。启动脚本
grassXY.bat
正是将PYTHONHOME和PYTHONPATH指向这个Python实例。

我希望在 VSCode 中使用 gdb 进行调试,并且我正在使用与

Msys2 MinGW 64
环境一起打包的 gdb。事实证明,Msys2 gdb 也使用 python,但以对用户透明的方式。不幸的是,gdb 希望调用 Msys2 环境中位于“C:\msys64\mingw64\bin\python.exe”的不同 python 实例,由于与 PYTHONHOME 和 PYTHONPATH 的设置值冲突而失败。如果我取消设置 PYTHONHOME,gdb 会正常启动,但这可能会破坏 GRASS GIS 的未指定部分。

有没有办法指示 gdb (或它调用的位于 'C:\msys64\mingw64\bin\python.exe' 的 python 实例)忽略 PYTHONHOME/PYTHONPATH 而不取消设置它们,以便它们继续可供其他人使用GRASS GIS 模块需要它们吗?

到目前为止我已经尝试过的 -

  1. 使用grass84.bat启动GRASS GIS
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. 显示所有以
    python
    -
  2. 开头的环境变量

C:\Users\User>set python
PYTHONHOME=C:\OSGeo4W\apps\Python39
PYTHONPATH=C:\OSGeo4W\apps\grass\grass84\etc\python
PYTHONUTF8=1
  1. 使用其完整路径调用 gdb.exe 以获得 python 错误 -
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. 取消设置 PYTHONHOME -
C:\Users\User>set PYTHONHOME=
  1. 显示所有以
    python
    -
  2. 开头的环境变量
C:\Users\User>set python
PYTHONPATH=C:\OSGeo4W\apps\grass\grass84\etc\python
PYTHONUTF8=1

再次尝试启动 gdb,这次成功 -

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)
python windows environment-variables gdb pythonpath
1个回答
0
投票

正如 ssbssa 评论的那样,您可以使用

PYTHONPATH
要求 GDB 忽略
gdb -eiex "set python ignore-environment on"

或者,您可以使用 (gdb) set env PYTHONPATH "C:\\OSGeo4W\\apps\\Python39"

重新设置
GDB 用于启动次级(正在调试)进程的环境。后一个设置可以在
$HOME/.gdbinit
.

© www.soinside.com 2019 - 2024. All rights reserved.