How to set up Python for backend development
Emmanuel Ayinde
Posted on February 16, 2024
Hey, I see you've been browsing the whole internet on how to install and set up python (latest) on your beautiful yet innocent machine. Is that wrong 😉?
Sigh, I welcome you to this ultimate beginner's guide to Python setup! Python is a powerful and popular programming language, but getting started with it can be overwhelming. In this guide, I'll walk you through the process of setting up Python on different operating systems and creating virtual environments to manage your projects effectively. I also have a bonus for you. I will teach you how to set up Python environments for web frameworks like FastAPI, Flask, and Django, making it easy for you to start building web applications. Let's start with a list of what we'll cover in this tutorial.
Table of Contents
- Download and Installation on MacOS
- Download and Installation on Windows
- Download and Installation on Linux
- Creating Virtual Environment For Web Application
- Bonus - Setting up Python Web Frameworks
Download and Installation on macOS
macOS comes with Python pre-installed, but if it's not installed or you want to manage Python versions separately, you can use a package manager like Homebrew or install it manually.
- Homebrew: Open terminal and run:
> /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
> brew install python3
- Manual Install: Visit the official Python website and download the latest version. Run the installer and follow the instructions.
Download and Installation on Windows
- Visit the official Python website.
- Download the latest version of Python for Windows.
- Run the installer and follow the installation wizard.
- Ensure to check the box that says "Add Python to PATH" during installation.
After you've done everything above run this command
python --version
to confirm that the installation is now successful.
Download and Installation on Linux
Most Linux distributions come with Python pre-installed. You can check by opening a terminal and typing python3 --version
. If Python isn't installed, you can install it using your package manager. For example, on Ubuntu, you would use:
> sudo apt update
> sudo apt install python3
Creating Virtual Environment For Web Application
Now, lets talk about virtual environment a bit.
WHy Virtual Environment?
Creating a virtual environment for your web application is like having a separate room for each project. It keeps your project's code and its dependencies separate from other projects and the main Python setup on your computer. This way, if you need different versions of libraries for different projects, they won't interfere with each other. It also makes it easier to share your project with others because you can package up the virtual environment along with your code. Overall, it helps keep things organized and prevents any messy conflicts between different projects.
How To Create Virtual Environment
- Open a terminal or command prompt (I am using linux at my end here).
- Navigate to your project directory using the
cd
command. - Run the following command to create a virtual environment named env:
> python3 -m venv env
Explaination
python3
: Invokes the Python interpreter.
-m
: Specifies that we want to run a specific module.venv
: The module we want to run, which is responsible for creating virtual environments.env
: The name of the virtual environment we want to create.
Activating Virtual Environments
Haven create a virtual environment, you need to activate it so that Python and pip commands use the packages installed within the virtual environment rather than the global system. I know you are asking _how do i activate it, right? Well, activating a virtual environment is as simple as smashing your PC on the ground when you face bug 😂. That was just a joke 😂😂😂
- Windows:
> .\env\Scripts\activate
- MacOS / Linux
> source env/bin/activate
How has been the tutorial so far??? I believe you are enjoying it. We are wrapping it up already.
Bonus 🔥🔥🔥 - Setting up Python Web Frameworks
Like I said from the begining, I have a bonus for you. The bonus entail teaching you how to create python environment for web frameworks like FastAPI
, Flask
and Django
. Shall we?
Setting Up FastAPI
Installation
After activating your virtual environment, install FastAPI and Uvicorn using pip:
> pip install fastapi uvicorn
- Explanation:
- pip install: Installs Python packages.
- fastapi: Installs the FastAPI framework.
- uvicorn: Installs the Uvicorn ASGI server, which is used to run FastAPI applications.
Congratulations 🎊, you can now start your application using uvicorn you installed in the last module with the following command:
> uvicorn main:app --reload
- Explanation:
- uvicorn: Command to start the Uvicorn server.
- main:app: Specifies the module and object to run, where main is the name of your Python file and app is the instance of your FastAPI application.
- --reload: Automatically reloads the server when code changes are detected, useful for development.
Setting Up Flask
Installation
Install Flask using pip:
> pip install Flask
- Explanation:
- Flask: Installs the Flask framework for building web applications.
Now that we have flask installed in my project directory, we can move on to starting our flask application by navigating to your project directory in the terminal, and running the following command:
> flask run
This command will start the Flask development server, and you should see output indicating that the server is running. By default, Flask runs on http://127.0.0.1:5000/.
Holla 🎊, you have successfully created a Flask app.
...and finally, wrapping up
Setting Up Django
Install Django using pip. Don't forget to activate your virtual environment as explained here
> pip install django
- Explanation:
- django: Installs the Django framework for building web applications.
Run the following command to start the Django development
> python manage.py runserver
This command will start the Django development server, and you should see output indicating that the server is running. By default, Django runs on http://127.0.0.1:8000/.
For more interesting and self-explanatory articles like this, follow me on my social media platforms:
Linkedin
Twitter / X
Instagram
Threads
GitHub,
Also, you can buy me a coffee ☕ to support and encourage me by donating here 👉 Buy Me Coffee
Till next time we meet again, bye 👋👋👋
Posted on February 16, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.