How to access environmental variable from Python script?

Hi, I have the following script that works flawlessly when the API key for DeepL is written into the script. It does not work however when using os.getenv('DEEPL_API_KEY', ''). The environment variable is properly set.

# #popclip
# popclip version: 4069
# name: DeepL β†’ English
# icon: iconify:twemoji:flag-united-kingdom
# interpreter: /Users/***/***/***/venv/bin/python3
# after: preview-result

import deepl # https://github.com/DeepLcom/deepl-python
import os

auth_key = os.getenv('DEEPL_API_KEY', '').strip()
translator = deepl.Translator(auth_key)

originaltext = os.environ['POPCLIP_TEXT']

result = translator.translate_text(originaltext, source_lang="DE", target_lang="EN-GB")
print(result.text, end="")

I assume my ~/.zshrc is not used because of the way PopClip runs scripts? Any ideas how to fix this? Thanks!

Kolkrabe

Hi @Kolkrabe β€”

PopClip runs scripts in a non-interactive zsh shell.

In this mode, zsh reads .zshenv but does not read .zshrc (because the shell isn’t interactive).

So if you want environment variables to be available to PopClip scripts, you should define them in .zshenv.

1 Like

Thanks!