Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 1

Setting Up Your Python Environment

Recommended resources

Welcome to the first lesson of our course! We're kicking things off by building the essential foundation for our project: a professional Python development environment. Your goal is to build a sophisticated AI application, and like any complex software project, it requires a clean, organized, and reproducible setup. This lesson will focus on achieving that by teaching you how to use virtual environments and Python's package manager, pip.

You have a strong background in web development with the MERN stack, so you're already familiar with the concepts of dependency management through tools like npm and package.json. We'll draw parallels to that experience. In the Python ecosystem, virtual environments serve a similar purpose to the node_modules folder, providing isolation, while pip and requirements.txt files act like npm and package.json for managing and listing dependencies.

By the end of this lesson, you will have a fully functional, isolated Python environment ready for the code we'll start writing in our next session.

What is a Virtual Environment?

Before we start typing commands, let's understand the "why". Imagine you're working on two different projects. Project A needs version 1.0 of a library, but Project B requires a newer, incompatible version 2.0. If you install these libraries globally on your computer, one will overwrite the other, breaking one of your projects. This is often called "dependency hell."

A Python virtual environment is a self-contained directory that houses a specific Python interpreter and all the libraries and packages for a single project. It creates an isolated "bubble" around your project, so the dependencies for one project don't interfere with another.

This diagram illustrates how two separate virtual environments, `my-ml-env1` and `my-ml-env2`, can maintain different versions of Python (`3.6` vs. `3.8.1`) and libraries (`scikit-learn` `0.19.3` vs. `0.24`) without conflict.

At a high level, each virtual environment contains a few key components that make this isolation possible.

A virtual environment bundles its own Python interpreter, its own `pip` for installing packages, activation scripts to switch into the environment, and a `site-packages` directory to store the project's unique set of libraries.

To get a deeper understanding of the problems virtual environments solve, let's turn to a great article from Real Python.

Python Virtual Environments: A Primer

This section explains the crucial reasons for using virtual environments. It will solidify your understanding of why this initial setup step is a non-negotiable best practice in modern Python development.

Please read the section titled "Why Do You Need Virtual Environments?". Pay close attention to the subsections on avoiding system pollution and sidestepping dependency conflicts. These are the most immediate problems you'll prevent by adopting this workflow.


Hands-On: Your First Virtual Environment

Now, let's put theory into practice. We'll create, activate, and use a virtual environment for our project. We will use venv, the tool for creating virtual environments that comes built-in with Python. It's the standard, officially recommended approach and requires no extra installation.

For a great visual walkthrough of the process we're about to undertake, I recommend watching the following video from Tech With Tim.

Python Virtual Environments - Full Tutorial for Beginners

This video provides a clear, step-by-step demonstration of creating, activating, and using a virtual environment with venv.

Watch the segments on creating the environment, activating and deactivating it, and installing packages with pip. Notice the different commands for Windows versus Mac/Linux.

Let's follow those steps.

1. Create the Virtual Environment

First, open your terminal or command prompt. Navigate to the folder where you plan to store your project files for our grasp.study clone. Once there, run the following command:




# On Windows
py -m venv .venv




# On macOS/Linux
python3 -m venv .venv

Let's break this command down:

  • py -m or python3 -m: This runs a Python module as a script. Your CompTIA Linux+ certification means you're likely comfortable with the python3 command on Linux. On Windows, py is the recommended launcher.
  • venv: This is the name of the module we're running.
  • .venv: This is the name we're giving our virtual environment directory. It's a modern convention to name it .venv and place it directly inside your project folder. The leading dot often hides the folder by default in file explorers, which is nice since you won't typically edit its contents directly.

After running this command, you'll see a new folder named .venv in your project directory.

2. Activate the Virtual Environment

Creating the environment isn't enough; you have to "activate" it for your current terminal session. Activation tells your shell to use the Python interpreter and tools inside the .venv folder instead of the global ones.




# On Windows (in Command Prompt or PowerShell)
.\.venv\Scripts\activate




# On macOS/Linux (in bash, zsh, etc.)
source .venv/bin/activate

After you run the activation command, you should see (.venv) appear at the beginning of your terminal prompt. This is your visual confirmation that the virtual environment is active. Any Python-related commands you run now will be scoped to this environment.

3. Install Packages with pip

With the environment active, we can now install packages. pip is Python's package installer. Let's install the requests library, which is a popular tool for making HTTP requests (something we'll do a lot of later).

python -m pip install requests

Using python -m pip is a robust way to ensure you're using the pip associated with the active environment's Python interpreter. The requests library and its own dependencies are now installed inside the .venv/Lib/site-packages folder, completely isolated from your other projects.

You can see a list of all packages installed in the current environment by running:

pip list

You will see requests and a few other packages it depends on, but none of the packages you might have installed globally.

4. Deactivate the Virtual Environment

When you're finished working on the project, you can deactivate the environment:

deactivate

The (.venv) prefix will disappear from your prompt, and your terminal session will revert to using your system's global Python environment. The .venv folder and its installed packages remain, ready for you to activate again next time.

For a more condensed, command-focused guide on this workflow, the "Bite code!" article is an excellent reference.

Back to basics with pip and venv

This article offers a very direct, "no-nonsense" guide to the essential commands. It reinforces the steps we just took and provides clear warnings about common pitfalls.

Skim through the sections on creating an environment and using it. Then, read the section on installing with pip and finally, review the excellent summary of what not to do, which highlights best practices.


Managing Dependencies for Reproducibility

Just like a package.json file records your project's dependencies in the JavaScript world, you need a way to record your Python dependencies. This is crucial for collaboration and for deploying your application. The standard convention is a file named requirements.txt.

You can automatically generate this file from your active virtual environment using pip freeze:

pip freeze > requirements.txt

This command takes the output of pip freeze (which is a list of all installed packages and their exact versions) and writes it to a file named requirements.txt. You should commit this file to your Git repository.

Now, if another developer (or you, on a different machine) wants to set up the project, they can create a new virtual environment, activate it, and run one command to install all the necessary packages:

pip install -r requirements.txt

This makes your project's environment perfectly reproducible.

The video from Dave Gray provides a helpful demonstration of this entire process, including the creation of a requirements.txt file and also a .gitignore file, which is used to tell Git to ignore the .venv folder.

Python Virtual Environment and pip for Beginners

This video shows how to create and use the requirements.txt and .gitignore files, which are essential for managing a professional Python project.

Please watch the sections on creating a requirements.txt file and setting up a .gitignore file. Since you're experienced with Git, the purpose of .gitignore will be immediately familiar.


Key Takeaways

This lesson covered the critical first step in any Python project: setting up a clean, isolated development environment.

  • Isolation is Key: Virtual environments prevent dependency conflicts between projects by creating isolated spaces for packages.
  • venv is Standard: We use the built-in venv module to create our environments with the command python -m venv .venv.
  • Activate and Deactivate: Environments must be activated (source .venv/bin/activate or .\.venv\Scripts\activate) before use and can be deactivated (deactivate) when you're done.
  • pip is for Packages: Inside an active environment, pip install <package_name> installs tools and libraries locally to that environment.
  • Reproducibility with requirements.txt: The pip freeze > requirements.txt command saves your project's dependencies, allowing anyone to perfectly replicate your environment with pip install -r requirements.txt.

You now have a professional environment set up and are ready to start writing Python code. In the next lesson, we will begin our journey into the Python language itself by exploring its core data types: strings, lists, tuples, and dictionaries. These are the fundamental building blocks you'll use to construct our application's logic.