Skip to content

(C). Getting Setup

Zamathuli Luthuli edited this page Oct 30, 2023 · 1 revision

Introduction

Welcome back

Let us begin the setup by creating a virtual environment for our project. Virtual environments are essential for REST API development, providing various benefits:

  1. A virtual environment allows you to create an isolated workspace for your project with its dependencies and packages, like Flask or Django. That helps ensure that your REST API project will not interfere with other projects or the global Python installation on your machine.
  2. Virtual environments allow quick duplication of projects and their dependencies on different machines.
  3. A virtual environment makes managing dependencies for your REST API project easy. You can install, update, or remove packages and libraries without affecting other projects.
  4. Establishing a virtual environment for your REST API project makes setting up and running automated tests in a controlled environment easy. That can help ensure that your code works as expected and that any changes or updates you make do not introduce unexpected bugs or errors.

Getting Setup

To complete the setup, please follow here the outlined steps; this tutorial uses Visual Studio Code IDE, but you may use an IDE of your preference.

Step 1: Create a virtual environment and activate it

1.1 Here are the steps to create a virtual environment for our REST API project.

  1. Create a project folder; you may name this folder 'Stores_REST_API.'
  2. Open the Visual Studio Code, and open your project folder.
  1. Open a command terminal or PowerShell; Navigate to the menu bar, select the 'Terminal' option, and select 'New Terminal.'
  1. From the command terminal, run this code: python3.10 -m venv .api_env, For Windows users, run python -m venv .api_venv.
  1. This command will initialize a new virtual environment in a directory called '.api_venv' (relative to your current location). It will create a virtual environment with isolated Python installation and libraries.

Note:

  • The 'venv' in Python is a built-in module that allows you to create and manage virtual environments.
  • .api_venv is the name of the environment folder; you may change this name to any name you prefer.
  • Files that start with a dot (.) prefix in Unix-like operating systems (such as Linux and macOS) are typically hidden files. However, a dot prefix on this file name indicates that users should not directly access or modify the file in their regular file interactions; the dot prefix itself does not automatically hide the file.

1.2 Here are the steps to activate a virtual environment.

  1. Open a command terminal.
  1. Change the current directory to a virtual environment directory we created in step 1.1; For this tutorial, our directory is '.api_venv'.
  1. Activate the virtual environment by running the activation script; 'source .api_venv/bin/activate.'

Note: The command to activate the virtual environment depends on the shell you are using.

  • If you are using Bash, you can run the following command:
source venv/bin/activate
  • If you are using a different shell, such as Zsh, you can run the following command:
source venv/bin/activate.zsh
  • If you are using windows, you can run the following command:
.\api_venv\Scripts\activate
  • The activate script initializes the virtual environment and modifies your shell's environment variables to use the Python interpreter and libraries within the virtual environment.
  1. To use a newly created virtual environment PowerShell, please delete the current command terminal.
  1. Navigate to the menu bar, select the 'View' option, then select the 'Command Palette' option, followed by the 'Python: Select Interpreter' option. Alternatively, press 'CTRL+SHIFT+P.'
  1. After you have selected the 'Python: Select Interpreter' option, select an interpreter with a path to the virtual environment.
  1. Navigate to the menu bar, select the 'Terminal' option, and select 'New Terminal.'
  1. You should see the virtual environment's name in your terminal prompt, indicating that the environment is active. For example, you now have (.api_venv) before your prompt.

Note: If you continue to encounter problems activating a virtual environment; please follow this link, Creating and Activating a Virtual Environment for Windows OS

1.3 Here is a short video showing how to create and activate a virtual environment:

Untitled.video.4.mp4

Step 2: Install Flask

  1. To install the required packages for building a REST API, run the following command:
pip install flask
  1. The installation process will take a few seconds to finish, and when completed, you should have Flask installed inside the virtual environment. And you should be able to see inside lib various packages such as Flask and Jinja2, et cetera.

Step 3: Create a file for the Flask app (you can name it app.py); create the Flask app.

Here are the steps to create a file for the Flask app:

  1. To create a file for the Flask app, click on the project folder, select the 'New File' option, and name the file 'app.py.'
  1. To create the Flask app, we will write a standard boilerplate:
from flask import Flask
app = Flask(__name__)
3. '**` app`**' is a Flask with**` __name__ `** as the argument; this code snippet creates a Flask app with the provided name, which we will use to build the rest of the REST API project.

Step 4: Use the Flask Command-Line Interface (CLI) to start the app and launch it

To start and launch your Flask app, follow these steps:

  1. Ensure that the terminal has the virtual environment activated and that you are in the folder that contains 'app.py.'

  2. To run the app, run the following command: 'flask run in the terminal.'

  1. This command creates a Flask app and starts it. Initially, you won't see any output.
  2. The app will run at 127.0.0.1:5000, which is the address where you can make requests.
  3. since you have not defined endpoints or functionality, any request made to the app's address will return a 404 not found error.
  4. To stop the app, use the keyboard shortcut **Ctrl+C**.

Now you're ready to define endpoints and add functionality to your Flask app in the section ahead.

Here is the short video showing the setup for our REST API project.

Untitled.video.5.mp4
Clone this wiki locally