VS Code中Conda环境下Python sleep失效及执行命令修改方法
I’ve dealt with this exact frustration before! When VS Code uses conda run to execute Python scripts in a Conda environment, it buffers output by default—hence why both print statements pop up together after the 2.4-second delay instead of the first one showing up immediately. Switching to directly calling your Conda environment’s Python interpreter fixes this real-time output issue. Here’s how to update VS Code’s default run command:
Option 1: Tweak the "Run in Terminal" Settings
- Open VS Code’s Settings: Hit
Ctrl+,(Windows/Linux) orCmd+,(Mac), or navigate via the menu bar:Code > Preferences > Settings(Mac) /File > Preferences > Settings(Windows/Linux). - In the search bar, type
python run in terminal argsto find thePython > Run In Terminal: Argssetting. - Click "Edit in settings.json" to modify the config. Replace any existing entries with:
This tells VS Code to run the file directly with your selected environment’s Python interpreter instead of wrapping it in"python.runInTerminalArgs": [ "${file}" ]conda run.
Option 2: Create a Custom Launch Configuration (More Control)
If you want explicit control over how your scripts run, this is the way to go:
- Open your Python file, then click the dropdown arrow next to the "Run" button in the top-right corner. Select Add Configuration....
- Choose Python File from the options—this generates a
.vscode/launch.jsonfile in your workspace. - Update the configuration entry to use your Conda environment’s Python directly. Here’s what the final config might look like:
The{ "version": "0.2.0", "configurations": [ { "name": "Python: Current File (No Conda Run)", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "command": "${config:python.pythonPath}", "args": ["${file}"] } ] }${config:python.pythonPath}variable automatically uses the Python interpreter of the Conda environment you’ve selected in VS Code. Now when you run your script, it’ll usepython "path/test_print.py"instead of the defaultconda runcommand.
Why This Fixes the Output Issue
The conda run command buffers output to manage environment activation smoothly, but that’s exactly what’s causing the delayed print behavior. By calling the Python interpreter directly, you bypass this buffering, so each print statement outputs in real time—just like it does in your Mac’s native Python environment.
内容的提问来源于stack exchange,提问作者harinsamaranayake

