如何在VSCode的Python解释器中运行文件并实现类PyScripter的变量自动加载?
Hey there! Let's break down your VSCode Python questions with practical, hands-on solutions:
You’ve got several straightforward options to execute your Python files:
- Quick Run Button: Look for the triangular "Run" icon in the top-right corner of the editor window. Clicking it will run your current file in the integrated terminal using your selected Python interpreter.
- Right-Click Menu: Right-click anywhere in your open Python file, then select "Run Python File in Terminal" from the dropdown. This directly executes the file in VSCode’s built-in terminal.
- Keyboard Shortcuts: Use
Ctrl+F5(Windows/Linux) orCmd+F5(Mac) to run the file without debugging, orF5to start a debug session if you need to step through code line by line. - Manual Terminal Command: Open the integrated terminal (press
Ctrl+), navigate to your file’s directory if needed, then runpython3 variables.py(orpython variables.pydepending on your system’s Python alias).
First, let’s clear up why import variables doesn’t let you access a directly: when you import a module, its variables live in the module’s namespace—so you’d need to call variables.a instead of just a. Your exec(open('variables.py').read()) workaround works because it runs the file’s code in your current interpreter namespace, but it lacks auto-reload for edits.
Here’s a better, automated solution using IPython’s built-in tools:
Step 1: Use IPython's %run Magic Command
Instead of exec, use IPython’s %run command—it loads the file’s variables directly into your current namespace (just like your exec method, but with more flexibility):
%run variables.py
After running this, you can access a, b, c directly in the terminal.
Step 2: Enable Auto-Reload for File Changes
To avoid re-running commands every time you edit variables.py, use IPython’s autoreload extension:
- Switch to an IPython terminal in VSCode: open the integrated terminal and type
ipythonto launch it. - Load the autoreload extension:
%load_ext autoreload - Set it to auto-reload all modules when they change:
%autoreload 2%autoreload 2means any changes to yourvariables.pyfile will be automatically picked up the next time you access its variables in the terminal. No need to re-run%runorexec!
Step 3: Make Auto-Reload Persistent (Optional)
If you don’t want to type the autoreload commands every time you open IPython, set up a startup script:
- Create a file at
~/.ipython/profile_default/startup/00-autoreload.py(create the directories if they don’t exist). - Add these lines to the file:
%load_ext autoreload %autoreload 2
Now every time you launch IPython in VSCode, autoreload will be enabled automatically.
Alternative: VSCode Debug Session
If you prefer using VSCode’s debugger, set up a launch.json configuration to run your file and keep variables accessible:
- Go to the "Run and Debug" tab (
Ctrl+Shift+D). - Click "create a launch.json file" and select "Python" > "Python File".
- Modify the configuration to include
"stopOnEntry": true(so the debugger pauses at the start of the file) or run it normally. When the script finishes, you can still access variables in the debug console—though you’ll need to restart the session if you edit the file.
内容的提问来源于stack exchange,提问作者CODEHUB

