0

In shell script , I'm able to export a variable. How do I do the same in Python

Shell code:

PYTHON_PATH=`which python`    
PYTHON_PATH=`dirname $PYTHON_PATH`    
PATH=/bin:/usr/bin    
export PATH=~/.local/bin:$PATH:$PYTHON_PATH

Python code:(Version - 2.7)

PYTHON_PATH = subprocess.check_output("which python", shell=True)    
PYTHON_PATH = os.path.dirname(PYTHON_PATH)    
PATH = os.path.expanduser('~') + "/.local/bin:" + PATH + ":" + PYTHON_PATH    
subprocess.call('export PATH="{}"'.format(PATH), shell=True)

Is this right way to export a PATH variable in python.

Inian
  • 80,270
  • 14
  • 142
  • 161
sabarish s
  • 49
  • 1
  • 6

1 Answers1

0

Does this answer your question (credit: this question/answer)?

import subprocess
import os

PYTHON_PATH = subprocess.check_output("which python", shell=True)    
PYTHON_PATH = os.path.dirname(PYTHON_PATH)    

os.environ["PATH"] += os.pathsep + PYTHON_PATH.decode()
Nic
  • 3,365
  • 3
  • 20
  • 31