Configure AWS Cloud9 to use Anaconda Python Environment
Asked Answered
H

4

9

I want AWS Cloud9 to use the Python version and specific packages from my Anaconda Python environment. How can I achieve this? Where should I look in the settings or configuration?

My current setup: I have an AWS EC2 instance with Ubuntu Linux, and I have configured AWS Cloud9 to work with the EC2 instance.

I have Anaconda installed on the EC2 instance, and I have created a conda Python3 environment to use, but Cloud9 always wants to use my Linux system's installed Python3 version.

Heeler answered 9/1, 2019 at 2:25 Comment(1)
I found a related question, with no answer unfortunately... #52109086Heeler
H
6

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.

Heeler answered 19/1, 2019 at 22:56 Comment(0)
M
2

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)
Mannerless answered 15/8, 2019 at 8:45 Comment(3)
I am getting "AssertionError: SRE module mismatch" error when I run a file with my environment. Any suggestions?Fluoroscopy
@SinanGok did you install ikp3db into your conda environment? It sounds like you conda environment is lacking a python package that it needs..Mannerless
I actually fixed that problem by updating conda with python=3.6. My custom runner, where I replaced the python path as you suggested above, did not work yesterday for some reason. But it works now. Thanks.Fluoroscopy
M
1

You could use a 'shell script' runner type. To do this you would:

  1. create your conda env, with python3 and any packages etc you want in it. Call it py3env
  2. create a directory to hold your runner scripts, something like $HOME/c9_runner_scripts
  3. put a script in there called py3env_runner.sh runner with code like:

    conda activate py3env python ~/c9/my_py3_script.py

  4. Then create a run configuration with the 'shell script' runner type and enter c9_runner_scripts/py3env_runner.sh

Mannerless answered 31/7, 2019 at 15:52 Comment(0)
S
-1

for me, on centos 7 the only way to execute with my conda python v 3.9.4 was to add a conda activate line to my .bash_profile like this:

conda activate /var/www/my_conda/python3.9

Then in Cloud 9 when I'm running my code under my conda python 3.9 env all is fine.

This is my simple python code which will print the current python version

import sys
print(sys.version)

Best.

Seely answered 29/4, 2021 at 5:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.