Public course previewThis is not your assigned passport. No identity, answers, or completion progress are stored here.How to start your passport
Guided lesson
02 / 06About 25 minuteschanges practice files

Create A Small Python Environment On Euler

Create or keep one small Euler-native Python environment at its final path. The guarded checks stop safely for an incomplete or moved environment.

Where you will workOn your computer first, then on the remote Euler computer
  1. 1Understand
  2. 2Try it yourself
  3. 3Check and submit

Before you start#

Python is a programming language run by a program called an interpreter. Your laptop and Euler are separate computers; copying an installed environment between them is not a reproducible setup.

Selection Lifetime What it provides
Euler software modules Current shell Training software stack and Python
Project environment at its final path Stored files Isolated project interpreter and packages
Activation Current shell Selects that environment for commands here

A new Slurm batch script must select its module and environment again. A check in a temporary shell verifies the installation without keeping your parent terminal activated. The command's explicit path is the installation target.

Predict which selections a fresh job needs before opening the batch fragment. First inspect the target; do not replace a valid environment.

Read the environment checks

The inspection checks ownership, activation path, Python and both pip entrypoints. Creation exclusively claims an empty final directory; a failure is retained for safe recovery. Only the complete verification prints euler-python-env-ok. An absent success marker is a stop, not permission to delete the directory.

Create at the final path and retain a failed target for safe recovery. Select the module and environment inside each job.

Start the practice

Practice

Follow the steps

Follow in order

Steps

In Euler Bash, inspect the final training path before creating anything. Keep a valid environment; stop for safe recovery if checks fail.

Optional: using the terminal

A command is a line of text that tells a computer to do one task. You paste it into a terminal, the text application used for commands. A shell is the part inside that application that reads the command. PowerShell, zsh, and Bash are shells. Open only the application named for your computer below; it starts its configured shell. Follow the shell named below. You do not need to install a separate Bash or zsh application.

Your computer or local means the laptop or desktop in front of you. Remote means another computer reached through the network. A command affects whichever computer the step names, so check that label before pasting.

  • Windows: press the Windows key, type PowerShell, and open the result named PowerShell. Do not use Command Prompt, Git Bash, or WSL for a block labelled PowerShell.
  • macOS: press Command+Space, type Terminal, and press Return. Terminal normally starts the zsh shell. If it uses another shell, run zsh for this session.
  • Linux: open the app named Terminal from the applications menu. It normally starts the Bash shell. If it uses another shell, run bash for this session.

The prompt is the text beside the blinking cursor. For each card, check the named computer, choose Copy, paste only the command after the prompt, press Enter once, and wait for the prompt to return.

If you have never done this before, first run the harmless terminal-ready practice on the start page. It lets you practise copying, pasting, pressing Enter, and reading output before a setup command.

Practise one harmless command first

Euler card: Euler is ETH Zurich's service made of many managed computers for research calculations. SSH is the protected connection that opens a Bash shell on Euler in the same terminal window. Connect first, then run the card there, not on your laptop.

