简体   繁体   中英

Configure AWS Cloud9 to use Anaconda Python Environment

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.

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).

NB

  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)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM