Python、Pycharm - os.environ 中缺少环境变量

Python, Pycharm - missing environment variables in os.environ

使用 Python 3.6,PyCharm 2019.3.1 CE,64 位 Ubuntu 18.04

我有一个环境变量,当 运行 我的 python 脚本从命令行访问时可以正常访问,但当 运行 PyCharm 时不显示。

这是脚本:

import os
print(os.environ.get("GENICAM_GENTL64_PATH"))

从终端,这是我的输出:

:/usr/lib/ids/cti

使用 PyCharm 时,这是我的输出:

None

这里发生了什么?为什么环境变量在运行 PyCharm时不显示?

当您从终端 运行 python 时,python 脚本从 shell 继承环境变量,后者又通过 GENICAM_GENTL64_PATH /etc/profile.d 中的启动脚本。 (参见 docs)。

相比之下,当您从 PyCharm 启动脚本时,没有 shell - PyCharm 启动 python 进程本身 - 所以您不会/etc/profile.d.

填充的环境变量

您可以通过从您的终端启动 PyCharm 来解决它,如 here 所述。