在Intel AI DevCloud运行qsub任务时遇keras模块缺失错误求助
I see the issue here—when you submit a job via qsub, it doesn't automatically inherit your active Conda environment settings from the login shell. Let's break down why this happens and how to fix it step by step.
Why This Error Occurs
- The
source activate myenvcommand you ran only activates the environment in your current interactive session. Whenqsubruns your job, it starts a fresh, non-interactive shell that has no clue about the environment you set up. - Even though you installed Keras and TensorFlow in
myenv, the job is actually running against the default system Python environment, which doesn't have those packages installed.
Step 1: Update Your Job Script to Include Environment Setup
The fix is straightforward—you need to explicitly load Conda and activate your myenv environment inside your job script. Here's the revised myjob script:
#PBS -l nodes=1 # Load the Anaconda module (required for Conda access on Intel AI DevCloud) module load anaconda3 # Activate your custom environment source activate myenv # Navigate to your working directory cd $PBS_O_WORKDIR echo Starting calculation python NASNet.py echo End of calculation
A quick note: The module load anaconda3 line is mandatory here because Intel AI DevCloud doesn't make Conda available by default in non-interactive shells. Skipping this step would mean the source activate command won't even be recognized.
Step 2: Verify the Environment (Optional but Useful)
If you want to confirm that the environment is activating correctly, add these lines to your script to print debug info:
... source activate myenv # Print which Python interpreter is being used which python # List installed packages to check for Keras pip list | grep -E "keras|tensorflow" ...
When the job runs, this output will show you exactly which Python environment is active, so you can verify it's myenv and that Keras/TensorFlow are installed there.
Step 3: Alternative: Use conda run for Simpler Execution
Another reliable way to run your script in the right environment is to use the conda run command, which eliminates the need to manually activate the environment. Here's how that looks in your script:
#PBS -l nodes=1 module load anaconda3 cd $PBS_O_WORKDIR echo Starting calculation conda run -n myenv python NASNet.py echo End of calculation
This command directly executes your Python script within the myenv environment, making it less prone to shell-specific issues in non-interactive sessions.
内容的提问来源于stack exchange,提问作者virtualdvid

