0

I am using Python 3.8 under Anaconda3 on a Ubuntu 18.04 machine, and I am trying to use cuda toolkit 11.2. I have set "CUDA_HOME" properly such that both echo $CUDA_HOME and sudo echo $CUDA_HOME can return the correct CUDA_HOME value. My python script looks something like this:

import os
print(os.environ.get('CUDA_HOME'))

When I execute python myscript.py, the output is fine. However, when I use sudo python myscript.py, the result is None. How can I resolve this issue?

talonmies
  • 70,661
  • 34
  • 192
  • 269
edwardliang2019
  • 302
  • 2
  • 9
  • When you do `sudo echo $CUDA_HOME`, the variable is being expanded in the original shell, not the shell run by `sudo`. Try `sudo bash -c 'echo $CUDA_HOME'` and you'll see the difference. – Barmar Apr 12 '22 at 00:09
  • @Barmar Thanks for the response, and you are right! Using your command does not seem to return anything. Any idea why this is happening? – edwardliang2019 Apr 12 '22 at 00:11
  • 1
    Because `sudo` clears most environment variables. – Barmar Apr 12 '22 at 00:15

0 Answers0