Skip to content
BoKSA

Setup Python Virtual Environment

Python Setup Manual for Students

Windows & macOS · Multiple Python versions · Virtual environments · Jupyter in VS Code

By the end of this manual you will be able to:

  1. Install one or more Python versions and switch between them
  2. Create a separate virtual environment for each project
  3. Write and run Jupyter notebooks inside Visual Studio Code

We use uv, a free, fast tool that installs Python versions and manages virtual environments with the same commands on Windows and macOS. You do not need to install Python from python.org or Anaconda first.

⏱️ Time needed: about 20–30 minutes. You need an internet connection and permission to install software on your computer.


Table of Contents

  1. Key ideas in 2 minutes
  2. Open a terminal
  3. Install uv
  4. Install and switch Python versions
  5. Create a project and a virtual environment
  6. Install packages
  7. Install and set up VS Code
  8. Run a Jupyter notebook in VS Code
  9. Daily workflow cheat sheet
  10. Troubleshooting
  11. Final checklist

1. Key ideas in 2 minutes

Term What it means
Python version Python exists in versions such as 3.11, 3.12 and 3.13. Some courses or libraries need a specific one, so it is useful to have several installed.
Virtual environment (venv) A private folder (usually called .venv) inside your project that holds one Python version plus the packages for that project only. Projects don't interfere with each other.
Package Extra code you install, such as numpy, pandas or matplotlib.
uv The tool that downloads Python versions, creates virtual environments and installs packages.
Jupyter notebook A .ipynb file with cells of code, output and text. VS Code can open and run them.
Kernel The Python that actually runs your notebook's code. You will point it at your project's .venv.

Golden rule: one project folder → one virtual environment → one set of packages.


2. Open a terminal

All commands in this manual are typed in a terminal.

Windows Press the Windows key, type PowerShell, and open Windows PowerShell (or Terminal). Do not choose "Run as administrator"; it isn't needed.

macOS Press Cmd + Space, type Terminal, and press Enter.

💡 In this manual, lines in grey boxes are commands. Type (or paste) them and press Enter. Don't type the # comments.


3. Install uv

Windows (PowerShell)

1
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

Alternative if you use winget: winget install --id=astral-sh.uv -e

macOS (Terminal)

1
curl -LsSf https://astral.sh/uv/install.sh | sh

Alternative if you use Homebrew: brew install uv

Check that it worked

Close the terminal and open a new one (so it picks up the new program), then run:

1
uv --version

You should see something like uv 0.x.y. If you get "command not found" / "not recognized", see Troubleshooting.


4. Install and switch Python versions

Install one or more versions

1
uv python install 3.12 3.13

This downloads both versions. They live in uv's own folder and won't conflict with any Python that is already on your computer.

See what is available and installed

1
uv python list

Installed versions show a file path; versions that can be downloaded are marked as such.

Switching versions — the idea

With uv you don't "switch" your whole computer from one Python to another. Instead, each project chooses its version. There are three ways to do this:

Situation Command
Run a quick one-off with a specific version uv run --python 3.13 python --version
Make a project always use a version uv python pin 3.12 (run inside the project folder; creates a .python-version file)
Create a virtual environment with a version uv venv --python 3.12 (see section 5)

Try it:

1
2
uv run --python 3.12 python --version
uv run --python 3.13 python --version

You should see two different version numbers. 🎉 You now have multiple Python versions.

Removing a version (optional)

1
uv python uninstall 3.13

5. Create a project and a virtual environment

5.1 Make a project folder

Windows (PowerShell)

1
2
mkdir $HOME\Documents\python-course
cd $HOME\Documents\python-course

macOS

1
2
mkdir -p ~/Documents/python-course
cd ~/Documents/python-course

⚠️ Avoid putting projects in OneDrive/iCloud-synced folders if you can; syncing thousands of small files in .venv can cause errors and slowness.

5.2 Create the virtual environment

1
2
uv python pin 3.12
uv venv

uv venv creates a .venv folder using the pinned version. You could also skip the pin and write uv venv --python 3.12.

5.3 Activate the environment

Activating means "from now on, python in this terminal refers to this project's .venv".

Windows (PowerShell)

1
.venv\Scripts\activate

macOS

1
source .venv/bin/activate

Your prompt now starts with (python-course) or (.venv). Check:

1
python --version

It should show 3.12.x.

To leave the environment:

1
deactivate

🔁 You must activate the environment each time you open a new terminal for this project. (VS Code can do this automatically; see section 7.)

5.4 Changing the Python version of a project

A virtual environment is tied to one Python version. To change it, simply rebuild it:

1
2
3
deactivate               # if it's active
uv python pin 3.13
uv venv                  # answer yes if asked to replace the existing .venv

Then reinstall your packages (easy if you keep a requirements.txt, see next section).


6. Install packages

With the environment activated, install packages with uv pip:

1
uv pip install ipykernel numpy pandas matplotlib

✅ ipykernel is required for Jupyter notebooks in VS Code. Always install it in every environment you want to use for notebooks.

Useful commands:

Task Command
List installed packages uv pip list
Save your package list uv pip freeze > requirements.txt
Install from a list (e.g. from your teacher) uv pip install -r requirements.txt
Remove a package uv pip uninstall pandas

💡 Tip for later: uv also has a "project" workflow (uv init, uv add numpy, uv sync) that records packages automatically in a pyproject.toml file. It's great once you're comfortable, but the commands above are all you need for this course.


7. Install and set up VS Code

7.1 Install VS Code

Download it from https://code.visualstudio.com and run the installer.

  • Windows: in the installer, tick "Add to PATH" and "Add 'Open with Code' action".
  • macOS: drag Visual Studio Code into your Applications folder. Then open VS Code, press Cmd + Shift + P, type Shell Command: Install 'code' command in PATH and press Enter.

