Resources

https://code.visualstudio.com/docs/python/python-tutorial

Setup for VS Code Python programming

Install Python for Windows

Reboot windows (so the correct environmental variables are picked up for python by VS code – we found was needed after adding / updating Python to a new version).

In VS Code, add the “Python extension for Visual Studio Code”, just named “Python”.

Ensure VS Code knows where your Python install is

Open the Command Palette (Ctrl+Shift+P) and enter “Python: Select Interpreter”
Select the python you’ve installed (e.g. “C:\Program Files\Python312\”)

VSCode Settings

Use C|TRL+SHIFT+P and then enter “Open User Settings (JSON)” to open the raw json file. Check it has the following:

	"[python]": {
		"editor.formatOnType": true,
		"editor.trimAutoWhitespace": false,
		"editor.insertSpaces": true,
		"editor.tabSize": 4  
	},

“editor.trimAutoWhitespace”: false – stops VSCode auto removing indent of blank lines

Creating a new Python project

VS Code menu > File > Open Folder

Open a folder that will be your workspace (you can just open an empty folder you’ve created).

Turn off Dropbox if your folder is within Dropbox (causes file access issues for us at times)

Now create a virtual environment for this project (recommended approach when using Python, so individual projects and their dependencies are kept isolated from each other).
Open the Command Palette (Ctrl+Shift+P) and enter “Python: Create Environment”
Select “Venv” (default, or whatever you want to use)
Select the interpreter to use (the version of Python you’ve installed on your machine).
Wait for the progress to complete. Check for any errors.

Create a new file

Menu > File > New file… > MyFileName.py

Running the Project

Use the play button at the top right of the code editor window “Run Python File”.

Debugging the project

Press F5

Or use the dropdown option on the play button at the top right of the code editor window and select “Debug Python File”.

Install python packages you want to use in your code

Open the Command Palette (Ctrl+Shift+P) and enter “Terminal: Create New Terminal” to create a powershell terminal window.

Say you want to install the “requests” python package:

py -m pip install requests

Troubleshooting

If there are errors creating the virtual environment

This page may help

Right click the Output window and select “Clear output” before trying again, so you are not looking at old errors.

If you make changes to try and fix, you can delete the .venv directory and try again (seems to work better than telling VS code to do it when running “Python: Create Environment” again in our experience).

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *