[Python] Why my environment variables are not the same when Pycharm execute the code?

Discussão em 'Python' iniciado por Stack, Novembro 26, 2024.

  1. Stack

    Stack Membro Participativo

    ENVIRONMENT:

    Mac OS Catalina 10.15.7

    Python 3.7.13

    PROBLEM:

    My python code needs to execute some terminal commands. But I get the error message: "command not found".

    So I tested these same commands from "Terminal" and it worked perfectly.

    So I compare the environment variables loaded with my Terminal and the ones loaded with my python script. The result is they are not the same.

    You can see here the content of my .zshrc file:

    export PATH="/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home:/Users/alisa/Android/cmdline-tools/latest/bin:/Users/alisa/Android/platform-tools:/bin:/opt/local/bin:/sbin:/usr/local/bin:/usr/sbin/:/usr/bin:/usr/local/opt/qt@/bin/:/usr/local/opt/qt@6/bin/:/Users/alisa/.wdm/drivers/geckodriver/macos/v0.30.0:/Library/Java/JavaVirtualMachines/jdk-15.0.2.jdk/Contents/Home:/usr/local/Cellar/python@3.9/:/usr/local/Cellar/python@3.9/3.9.12_1/bin/:/usr/local/lib/python3.9/site-packages:/usr/local/Cellar/python@3.9/3.9.12_1/Frameworks/Python.framework/Versions/3.9/bin/"
    export ANDROID_HOME="/Users/alisa/Android"
    export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk-15.0.2.jdk/Contents/Home"
    DYLD_LIBRARY_PATH="/usr/local/lib/python3.9/site-packages/PySide2:/usr/local/lib/python3.9/site-packages/PyQt5/:/Library/Java/JavaVirtualMachines/jdk-15.0.2.jdk/Contents/Home"
    export DYLD_LIBRARY_PATH
    export DYLD_PRINT_LIBRARIES=1
    set QT_DEBUG_PLUGINS=1
    export REQUESTS_CA_BUNDLE="$HOME/.mac-ca-roots"
    export LDFLAGS="-L$(brew --prefix xz)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib"
    export CPPFLAGS="-I$(brew --prefix xz)/include -I$(brew --prefix readline)/include -I$(brew --prefix zlib)/include -I$(xcrun --show-sdk-path)/usr/include"
    alias python3=/usr/local/opt/python@3.7/bin/python3
    APPDATA="/users/alisa"
    export APPDATA

    TESSDATA_PREFIX="/opt/local/share/tessdata"
    export TESSDATA_PREFIX
    export PATH="/usr/local/sbin:$PATH"
    alias python3="/usr/local/Cellar/python@3.9/3.9.12_1/Frameworks/Python.framework/Versions/3.9/bin/python3.9"
    export PYTHONPATH=/usr/local/lib/python3.9/site-packages/


    And here the values of environment variables loaded with my python code:

    HOME:/Users/alisa
    LC_CTYPE:en_GB.UTF-8
    LD_LIBRARY_PATH:/usr/local/Cellar/opencv/4.5.5_2/lib/python3.9/site-packages/cv2/../../../../lib:
    LOGNAME:alisa
    PATH:/usr/bin:/bin:/usr/sbin:/sbin
    PWD:/Volumes/GoogleDrive/My Drive/Phonebot/Phonebot_debug3
    PYCHARM_HOSTED:1
    PYTHONIOENCODING:UTF-8
    PYTHONPATH:/Volumes/GoogleDrive/My Drive/Phonebot/Phonebot_debug3
    PYTHONUNBUFFERED:1
    SHELL:/bin/zsh
    SSH_AUTH_SOCK:/private/tmp/com.apple.launchd.OJCYKYn5oR/Listeners
    TMPDIR:/var/folders/gg/d60v4x454xd_n7dzwkv_y9140000gn/T/
    USER:alisa
    XPC_FLAGS:0x0
    XPC_SERVICE_NAME:com.jetbrains.pycharm.20596
    __CF_USER_TEXT_ENCODING:0x1F5:0x0:0x2


    You can see the PATH are not the same and so that is why I get the error "command not found".

    Here is the Python code I use to display the environment variable:

    def GetEnvVariables():
    dico_env={}

    for k, v in sorted(os.environ.items()):
    #print(k+':', v)
    dico_env[k]=v
    print(f"dico_env:{dico_env}")
    return dico_env

    dico=mymodules.GetEnvVariables()
    for key, value in dico.items():
    print('%s:%s\n' % (key, value))


    I also tried with this command:

    print(os.environ.get('PATH'))


    And here is the output:

    /usr/bin:/bin:/usr/sbin:/sbin


    Again, the PATH value is not the same than my .zshrc file

    I am using pycharm to execute my Python code. So I run my code from terminal and this time it worked! I stopped and restart Pycharm, and I still have same issue.

    Does anyone know how can I modify Pycharm to consider all my environment variables?

    Thanks

    Continue reading...

Compartilhe esta Página