0

Need to add this path --> /home/ubuntu/folderA to my PATH environment variable in linux ubuntu.

Update

print(os.environ["PATH"])

path = "/home/ubuntu/folderA"
os.environ["PATH"] += os.pathsep + path

print(os.environ["PATH"])
#Can see the newly added path here when i print

However, i am still unable to access the path.

And when i do printenv, i still do not see the path.

Tried os.getenv("PATH") to see if path has been updated but it is still the same thing

If i do it manually using the following command, then useprintenv, the path is there.

export PATH=$PATH:/home/ubuntu/folderA

What I need is to update the path using the first method in my python script then carry on which the rest of my methods.

Hopefully someone can take a look. Thank you.

Final update: Turns out the path is appended when i run my python script. Just that after the script is completed, the path will not persist. Hence when i do printenv, the path is not showing up.

Mason
  • 173
  • 5
  • 23
  • `sys.path` is not the `PATH` environment variable, and environment variables are local to a process. – melpomene Jul 29 '19 at 06:37
  • 1
    What do you mean by "*my script still fails to find the newly added path*"? Please provide a [mcve]. – melpomene Jul 29 '19 at 06:51
  • Are you using one python script to add path and another script to access the added path? – ObiWan Jul 29 '19 at 07:04
  • Also please explain what you are trying to accomplish in little detail! – ObiWan Jul 29 '19 at 07:07
  • @ssokhey I am trying to use only one script to add path and the same script to access the added path. – Mason Jul 29 '19 at 07:10
  • @Mason What process is trying to access the added path? Native Python or something else. You need to elaborate or we can't help. – ObiWan Jul 29 '19 at 07:24
  • @ssokhey Hmm. I actually went to create a separate python file on my own (with the codes above). Without even accessing the added path, but when i do printenv, i still do not see the path there. – Mason Jul 29 '19 at 07:30

3 Answers3

2

You can use os.environ["PATH"].

Check this question to get ideas.

ObiWan
  • 196
  • 1
  • 12
1

sys.path is of type list. Therefore you should use the built-in append() method to add a string.

import sys
sys.path.append("/home/ubuntu/folderA")

The down-votes led me to look further into the difference between sys.path and os.environ\['PATH'\].

For updating the path variable for the current user, use os.environ['PATH'].

A point to note is that the path variable is a string object and updating it inside a loop will consume memory. Use the following method if you're updating from a list.

# os.pathsep is the character that separates different paths in the path variable
os.environ["PATH"] += os.pathsep + os.pathsep.join(pathlist)
sidx
  • 640
  • 2
  • 11
  • 28
1

This is because on Unix like systems the environment is unique to each process.

What is happening is that when you run your script the OS copies current environment variables into the new environment. In that new environment your script modifies the PATH variable. After your script exits, that environment is destroyed.

Try running running this to see what I am talking about:

path = "/home/ubuntu/folderA"
os.environ["PATH"] += os.pathsep + path
print(os.environ["PATH"])
os.system('printenv')

You should see your modified PATH variable.

If you want to modify the PATH variable for further processes you have to do one of the following.

  1. Launch a bash shell from your script. Using os.system('bash') this will start a child shell process which will inherit the PATH variable form your script.

  2. Preferred way is to change your script to something like this

     path = "/home/ubuntu/folderA"
     print('export PATH=' + os.environ["PATH"] += os.pathsep + path)
    

    And then execute your script like this

     eval $(python path.py)
    

    This will get your the modified path in the shell where you launched your script. Note that on Windows type systems things are completely different

Vlad
  • 9,180
  • 5
  • 48
  • 67