如何在 sudo 上运行 Anaconda Python

问题描述

目前使用 AWS 在机器学习项目上运行一些测试.我想在没有互联网的情况下(通过 root)运行 Python 脚本,因为互联网带宽非常有限.我尝试通过执行来运行 convnets.py 脚本

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

但这不起作用,因为 Anaconda 不使用 PYTHONPATH,因此 root 无法找到 Anaconda Python 环境.因此会抛出无法导入"和找不到模块"等错误.

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.

如何进行设置,以便让 Anaconda 和 sudo 公平竞争?

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


解决方案

由于使用 sudo 使用的 PATH 与您的典型环境不同,因此您需要确保指定你想使用 Anaconda 的 python 解释器而不是系统 python.您可以使用以下命令检查正在运行的是哪一个

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

要解决此问题,并指向 Anaconda 的 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

如果你这样做,你应该能够访问所有由 anaconda 管理的模块.

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

另一方面,如果您创建了 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

相关文章