0

I'd like to get shell variable content to be used within a python script, specifically to get path out of it to be used later for a function call to a python script located at another directory, using sys.path.insert

Let's assume my shell variable is called SHELL_VAR1, and I want to concatenate to it /scripts folder name, so I get flow like that - using in this example os.environ.get just to illustrate my need in case it was environment variable and not a shell variable - and the question is what should be used instead:

python -c 'import os; import sys;var1=os.environ.get('SHELL_VAR1'); sys.path.insert(1, 'var1' ''/scripts'');' 
3
  • Does this answer your question? How to call an external command? Commented Sep 15, 2020 at 16:22
  • No, as far as I see, since I want to embed the result within the python script -Jakob's answer below helped me, thanks Commented Sep 15, 2020 at 16:53
  • and the question there is more generalized than mine, so I get 'lost' with the answers... Commented Sep 15, 2020 at 17:04

1 Answer 1

1

A shell variable and environment variable are the same thing, in my understanding.

You can use pathlib for OS-agnostic filepath operations (like joining filepaths).

import os
from pathlib import Path
import sys

# This will raise a KeyError if the key is not in os.environ.
var1 = os.environ["SHELL_VAR1"]
path = Path(var1) / "scripts"
sys.path.insert(1, str(path))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.