How to open a terminal and run a command
  1. 01

    Separate laptop and Euler software

    Read and answer on this web page

    Python is a programming language run by a program called an interpreter. Your laptop and Euler are separate computers. Use the software modules/project environment table above. Each Slurm batch script starts its own shell and must select both again.

    Look forYou know why a laptop environment is not copied to Euler and why a job script activates its own environment.

    Explanation and help
    What this means
    Connect to Euler with the tested alias.
    Safe recovery
    Read the linked Euler Python reference again. You do not need the laptop Python track unless it appears in your assigned route.
  2. 02

    Connect to Euler

    On your computer

    From the local terminal, connect with the verified euler alias. Run every later command in the Euler Bash shell that opens.

    RunPowerShell on Windows
    ssh euler
    RunTerminal on macOS (zsh)
    ssh euler
    RunTerminal on Linux (Bash)
    ssh euler

    Look forThe prompt changes to an Euler login node.

    If this differsReturn to the SSH mission. Do not run Euler commands in the local shell.

    Explanation and help
    What this means
    Check the documented Python module.
  3. 03

    Check the Euler Python module

    Use Euler after you connect from your computer

    Check the dated software stack used by this training release. This command loads it in a temporary shell; the later checks load it again for their own work.

    RunThis text window after SSH connects to Euler
    (
    set -eu
    module purge
    module load stack/2024-06 python/3.11.6
    version="$(python --version 2>&1)"
    printf '%s\n' "$version"
    [ "$version" = "Python 3.11.6" ] || { printf 'STOP: expected Python 3.11.6, got %s\n' "$version" >&2; exit 1; }
    printf 'module-python-ok\n'
    )

    Look forThe final two lines are Python 3.11.6 and module-python-ok.

    If this differsRun module spider python and use the help path with its non-secret output. Do not guess a replacement module.

    Explanation and help
    What this means
    Inspect the dedicated environment path.
  4. 04

    Inspect before creating

    Use Euler after you connect from your computer

    Inspect the exact training path. This checks ownership, activation, the selected Python, and both ways of starting pip. It keeps a valid environment and stops if an old moved environment or an incomplete directory is found.

    RunThis text window after SSH connects to Euler
    (
    set -eu
    umask 077
    module purge
    module load stack/2024-06 python/3.11.6
    [ "$(python -c 'import platform; print(platform.python_version())')" = 3.11.6 ] || {
      printf 'STOP: the loaded module must provide Python 3.11.6.\n' >&2; exit 1
    }
    root="$HOME/passport-euler"
    base="$root/venvs"
    venv="$base/passport-python"
    # <!-- passport-snippet:euler-environment-check -->
    check_directories() {
      python - "$@" <<'PY'
    import os, stat, sys
    for name in sys.argv[1:]:
        info = os.lstat(name)
        if not stat.S_ISDIR(info.st_mode) or info.st_uid != os.getuid() or info.st_mode & 0o022:
            raise SystemExit("STOP: training paths must be real directories owned by you, without group/other write access.")
    PY
    }
    verify_environment() (
      check_directories "$root" "$base" "$venv" "$venv/bin" || exit 1
      [ -f "$venv/pyvenv.cfg" ] && [ ! -L "$venv/pyvenv.cfg" ] &&
        [ -f "$venv/bin/activate" ] && [ ! -L "$venv/bin/activate" ] &&
        [ -x "$venv/bin/python" ] && [ -x "$venv/bin/pip" ] || {
        printf 'STOP: the training environment is incomplete or unsafe.\n' >&2; exit 1
      }
      . "$venv/bin/activate" || exit 1
      [ "${VIRTUAL_ENV:-}" = "$venv" ] && [ "$(command -v python)" = "$venv/bin/python" ] || {
        printf 'STOP: activation points to another location; use the training-environment recovery instructions.\n' >&2; exit 1
      }
      python - "$venv" <<'PY' || exit 1
    import pathlib, platform, sys
    import pip
    expected = pathlib.Path(sys.argv[1])
    assert pathlib.Path(sys.prefix).resolve() == expected.resolve(), "wrong environment prefix"
    assert pathlib.Path(sys.executable).parent.resolve() == (expected / "bin").resolve(), "wrong interpreter directory"
    assert pathlib.Path(sys.executable).name == "python", "wrong interpreter"
    assert platform.python_version() == "3.11.6", "wrong Python version"
    assert expected.resolve() in pathlib.Path(pip.__file__).resolve().parents, "pip belongs to another environment"
    PY
      module_pip="$(python -m pip --version)" || exit 1
      entry_pip="$("$venv/bin/pip" --version)" || exit 1
      [ "$module_pip" = "$entry_pip" ] || { printf 'STOP: pip entrypoint uses another environment.\n' >&2; exit 1; }
      printf '%s\n' "$module_pip"
      deactivate
    )
    # <!-- /passport-snippet:euler-environment-check -->
    for directory in "$root" "$base"; do
      if [ -e "$directory" ] || [ -L "$directory" ]; then check_directories "$directory"; fi
    done
    if [ ! -e "$venv" ] && [ ! -L "$venv" ]; then
      printf 'environment-target-available\n'
    else
      [ ! -e "$venv/.passport-create-in-progress" ] || {
        printf 'STOP: a creation may still be running; do not move this environment. Ask lab IT if interrupted.\n' >&2; exit 1
      }
      verify_environment
      printf 'existing-environment-ok\n'
    fi
    )

    Look forThe final line is environment-target-available or existing-environment-ok.

    If this differsFollow the linked recovery procedure only for this Passport training environment. Leave unfamiliar paths, shared permissions, and in-progress creations for lab IT to inspect.

    Explanation and help
    What this means
    Create only when absent, otherwise reuse it.
  5. 05

    Create or reuse the environment

    Use Euler after you connect from your computer

    If inspection printed environment-target-available, open and run the creation command. It claims an empty final directory and verifies the new environment. If inspection printed existing-environment-ok, keep it and go directly to Verify the active interpreter. If inspection stopped, use its safe recovery before continuing. A failed creation is retained; nothing is recursively deleted or overwritten.

    Create only after environment-target-available (This text window after SSH connects to Euler)
    RunThis text window after SSH connects to Euler
    (
    set -eu
    umask 077
    module purge
    module load stack/2024-06 python/3.11.6
    [ "$(python -c 'import platform; print(platform.python_version())')" = 3.11.6 ] || {
      printf 'STOP: the loaded module must provide Python 3.11.6.\n' >&2; exit 1
    }
    root="$HOME/passport-euler"
    base="$root/venvs"
    venv="$base/passport-python"
    # <!-- passport-snippet:euler-environment-check -->
    check_directories() {
      python - "$@" <<'PY'
    import os, stat, sys
    for name in sys.argv[1:]:
        info = os.lstat(name)
        if not stat.S_ISDIR(info.st_mode) or info.st_uid != os.getuid() or info.st_mode & 0o022:
            raise SystemExit("STOP: training paths must be real directories owned by you, without group/other write access.")
    PY
    }
    verify_environment() (
      check_directories "$root" "$base" "$venv" "$venv/bin" || exit 1
      [ -f "$venv/pyvenv.cfg" ] && [ ! -L "$venv/pyvenv.cfg" ] &&
        [ -f "$venv/bin/activate" ] && [ ! -L "$venv/bin/activate" ] &&
        [ -x "$venv/bin/python" ] && [ -x "$venv/bin/pip" ] || {
        printf 'STOP: the training environment is incomplete or unsafe.\n' >&2; exit 1
      }
      . "$venv/bin/activate" || exit 1
      [ "${VIRTUAL_ENV:-}" = "$venv" ] && [ "$(command -v python)" = "$venv/bin/python" ] || {
        printf 'STOP: activation points to another location; use the training-environment recovery instructions.\n' >&2; exit 1
      }
      python - "$venv" <<'PY' || exit 1
    import pathlib, platform, sys
    import pip
    expected = pathlib.Path(sys.argv[1])
    assert pathlib.Path(sys.prefix).resolve() == expected.resolve(), "wrong environment prefix"
    assert pathlib.Path(sys.executable).parent.resolve() == (expected / "bin").resolve(), "wrong interpreter directory"
    assert pathlib.Path(sys.executable).name == "python", "wrong interpreter"
    assert platform.python_version() == "3.11.6", "wrong Python version"
    assert expected.resolve() in pathlib.Path(pip.__file__).resolve().parents, "pip belongs to another environment"
    PY
      module_pip="$(python -m pip --version)" || exit 1
      entry_pip="$("$venv/bin/pip" --version)" || exit 1
      [ "$module_pip" = "$entry_pip" ] || { printf 'STOP: pip entrypoint uses another environment.\n' >&2; exit 1; }
      printf '%s\n' "$module_pip"
      deactivate
    )
    # <!-- /passport-snippet:euler-environment-check -->
    for directory in "$root" "$base"; do
      if [ ! -e "$directory" ] && [ ! -L "$directory" ]; then
        mkdir -m 700 -- "$directory" || { printf 'STOP: a training parent appeared concurrently; inspect again.\n' >&2; exit 1; }
      fi
      check_directories "$directory"
    done
    if [ -e "$venv" ] || [ -L "$venv" ]; then
      [ ! -e "$venv/.passport-create-in-progress" ] || {
        printf 'STOP: a creation may still be running; do not move this environment. Ask lab IT if interrupted.\n' >&2; exit 1
      }
      verify_environment
      printf 'existing-environment-kept\n'
    else
      # Claim the final directory exclusively; never run venv over an existing target.
      mkdir -m 700 -- "$venv" || { printf 'STOP: the target appeared concurrently; inspect again.\n' >&2; exit 1; }
      mkdir -- "$venv/.passport-create-in-progress"
      trap 'rmdir -- "$venv/.passport-create-in-progress"' EXIT
      trap 'exit 1' HUP INT TERM
      printf 'passport-python-training-v1\n' > "$venv/.passport-training-environment"
      # venv records absolute paths. Create and test it at its permanent location.
      python -m venv "$venv" || {
        printf 'STOP: creation failed; the partial training directory was kept for safe recovery.\n' >&2; exit 1
      }
      verify_environment
      printf 'created-environment-ok\n'
    fi
    )

    Look forThe command prints existing-environment-kept or created-environment-ok and does not overwrite an existing path.

    If this differsKeep the first error and follow the linked recovery procedure. Do not rerun venv over an existing target, use sudo, or install into base.

    Explanation and help
    What this means
    Activate and verify the exact interpreter.
  6. 06

    Verify the active interpreter

    Use Euler after you connect from your computer

    Activate the training environment in this temporary shell and check its activation path, selected interpreter, Python version, and pip entrypoint. The success marker appears only after every check passes. Enter only that marker in the Passport.

    RunThis text window after SSH connects to Euler
    (
    set -eu
    umask 077
    module purge
    module load stack/2024-06 python/3.11.6
    [ "$(python -c 'import platform; print(platform.python_version())')" = 3.11.6 ] || {
      printf 'STOP: the loaded module must provide Python 3.11.6.\n' >&2; exit 1
    }
    root="$HOME/passport-euler"
    base="$root/venvs"
    venv="$base/passport-python"
    # <!-- passport-snippet:euler-environment-check -->
    check_directories() {
      python - "$@" <<'PY'
    import os, stat, sys
    for name in sys.argv[1:]:
        info = os.lstat(name)
        if not stat.S_ISDIR(info.st_mode) or info.st_uid != os.getuid() or info.st_mode & 0o022:
            raise SystemExit("STOP: training paths must be real directories owned by you, without group/other write access.")
    PY
    }
    verify_environment() (
      check_directories "$root" "$base" "$venv" "$venv/bin" || exit 1
      [ -f "$venv/pyvenv.cfg" ] && [ ! -L "$venv/pyvenv.cfg" ] &&
        [ -f "$venv/bin/activate" ] && [ ! -L "$venv/bin/activate" ] &&
        [ -x "$venv/bin/python" ] && [ -x "$venv/bin/pip" ] || {
        printf 'STOP: the training environment is incomplete or unsafe.\n' >&2; exit 1
      }
      . "$venv/bin/activate" || exit 1
      [ "${VIRTUAL_ENV:-}" = "$venv" ] && [ "$(command -v python)" = "$venv/bin/python" ] || {
        printf 'STOP: activation points to another location; use the training-environment recovery instructions.\n' >&2; exit 1
      }
      python - "$venv" <<'PY' || exit 1
    import pathlib, platform, sys
    import pip
    expected = pathlib.Path(sys.argv[1])
    assert pathlib.Path(sys.prefix).resolve() == expected.resolve(), "wrong environment prefix"
    assert pathlib.Path(sys.executable).parent.resolve() == (expected / "bin").resolve(), "wrong interpreter directory"
    assert pathlib.Path(sys.executable).name == "python", "wrong interpreter"
    assert platform.python_version() == "3.11.6", "wrong Python version"
    assert expected.resolve() in pathlib.Path(pip.__file__).resolve().parents, "pip belongs to another environment"
    PY
      module_pip="$(python -m pip --version)" || exit 1
      entry_pip="$("$venv/bin/pip" --version)" || exit 1
      [ "$module_pip" = "$entry_pip" ] || { printf 'STOP: pip entrypoint uses another environment.\n' >&2; exit 1; }
      printf '%s\n' "$module_pip"
      deactivate
    )
    # <!-- /passport-snippet:euler-environment-check -->
    [ ! -e "$venv/.passport-create-in-progress" ] || {
      printf 'STOP: a creation may still be running; wait for it or ask lab IT.\n' >&2; exit 1
    }
    verify_environment
    printf 'python_environment=passport-python\neuler-python-env-ok\n'
    )

    Look forThe output includes python_environment=passport-python, euler-python-env-ok, and pip from the same environment.

    If this differsUse the linked recovery procedure if this is the training environment affected by the earlier recipe. Do not copy a laptop environment or change shell startup files.

    Explanation and help
    What this means
    Learn where these lines belong in a job.
  7. 07

    Put setup inside the Slurm script

    Use Euler after you connect from your computer

    Open this fragment to see how a fresh Slurm job selects its module and project environment before running Python. It illustrates setup only; do not paste it as a workload on the login node.

    Show environment setup inside a batch script (file content)
    Put in fileBash file
    module purge
    module load stack/2024-06 python/3.11.6
    . "$HOME/passport-euler/venvs/passport-python/bin/activate"
    python your_script.py

    Look forYou can identify the three environment lines that must precede the program in a Slurm script.

    Explanation and help
    What this means
    Complete the questions and confirmation.
    Safe recovery
    Do not rely on a login-shell activation to define an unattended job.
  8. 08

    Confirm the safe marker

    On your computer

    Return to the local Passport, enter only euler-python-env-ok, complete the questions, and run Check my work.

    Look forThe local check accepts the exact environment marker and all three safety decisions.

    Explanation and help
    What this means
    Submit once and continue to the first CPU job.
    Safe recovery
    Repeat only the verification step on Euler; do not recreate a working environment.

