Your Python installation
Remember when we installed Python? That installation lives in a specific folder on your computer:- Windows:
C:\Users\[YourName]\AppData\Local\Programs\Python\Python313\ - macOS:
/Library/Frameworks/Python.framework/Versions/3.13/ - Linux:
/usr/bin/python3
The problem with sharing
Imagine you’re working on two projects:- Project A needs version 1.0 of a tool
- Project B needs version 2.0 of the same tool
What are virtual environments?
A virtual environment is like a private copy of Python for each project. Think of it as:- A separate folder for each project
- Its own Python installation
- Its own place to install packages
- Complete isolation from other projects
Virtual environments are so important that I create a new one for EVERY project. It’s a Python best practice that will save you from many headaches.
Understanding packages
Before we continue, let’s understand what packages are: Packages are pre-written code that others have created for us to use. Instead of writing everything from scratch, we can import these packages. For example:requests- for downloading web pagespandas- for working with dataopenai- for using AI models
Create your first virtual environment
Let’s create one for ourpython-for-ai project. You have two methods:
Method 1: VS Code Command Palette (easier)
- Open your project in VS Code
- Press
Ctrl/Cmd + Shift + P - Type “Python: Create Environment”
- Select “Venv”
- Select your Python installation
- VS Code creates and activates everything for you!
Method 2: Terminal command
- Open the terminal in VS Code (View > Terminal)
- Make sure you’re in your project folder
- Run this command:
- Windows
- macOS/Linux
python -m venv- Run Python’s virtual environment module.venv- The name of the folder to create (convention is to use “.venv” with a dot)
What just happened?
Look at your project folder - you now have a new.venv folder:
The dot in
.venv makes it a hidden folder on Mac/Linux. This keeps your project clean since you don’t need to see this folder often..venv folder contains:
- A copy of Python
- A place to install packages
- Scripts to activate/deactivate
Activate your virtual environment
VS Code makes activation super easy:- Press
Ctrl/Cmd + Shift + Pto open Command Palette - Type “Python: Select Interpreter”
- Choose the one that says
.venv(it will show the full path like./.venv/bin/python) - That’s it! VS Code automatically activates it for you
(.venv) in your terminal:
This is my recommended approach. VS Code remembers your choice and automatically activates the environment every time you open the project!
Common issues
Command not found
Command not found
If Other systems: It should come with Python by default
python -m venv doesn’t work, you might need to install it:Ubuntu/Debian:Permission denied
Permission denied
On macOS/Linux, you might need to make the activate script executable:
VS Code not recognizing venv
VS Code not recognizing venv
- Reload VS Code window:
Ctrl/Cmd + Shift + P> “Developer: Reload Window” - Manually select interpreter again
- Make sure the Python extension is installed
What about Anaconda?
You might have heard of Anaconda - it’s another tool that manages Python environments and packages. It’s popular in the data science world because it comes pre-loaded with many data science packages. However, I don’t recommend using Anaconda unless you have a specific reason to.Stick with Python’s built-in virtual environments unless your company or a specific project requires Anaconda. You’ll have a lighter, faster, and simpler setup.
What’s next?
Your virtual environment is ready! Now let’s learn about Python packages and how to install them.Python packages
Understanding pip and package management