Mac系统更新Anaconda后,如何配置使用Visual Studio Code?
Hey there, let's get your VS Code linked up with your updated Anaconda installation smoothly. Here's what you need to do:
Step 1: Confirm Your Conda Path
First, let's make sure we know exactly where Anaconda's conda tool lives. Open your Mac terminal and run:
which conda
You'll get a path like /Users/[your-username]/anaconda3/bin/conda—jot this down, we'll need it in a later step.
Step 2: Install the Python Extension in VS Code
VS Code relies on its official Python extension to integrate with Anaconda environments. Here's how to get it:
- Open VS Code, click the Extensions icon (the square icon on the left sidebar)
- Search for Python (published by Microsoft)
- Click "Install" to add it to your VS Code setup.
Step 3: Link Anaconda's Python Interpreter to VS Code
This is the core step to connect the two tools:
- Press
Cmd + Shift + Pto open the Command Palette in VS Code - Type
Python: Select Interpreterand hit Enter - In the dropdown list, look for an option labeled
base (anaconda3)—select that if it appears. - If you don't see it, choose
Enter interpreter path, then paste the path from Step 1 but replacecondawithpython(e.g.,/Users/[your-username]/anaconda3/bin/python) and confirm.
Step 4: Verify the Connection
Let's confirm everything is working as expected:
- Create a new Python file (e.g.,
test_connection.py) in VS Code - Paste this code snippet:
import sys import conda print("Python executable path:", sys.executable) print("Conda version:", conda.__version__)
- Run the file (click the play button in the top right, or use
Cmd + F5) - Check the terminal output: it should show your Anaconda Python path and conda version 1.6.9, which means the integration is successful.
Bonus: Work with Custom Conda Environments
If you want to use a specific conda environment instead of the default base one:
- In your terminal, create the environment first:
conda create -n my_custom_env python=3.9
- Activate it once to register it properly:
conda activate my_custom_env
- Go back to VS Code's Command Palette, run
Python: Select Interpreteragain—you should now seemy_custom_envin the dropdown list. Select it, and you're ready to work in that environment.
If VS Code doesn't pick up the environment right away, try restarting the app or double-checking that your Anaconda path is correct.
内容的提问来源于stack exchange,提问作者KansaiRobot

