paganotti
paganotti

Reputation: 5661

python doesn't get environment variable but it is set on mac os

if I run env command I obtain this output:

TERM_PROGRAM=Apple_Terminal
GPG_AGENT_INFO=/Users/paganotti/.gnupg/S.gpg-agent:346:1
TERM=xterm-color
SHELL=/bin/bash
TMPDIR=/var/folders/BM/BMT-0W4+H5yVS5fJngFdsk+++TI/-Tmp-/
Apple_PubSub_Socket_Render=/tmp/launch-V50MvM/Render
TERM_PROGRAM_VERSION=273.1
USER=paganotti
COMMAND_MODE=unix2003
SSH_AUTH_SOCK=/tmp/launch-llkTBf/Listeners
__CF_USER_TEXT_ENCODING=0x1F5:0:0
PATH=/Library/Frameworks/Python.framework/Versions/2.7/bin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/3.2/bin:/Users/paganotti/Documents/Project/ZendFramework-1.11.11-minimal/bin/zf.sh:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin://usr/local/git/bin/git:/Library/Frameworks/Python.framework/Versions/3.1/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/local/ImageMagick/bin:/usr/local/MacGPG2/bin:/usr/texbin:/usr/X11/bin:/Users/paganotti/Sviluppo/android-sdk-1.5_r3/tools
PWD=/Users/paganotti
LANG=it_IT.UTF-8
SHLVL=1
HOME=/Users/paganotti
LOGNAME=paganotti
GIT_PYTHON_GIT_EXECUTABLE=/usr/local/git/bin/git
DISPLAY=/tmp/launch-aqcM4a/org.x:0
_=/usr/bin/env

I'm try get GIT_PYTHON_GIT_EXECUTABLE variable with python:

    GIT_PYTHON_GIT_EXECUTABLE = os.getenv('GIT_PYTHON_GIT_EXECUTABLE')
    print GIT_PYTHON_GIT_EXECUTABLE

but output print:

    None

because it seems not view GIT_PYTHON_GIT_EXECUTABLE variable? What's wrong?

UPDATE

I set environment variable in pycharm and now it read that, but in my real application i use QThread class of pyqt library. My application build on top of pyqt library when start, it execute a Qthread. In Qthread would to execute a gitpython functions. GitPython functions start a subprocess and try to read a GIT_PYTHON_GIT_EXECUTABLE, but it can't read variable.

Upvotes: 2

Views: 7288

Answers (1)

Dunes
Dunes

Reputation: 40703

In order for the python interpreter to get access to an environment variable you set, you must set the environment before you invoke Pycharm, and you must run Pycharm from the shell where you set the environment variable. Otherwise Pycharm won't get access to the variable, and nor will any of its child processes (python).

The reason is that when you set an environment variable it is not global to the entire machine, just to the current shell and its sub processes. And each sub proccess only gets a snapshot of the parent's environment when it starts. Future changes to the environment in the parent are not reflected in the child process (and vice versa).

You can make the variable global to the whole system by editing a file called .profile (or preferably .bash_profile if you are using bash) in your home directory. If they do not exist, just create them. At the file just add the export command that you want. This file gets executed when you first login to your computer (so you will need to relogin for this to work). All future programs after you login will have access to the environment variables set in the profile file. This works because when you login a new shell is spawned from which every other process you run will be run from.

Upvotes: 6

Related Questions