首页 > 解决方案 > SLURM 工作。NameError:名称'python3'未定义

问题描述

一段时间以来,我一直在试图弄清楚如何在 slurm 上运行工作。

用户目录中的 Python 版本:

$ python -V
Python 2.7.5
$ python3 -V
Python 3.6.8

test.py 包括:

import pandas as pd
import numpy as np
true = pd.read_csv("testfile.csv")
print('Just Testing. End for now.')

传递给 SLURM 的 gpu.job:

#!/usr/bin/python3

#SBATCH --job-name=testjob       # Job name
#SBATCH --output=job.%j.out      # Name of output file (%j expands to jobId)
#SBATCH --cpus-per-task=4        # Schedule one core
#SBATCH --gres=gpu               # Schedule a GPU
#SBATCH --time=71:59:59          # Run time (hh:mm:ss) - run for one hour max
#SBATCH --partition=red          # Run on either the Red or Brown queue
#SBATCH --mail-type=END          # Send an email when the job finishes
#SBATCH --export=ALL             # All of the users environment will be loaded from callers environment


python3 /home/username/test/test.py

运行后sbatch gpu.job,我得到:

回溯(最近一次通话最后):

文件“/var/spool/slurm/d/job402350/slurm_script”,第 13 行,在

python3 /home/用户名/test/test.py

NameError: name 'python3' is not defined ~

这些变化也没有帮助,并给出了同样的错误:

python3 test.py
/usr/bin/python3 test.py
/usr/bin/python3 /home/username/test/test.py

建议将不胜感激。

标签: python-3.xcronhpcslurmnameerror

解决方案


您的提交脚本是一个 shell 脚本,而不是 Python 脚本。所以你的提交脚本的第一行应该是

#!/usr/bin/env bash

而不是

#!/usr/bin/python3

从技术上讲,您可以提交一个 Python 脚本的作业脚本,但是#SBATCH指令直接在 Python 脚本中得到,这就是您提交的脚本:

#!/usr/bin/python3

#SBATCH --job-name=testjob       # Job name
#SBATCH --output=job.%j.out      # Name of output file (%j expands to jobId)
#SBATCH --cpus-per-task=4        # Schedule one core
#SBATCH --gres=gpu               # Schedule a GPU
#SBATCH --time=71:59:59          # Run time (hh:mm:ss) - run for one hour max
#SBATCH --partition=red          # Run on either the Red or Brown queue
#SBATCH --mail-type=END          # Send an email when the job finishes
#SBATCH --export=ALL             # All of the users environment will be loaded from callers environment


import pandas as pd
import numpy as np
true = pd.read_csv("testfile.csv")
print('Just Testing. End for now.')

然后你可以sbatch直接使用这个 Python 脚本。但大多数情况下,最好使用 Bash 脚本来设置环境、更改目录、来回复制文件等,这在 Bash 中比在 Python 中更容易。


推荐阅读