Set Up a Maintainable Flask Application in a Virtual Environment
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
Setting up a new Flask Application
Here are the steps to set up the foundation for a maintainable Flask application.
- Create a folder for our new application called
yumroad-app
- With your terminal, create a virtual environment in the
yumroad-app
folder by runningpython3 -m venv env
- Activate the Virtual Environment by running
source env/bin/activate
in your terminal (You should see your prompt prefixed with a(env)
to know that your virtual env is activated)
caution
Your virtualenv needs to be activated in every terminal session where you plan to run the application If you open a new tab or window for example and want to run commands that depend on things installed within the virtual environment, you'll need to run the activation command again.
- Create a
requirements.txt
that lists the following starting libraries as requirements
flask
gunicorn
pytest
pytest-flask
pytest-cov
- Install those libraries by running
pip install -r requirements.txt
- Create a folder called
yumroad
and then a file called__init__.py
within theyumroad
folder - Setup the basic application factory pattern in the
__init__.py
file
This lesson preview is part of the Fullstack Flask: Build a Complete SaaS App with Flask course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to Fullstack Flask: Build a Complete SaaS App with Flask with a single-time purchase.

[00:00 - 00:06] All right. So in order to get started with YumRoad, we're going to have to go and create a new Flask application.
[00:07 - 00:24] What you're going to see here is the steps that I take whenever I'm creating a new Python application using Flask. You can repeat these steps as it makes sense whenever you're creating a new application that's not using a boilerplate template.
[00:25 - 00:26] OK. First off, I'm going to explore our project directory.
[00:27 - 00:34] And we can see that there's two folders for the two applications we've created so far. I'm going to create a new one called YumRoad-App.
[00:35 - 00:43] And within YumRoad-App, I'm going to go and create a virtual environment. Again, we're going to do that using the VEN or virtual environment module.
[00:44 - 00:51] We're going to create that within the N folder. Then we're going to activate the script there.
[00:52 - 01:00] And now we're in our virtual environment. Next up is we're going to go and open our folder in a code editor.
[01:01 - 01:05] So I have it opened here in VS code. And you can see there's nothing really here.
[01:06 - 01:13] There's that N folder. But I'm going to go ahead and create a requirements.txt file.
[01:14 - 01:21] So here we're going to add all the dependencies that our application depends on . So Flask is a good one to start with.
[01:22 - 01:30] We're going to add GUnicorn here to serve our application in production. And then some things we'll need to test our application.
[01:31 - 01:38] So we're just going to start with PyTest+ and PyTest.com. So these are some good basic starters to work with.
[01:39 - 01:55] Going back in our terminal, we're going to go ahead and run pip install-r requirements.txt. Now one thing we'll also want is if we're tracking our project in Git, we'll want to create a gitignore file.
[01:56 - 02:07] So we can do that there and we can start by specifying some things and files that we don't want to track. Back in VS code, we can paste some information here.
[02:08 - 02:15] So this is what I like to start with for a simple Python application. There's really not too much here.
[02:16 - 02:26] And you can download templates for Python here. But this is just giving us a starter to ignore any secret files or the virtual environment or any cache files.
[02:27 - 02:33] Okay. Now going back in our terminal, we can also create a new git project.
[02:34 - 02:48] This step is really optional, but if you're making a complex project, it's always a good idea to start with the git repository locally. Then you can add your project files and commit it as necessary.
[02:49 - 02:57] Okay. Now that we have a git repository set up, let's go ahead and create a folder for where we're going to put all of our application code.
[02:58 - 03:05] In VS code, I'm going to go and create a folder called YumRoad. And this YumRoad folder is where all of our application code is going to live.
[03:06 - 03:21] I'm going to create something here to create a nipi file in the YumRoad module that will be where we create our app. So if you remember from the application factory pattern, we did something very similar earlier in the course.
[03:22 - 03:31] And we're going to start with our defaults from Flask and Power Flask. And we're going to use the create app function again.
[03:32 - 03:45] And our job here will be to return some kind of app instance for Flask. So to initialize that, we can just start with Flask and then name just like usual.
[03:46 - 03:53] All right. So this is the very basic create application factory pattern.
[03:54 - 04:00] The next thing we're going to do is set up some configuration variables. So our configuration is obviously going to change for every different environment.
[04:01 - 04:10] So we're going to go and create a config.py file as well. The config.py file is where I'll specify all of the defaults that I care about.
[04:11 - 04:22] So for example, we probably want to specify the test needs false and the debug is false in the base config. And then we can change that for other classes.
[04:23 - 04:44] For example, our dev config will want to inherit from the base config, but specifically set debug equals true, for example. Now these setting variables are not particularly useful because Flask and the end file we set when we run our applications, we'll set them.
[04:45 - 05:02] But if we had anything specifically wanted to test, for example, a flash test config, this config, I could say testing is equal to true here. And then we'll also define one for the prod config.
[05:03 - 05:19] So nothing here for now. Then we'll create a configurations object that we can look up into later.
[05:20 - 05:27] So this is just one way of setting up your configuration files or plenty of other ways the flash supports. This is just the one that I've liked the most.
[05:28 - 05:34] All right. And I use OS here so that we can import environment variables later.
[05:35 - 05:38] For now, we don't have any so we can leave a blank. All right.
[05:39 - 05:59] Going back in our application init file, we now are going to need to import that file. So we're going to say from yumroad.config, again, using the full path there so that the import order is clear for anyone who reads it, we're going to say app.config and we're going to load it from an object.
[06:00 - 06:06] And we're going to say configurations and the name of the environment. Okay.
[06:07 - 06:13] So at this point, we can actually go back into our terminal and launch our application. There's really nothing in here.
[06:14 - 06:30] This is just a sanity check to make sure everything works. So we're going to say if last and is equal to development and flask app is equal to yumroad.create app, then we can run plus run.
[06:31 - 06:36] And then at that point, we can actually load our application in our browser. Okay.
[06:37 - 06:56] It's going to give us a 404 page because we haven't defined any routes, but looking back at a browser, it loaded it properly, which is really the test we were going for here. Now one thing I'm going to do is save us a little bit of time is I'm just going to export some of these variables into our terminal session.
[06:57 - 07:06] So for example, flask app is equal to yumroad.create app. Now when I run our command, I can just type flask run.
[07:07 - 07:15] And instead of that whole script, you can. Create a script in your own folder to help you do this.
[07:16 - 07:30] For example, dev server dot sh as a bash script that does this, for example, or you can just set the environment variables explicitly inside of every terminal session. Either one really works.
[07:31 - 07:35] But for now, I'm just going to keep them in our session. Okay.
[07:36 - 07:43] And now that we have the basic application structure, we're next going to think about setting up a database, which will be in the next video.