Executing .py files from the shell

Executing .py files from the shell#

Learning Objectives#

After working through this topic, you should be able to:

  • Activate a conda environment in your shell

  • Execute python files from your shell

Materials#

We assume that you have already watched the screencasts on installation and have a fully working conda installation on your computer.

We also assume that you have watched the screencasts on the shell and know how to navigate to directories in a shell.

Here is the screencast and these are the slides.

Quiz#

from jupyterquiz import display_quiz

content = [
    {
        "question": (
            "Your project's folder contains the following files: exercise_1.py, \
            a folder called data, and a folder called results. Your shell is currently \
            in the data folder. Which of the following commands would you use to run \
            the exercise_1.py script?"
        ),
        "type": "many_choice",
        "answers": [
            {
                "answer": "'python exercise_1.py'",
                "correct": False,
                "feedback": "This would work if you were in the folder containing \
                exercise_1.py.",
            },
            {
                "answer": "'python ../exercise_1.py'",
                "correct": True,
                "feedback": "This works because ../ moves you up one folder, \
                    and then you can run the script from there.",
            },
            {
                "answer": "'python ~/exercise_1.py'",
                "correct": False,
                "feedback": "This would work if you were in your home folder (on a \
                    Mac system).",
            },
            {
                "answer": "'cd ..' and then 'python exercise_1.py'",
                "correct": True,
                "feedback": "This works because cd .. moves you up one folder, \
                    and then you can run the script from there.",
            },
        ],
    },
]
display_quiz(content, colors="fdsp")