pythonamazon-web-servicesrootanacondasudo

How to run Anaconda Python on sudo


Currently using AWS to run some tests on a machine learning project. I would like to run Python scripts without internet (via root) because the internet bandwidth is extremely limited. I try to run the convnets.py script by doing

sudo python convnets.py >> output

But that does not work, as Anaconda does not use PYTHONPATH, making it impossible for root to find the Anaconda Python environment. So errors like "cannot import" and "module not found" are thrown.

How do I set this up so I can get Anaconda and sudo to play fair together?


Solution

  • Because using sudo uses a different PATH than your typical environment, you need to be sure to specify that you want to use Anaconda's python interpreter rather than the system python. You can check which one is being run with the following command

    sudo which python
    

    To fix this, and point to Anaconda's python interpreter, specify the full path to the correct interpreter.

    sudo /path/to/anaconda/bin/python convnets.py >> output
    

    If you do this, you should be able to access all of the modules managed by anaconda.

    On the other hand, if you have an Anaconda environment created

    conda create --name $ENVIRONMENT_NAME python
    

    You can activate it prior to running your command

    sudo source activate $ENVIRONMENT_NAME && python convnets.py >> output