psutil
psutil copied to clipboard
[Windows] imort psutil failed when running in shell but it can import success in my IDE enviorment which the parent process is idea64.exe
Summary
- OS: Windows11
- Architecture: AMD 64bit
- Psutil version: 7.0.0
- Python version: 3.13.4
- Type: scripts
Description
I find the exact line which make my process break down.
It is from ._psutil_windows import BELOW_NORMAL_PRIORITY_CLASS line 30 at _pswindows.py
Here is my test script:
import os
import subprocess
import sys
import traceback
import pydevd_pycharm
def check_import_psutil():
"""检查 `import psutil` 是否失败,并记录详细日志"""
print("🔍 正在尝试导入 psutil...")
sys.stderr.flush() # 确保日志立即输出
try:
import psutil
print("✅ 成功导入 psutil")
except Exception as e:
print(f"❌ 导入 psutil 失败: {e}")
traceback.print_exc() # 详细错误信息
def check_sys_path():
"""检查 sys.path 是否正确"""
print("\n🔍 当前 sys.path:")
for p in sys.path:
print(f"- {p}")
def check_env_variables():
"""检查环境变量 PATH 和 PYTHONPATH 是否正确"""
print("\n🔍 当前环境变量:")
for key in ["PATH", "PYTHONPATH"]:
print(f"{key}: {os.getenv(key)}")
def check_psutil_installation():
"""检查 `psutil` 是否正确安装"""
print("\n🔍 检查 psutil 版本...")
subprocess.run(["python", "-m", "pip", "list", "|", "grep", "psutil"], stdout=sys.stdout, shell=True)
def check_python_version():
"""检查 Python 版本"""
print("\n🔍 当前 Python 版本:")
subprocess.run(["python", "-V"], stdout=sys.stdout, shell=True)
def main():
"""执行所有检查项"""
pydevd_pycharm.settrace('localhost', port=5678, stdoutToServer=True, stderrToServer=True)
# Even if I make my pwsh's sys.path and $PYTHONPATH eq to my idea64.exe enviorment, it still break down in pwsh but success in idea64.exe
check_python_version()
check_sys_path()
check_env_variables()
check_psutil_installation()
check_import_psutil()
if __name__ == "__main__":
main()