VS Code中Jupyter输出与本地终端不一致问题求助
Hey there! I totally get why this is frustrating—you’ve got !nvcc -V working fine in your regular terminal but not in VS Code’s Jupyter notebook, and even reinstalling environments hasn’t helped. Let’s break down the most common reasons and fixes for this:
Your Jupyter notebook is using a different environment than your terminal
Chances are, you’re activating a specific conda/virtualenv environment in your terminal (likeconda activate my_cuda_env) but VS Code’s Jupyter is defaulting to a different kernel. Check the bottom-right corner of VS Code—you’ll see the current Jupyter kernel listed. Click it, then select the same environment you’re using in your terminal. That should sync up the tools available to Jupyter.The
PATHenvironment variable doesn’t include nvcc’s location in Jupyter
Your terminal’sPATHhas the folder wherenvcclives (usually something like/usr/local/cuda/binor the bin folder of your CUDA installation), but Jupyter’s environment doesn’t inherit that path. Here’s a quick test:- In your terminal, run
which nvccto get the full path to the executable (e.g.,/usr/local/cuda/bin/nvcc). - In your Jupyter notebook, run this code to add the path temporarily:
import os # Replace the path below with the folder you got from `which nvcc` (strip off "/nvcc") os.environ['PATH'] += ':/usr/local/cuda/bin' - Now try
!nvcc -Vagain. If this works, you can make the change permanent by adding the path to your system’s environment variables, or by configuring VS Code’s Jupyter settings to include it.
- In your terminal, run
VS Code isn’t inheriting your terminal’s environment variables
On Linux or macOS, if you launch VS Code from the graphical interface (like your dock or applications folder), it might not load your shell’s configuration files (.bashrc,.zshrc, etc.) where you set up CUDA paths. Try closing VS Code, then opening it directly from your terminal with thecode .command—this way, VS Code will inherit all the environment variables from your terminal session, including the ones for CUDA.Jupyter kernel configuration is missing the path
If none of the above works, you can check your Jupyter kernel’s config. Runjupyter kernelspec listin your terminal to find the path to your kernel’s folder. Inside that folder, edit thekernel.jsonfile and add thePATHvariable to theenvsection, like this:{ "argv": ["python", "-m", "ipykernel_launcher", "-f", "{connection_file}"], "display_name": "My CUDA Env", "language": "python", "env": { "PATH": "/usr/local/cuda/bin:$PATH" } }Save the file, restart Jupyter in VS Code, and try again.
Reinstalling environments won’t fix this because the issue isn’t with the environment itself—it’s about how VS Code and Jupyter are loading that environment’s paths. Give these steps a shot, and you should get nvcc working in your notebook!
备注:内容来源于stack exchange,提问作者Yufan Andrew Liu

