The first step in any robust software project is to set up a clean and isolated development environment. This chapter will guide you through creating a new project directory and establishing a Python virtual environment to manage dependencies effectively.
Purpose of this Chapter
By the end of this chapter, you will have:
- A dedicated project folder for your chat application.
- A Python virtual environment configured to keep project dependencies separate from your system-wide Python installation.
- Installed FastAPI and Uvicorn, the core components of our application.
Concepts Explained: Virtual Environments
A virtual environment is a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.
Why use a virtual environment?
- Isolation: Prevents conflicts between dependencies of different projects. For example, if Project A needs
fastapi==0.100.0and Project B needsfastapi==0.110.0, virtual environments allow both to coexist on your machine without issues. - Reproducibility: Makes it easy to share your project and ensure others can install the exact same dependencies you used, preventing “it works on my machine” problems.
- Cleanliness: Keeps your global Python installation clean from project-specific packages.
We’ll use pipenv for managing our virtual environment and dependencies. pipenv combines pip and virtualenv into a single tool, simplifying dependency management.
Step-by-Step Tasks
1. Create the Project Directory
First, create a new directory for your chat application. You can name it whatever you like, for example, realtime-chat-app.
On your command line, navigate to where you want to create your project and run:
mkdir realtime-chat-app
cd realtime-chat-app
2. Install Pipenv (if you don’t have it)
If you don’t already have pipenv installed globally, you can install it using pip:
pip install pipenv
If you encounter permission errors, you might need to use sudo or install it within a user-specific directory (check Python’s documentation for user-site packages).
3. Create a Virtual Environment with Pipenv
Inside your realtime-chat-app directory, initialize a new virtual environment. pipenv will automatically detect your Python version and create a virtual environment for it.
pipenv --python 3.13 install
This command tells pipenv to create a virtual environment using Python 3.13 and also generates a Pipfile and Pipfile.lock in your project directory. The Pipfile lists your direct dependencies, and Pipfile.lock records the exact versions of all direct and transitive dependencies, ensuring reproducible builds.
4. Install FastAPI and Uvicorn
Now, install FastAPI and Uvicorn into your newly created virtual environment.
pipenv install fastapi uvicorn websockets
fastapi is our web framework, uvicorn is the server that will run our FastAPI application, and websockets provides the necessary tools for real-time communication. pipenv will automatically add these to your Pipfile and update Pipfile.lock.
5. Verify Installation
To ensure everything is set up correctly, you can activate the shell within the virtual environment and check the installed packages.
pipenv shell
Your command prompt might change to indicate you are in the virtual environment. Then run:
pip list
You should see fastapi, uvicorn, websockets, and their dependencies listed.
To exit the virtual environment shell, simply type exit.
Summary/Key Takeaways
You have successfully initialized your project, set up a virtual environment using pipenv, and installed the core dependencies: FastAPI, Uvicorn, and WebSockets. This foundation is crucial for maintaining a clean and manageable project as we add more features and dependencies.
In the next chapter, we’ll write our very first FastAPI endpoint and establish the basic structure for our application.