Configure AWS Cloud9 to use Anaconda Python Environment

I finally found something that forces AWS Cloud9 to use the Python3 version installed in my Anaconda environment on my AWS EC2 instance.

The instructions to create a custom AWS Cloud9 runner for Python are here:

{
    "cmd" : ["/home/ubuntu/anaconda3/envs/ijackweb/bin/python3.6", "$file", "$args"],
    "info" : "Running $project_path$file_name...",
    "selector" : "source.py"
}

I just create a new runner and paste the above code in there, and Cloud9 runs my application with my Anaconda environment's version of Python3.

The only thing I don't understand about the above code is what the "selector": "source.py" line does.


After some testing, I realised that my previous answer prevents you being able to use the debugger. Building on @Sean_Calgary 's answer (which is better than my original answer), you can edit one of the in-built python runners (again, just replacing the python call with the full path to the conda env's python path), like so:

    {
  "script": [
    "if [ \"$debug\" == true ]; then ",
    "    /home/tg/miniconda/envs/env-name/bin/python -m ikp3db -ik_p=15471 -ik_cwd=$project_path \"$file\" $args",
    "else",
    "   /home/tg/miniconda/envs/env-name/bin/python \"$file\" $args",
    "fi",
    "checkExitCode() {",
    "    if [ $1 ] && [ \"$debug\" == true ]; then ",
    "        /home/tg/miniconda/envs/env-name/bin/python -m ikp3db 2>&1 | grep -q 'No module' && echo '",
    "    To use python debugger install ikpdb by running: ",
    "        sudo yum update;",
    "        sudo yum install python36-devel;",
    "        sudo pip-3.6 install ikp3db;",
    "        '",
    "    fi",
    "   return $1",
    "}",
    "checkExitCode $?"
  ],
  "python_version": "python3",
  "working_dir": "$project_path",
  "debugport": 15471,
  "$debugDefaultState": false,
  "debugger": "ikpdb",
  "selector": "^.*\\.(py)$",
  "env": {
    "PYTHONPATH": "$python_path"
  },
  "trackId": "Python3"
}

To do this, just click on 'runners' next to CWD in the bottom-right corner -> python3 -> edit runner -> save as 'env-name.run' in /.c9/runners (that save as should point you to the right directory by default).

N.B.

  1. Replace env-name with the name of your environment throughout.
  2. You will need the package for the debugger installed in your conda env. It's called ikp3db.
  3. You may need to check the path to your conda envs executable python by activating the environment and running which python (his caught me out because my path ended in /python, not /python3.6, even though it's python 3.6 that's installed)