Configure AWS Cloud9 to use Anaconda Python Environment

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 09:37:28

问题


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.


回答1:


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.




回答2:


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




回答3:


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)


来源:https://stackoverflow.com/questions/54102477/configure-aws-cloud9-to-use-anaconda-python-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!