Install Visual Code and Create Virtual Environmnet

- Install Visual Code: https://code.visualstudio.com/
- Install the Python Extension(in case this was not installed when installing Visual Code)
- Open VS Code.
- Click the Extensions icon on the left sidebar (it looks like four squares).
- Search for "Python" and install the one provided by Microsoft.
- Open Your Project Folder
- Create a folder on your computer (e.g., Documents/my_project).
- In VS Code, go to File > Open Folder and select that folder.
- Open a new Terminal by going to Terminal > New Terminal at the top menu.
- Create and Activate the Virtual Environment
- In the terminal window at the bottom of your screen, type these commands: python -m venv .venv
- Wait a moment; you'll see a folder named .venv appear in your file explorer.
- Activate the environment:
- .venv\Scripts\activate
- Note: You should now see (.venv) in green text at the start of your command line.
- Install the libraries in the Virtual Environment
Note:
Windows sets the default execution policy to Restricted
to prevent malicious scripts from running in the background. Since the
.venv\Scripts\Activate.ps1
file is technically a script, Windows blocks it until you explicitly tell it that you trust the scripts in your current process.
To make this change permanent so you never have to deal with that error again, you need to change the Execution Policy for your Current User rather than just the "Process."
Here is how to do it safely:
1. Open PowerShell as Administrator
- Click the Windows Start button.
- Type PowerShell.
- Right-click on Windows PowerShell and select Run as Administrator. (This is important because changing system-wide policies requires extra permissions).
2. Run the Permanent Command
In the blue window that opens, type (or paste) the following command and press Enter:
PowerShell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
3. Confirm the Change
- PowerShell will ask for confirmation.
- Type Y (for Yes) and press Enter.
Why this is the "Sweet Spot" for Security
By setting it to
RemoteSigned
for the
CurrentUser
:
- Local scripts: Any script you create on your own computer (like the
.venvactivation script) will run freely. - Internet scripts: Any script downloaded from the internet must be "signed" by a trusted publisher to run, which keeps you protected from most malicious files.
Pro-Tip: Let VS Code Auto-Activate
Now that the permissions are fixed, VS Code can actually do the work for you.
- In VS Code, press
Ctrl + Shift + P. - Type "Python: Select Interpreter".
- Select the one that points to your virtual environment (it should look like
.\.venv\Scripts\python.exe). - Now,
whenever you open a new terminal in this project folder, VS Code will automatically run the activation script for you! You’ll see
(.venv)pop up without you typing a single word.