- Newest
- Most votes
- Most comments
It appears there's a mismatch between the kernel you created and the environment that's actually being used when you run your code.
When you created your custom kernel with ipykernel install, it should have linked to your conda environment (p313_torch), but the output shows that your notebook is still using Python 3.10.19 or 3.12.12 from the Anaconda installation path, not Python 3.13 from your custom environment.
This discrepancy could be happening for a few reasons:
-
In SageMaker notebook instances, kernels are separate conda environments. When you restart a notebook instance, SageMaker refreshes the default environments, which might have overwritten your custom kernel configuration.
-
To make custom kernels persist in SageMaker, you need to install them on the notebook instance's Amazon EBS volume and use lifecycle configuration scripts (both on-create and on-start scripts) to ensure they remain available after restarts.
-
The
!pythoncommand in Jupyter cells uses the system Python, not necessarily the kernel's Python. That's why you're seeing Python 3.10.19 when using the shell command. -
The
sys.executableoutput shows you're using Python from the Anaconda installation path, not from your custom environment.
To fix this issue:
- Make sure your custom environment is properly registered with Jupyter
- Consider using lifecycle configuration scripts to ensure your custom kernel persists
- Verify the kernel is actually selected in your notebook (check the top right corner of your notebook)
- Try using
import sys; sys.executableto confirm which Python interpreter is being used - For package installations, use
%pip installor%conda installmagic commands instead of shell commands to ensure packages are installed in the current kernel's environment
Sources
Install External Libraries and Kernels in Amazon SageMaker Studio Classic - Amazon SageMaker AI
External library and kernel installation - Amazon SageMaker AI
Amazon Linux 2 notebook instances - Amazon SageMaker AI
Hello.
I'm not very familiar with Conda virtual environments, but I think it might be pointless to run "python -m ipykernel install" after running "conda deactivate".
So, try switching the order of commands 7 and 6 and executing them.
The "conda deactivate" command is used to exit an active Conda virtual environment.
https://docs.conda.io/projects/conda/en/stable/user-guide/tasks/manage-environments.html#deactivating-an-environment
Relevant content
asked 2 years ago