7.2 Install the extensions

  1. Open VS Code.
  2. Click the Extensions icon in the left sidebar (four squares), or press Ctrl + Shift + X (Windows) / Cmd + Shift + X (Mac).
  3. Search for and install:
  4. Python (publisher: Microsoft)
  5. Jupyter (publisher: Microsoft)

The Python extension also installs Pylance and a Python Environments helper automatically.

7.3 Open your project folder

In the terminal, inside your project folder:

1
code .

Or use File → Open Folder… and choose python-course. If VS Code asks whether you trust the authors of the folder, click Yes.

⚠️ Always open the project folder, not a single file. VS Code finds .venv by looking in the open folder.

7.4 Select the interpreter

  1. Press Ctrl + Shift + P (Windows) / Cmd + Shift + P (Mac).
  2. Type Python: Select Interpreter and press Enter.
  3. Choose the entry that shows .venv (e.g. Python 3.12.x ('.venv': venv)).

Now open a new terminal in VS Code (Terminal → New Terminal). It should activate .venv automatically; you'll see (.venv) or the project name in the prompt.

7.5 Quick test with a script

Create a file hello.py:

1
2
3
import sys
print("Hello from Python", sys.version)
print("Running from:", sys.executable)

Click the ▶️ Run button in the top-right. The output should show your chosen version and a path that contains .venv.


8. Run a Jupyter notebook in VS Code

  1. Press Ctrl/Cmd + Shift + P → Create: New Jupyter Notebook (or create a file ending in .ipynb, e.g. week1.ipynb).
  2. Click Select Kernel in the top-right of the notebook.
  3. Choose Python Environments… → select the .venv of your project.
  4. In the first cell type:
1
2
import sys
print(sys.executable)
  1. Press Shift + Enter to run the cell.

The output path should contain .venv. Try a second cell:

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 200)
plt.plot(x, np.sin(x))
plt.title("It works!")
plt.show()

You should see a sine wave. 🎉

If VS Code shows a popup saying "Running cells requires the ipykernel package", click Install, or run uv pip install ipykernel in the activated environment and then choose the kernel again.

Useful notebook shortcuts

Action Shortcut
Run cell and move to next Shift + Enter
Run cell and stay Ctrl + Enter (Win) / Cmd + Enter (Mac)
New cell below B (when not typing in a cell; press Esc first)
Delete cell D D (press twice, when not typing)
Change to Markdown cell M
Change to code cell Y
Restart kernel Click Restart in the notebook toolbar

9. Daily workflow cheat sheet

Starting a new project

1
2
3
4
5
6
7
8
mkdir my-project
cd my-project
uv python pin 3.12
uv venv
# activate (Windows):  .venv\Scripts\activate
# activate (macOS):    source .venv/bin/activate
uv pip install ipykernel numpy pandas matplotlib
code .

Then in VS Code: Select Interpreter → .venv and in notebooks Select Kernel → .venv.

Continuing an existing project

1
2
cd my-project
code .

VS Code remembers the interpreter and kernel.

Getting a project from a teacher or classmate

1
2
3
4
cd their-project
uv venv --python 3.12
# activate
uv pip install -r requirements.txt

🚫 Never copy or share the .venv folder itself (e.g. by email or USB). It's large and only works on the computer that created it. Share requirements.txt instead. If you use Git, add .venv/ to your .gitignore.


10. Troubleshooting

uv is "not recognized" / "command not found" Close all terminals (and VS Code) and open a new one. The installer changed your PATH, and only new windows see it. On macOS you can also run source ~/.zshrc. If that doesn't help, rerun the install command from section 3.

Windows: "running scripts is disabled on this system" when activating PowerShell blocks scripts by default. Run this once (not as administrator):

1
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Answer Y, then try activating again.

Windows: typing python opens the Microsoft Store That's a Windows shortcut, not real Python. Either activate your .venv first, or turn it off: Settings → Apps → Advanced app settings → App execution aliases → switch off python.exe and python3.exe.

macOS: a popup asks to install "command line developer tools" Click Install. This is Apple's standard developer package; it's safe and sometimes needed.

VS Code doesn't show my .venv in the list - Make sure you opened the project folder (File → Open Folder). - Choose Enter interpreter path… and browse to: - Windows: .venv\Scripts\python.exe - macOS: .venv/bin/python - Reload VS Code: Ctrl/Cmd + Shift + P → Developer: Reload Window.

ModuleNotFoundError: No module named 'numpy' in a notebook The notebook is using a different kernel than the environment where you installed the package. Check the kernel name in the top-right; select your .venv. Then run import sys; print(sys.executable) to confirm.

I installed a package but my notebook still can't find it Restart the kernel (toolbar → Restart) after installing packages.

Something is completely broken A virtual environment is disposable. Delete the .venv folder and recreate it:

1
2
3
uv venv
# activate, then:
uv pip install -r requirements.txt

11. Final checklist

Tick each item when it works:

  • uv --version prints a version number
  • uv python list shows at least two installed Python versions
  • uv run --python 3.12 python --version and ... 3.13 ... print different versions
  • I created a project folder with a .venv inside it
  • After activating, python --version shows the expected version
  • uv pip list shows ipykernel, numpy, pandas and matplotlib
  • VS Code has the Python and Jupyter extensions installed
  • VS Code's selected interpreter is my project's .venv
  • A notebook runs a cell and sys.executable points into .venv
  • The sine-wave plot appears in my notebook

All ticked? You're ready for the course. 🚀


Further reading: uv documentation: https://docs.astral.sh/uv/ · VS Code Python docs: https://code.visualstudio.com/docs/python/python-tutorial · VS Code Jupyter docs: https://code.visualstudio.com/docs/datascience/jupyter-notebooks