How to run Anaconda Python on sudo
Asked Answered
B

2

12

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?

Burl answered 16/4, 2016 at 2:16 Comment(1)
Are you sure that python is pointing to Anaconda's python? Chances are that it is not since sudo has a different PATH. Try sudo /path/to/anaconda/bin/python convnets.pyWilkey
W
21

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
Wilkey answered 16/4, 2016 at 2:25 Comment(2)
Great, this worked! Thanks, love it when the solution is so simple :)Burl
@d4tm4x it worked for meEnvenom
H
0

If your conda env is currently active:

sudo $(which python) [REST OF COMMAND]

In your case:

sudo $(which python) convnets.py >> output
Haerr answered 2/5 at 2:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.