Practise

One practice question. It does not affect your progress.

01Your login terminal uses the project interpreter. A new batch script only says python run.py. Does the terminal activation carry over?
See an explanation

A batch script starts its own shell. Declare the Euler software module and activate the environment at its final path in that script; do not rely on a previously activated terminal.

Finish the lesson

Questions to submit

Answer these 3 questions, then confirm the work you checked.

Every answer must be correct. Completion appears after the automatic GitHub check passes.

01May you copy the .venv directory from your laptop to Euler?Safety-critical
02Where does this small training environment belong on Euler?Safety-critical
03Where must the module load and environment activation appear for a batch job?Safety-critical
Confirm the result you personally observed

These values are processed only by the local launcher. Private paths are not written to the public submission.

Before submitting
Open my real passport first

You can check sample answers here. Open your local Passport to check your practice files and submit the lesson.

Preview onlyYou can try the questions here. This page does not store or submit your answers.
Need help?Open recovery steps and AI-review guidance

If Blocked#

Keep the first non-secret error. Use module spider python if the dated module cannot be loaded. Do not use sudo, delete an existing environment, copy a laptop environment, or install into base. See the Euler Python environment reference.

Understand Before Accepting AI Output#

Personally verify the Python version, environment path, and the activation lines in the batch script. An agent must not choose a different module, delete an environment, or install packages without your review.