简体   繁体   中英

Python, Pycharm - missing environment variables in os.environ

Using Python 3.6, PyCharm 2019.3.1 CE, 64-bits Ubuntu 18.04

I have an environment variable that is accessed fine when running my python script from the command line, but does not show up when running with PyCharm.

This is the script:

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

From the terminal, this is my output:

:/usr/lib/ids/cti

While with PyCharm, this is my output:

None

What is happening here? Why does the environment variable not show up when running with PyCharm?

When you run python from your terminal, the python script inherits the environment variables from the shell, which in turn gets GENICAM_GENTL64_PATH through a startup script in /etc/profile.d. (see docs ).

In contrast, when you start the script from PyCharm, there is no shell - PyCharm starts the python process itself - so you won't have the environment variables that populated by /etc/profile.d.

You can solve it by launching PyCharm from your terminal, as described here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM