Flask and Python Virtual Environments
Follow the instructions in this guide to create a Python virtual environment, install Flask, and run a simple Flask web app.
Overview
Many Python programs and libraries can be easily installed as packages from the Python Package Index (PyPI). This is typically done using the command pip
. pip
will install packages into a Python environment.
When you install Python, your computer has a system-wide Python environment which consists of a Python interpreter and a set of packages that the system uses by default. Virtual environments are a great way to keep this system environment uncluttered. Each virtual environment lives in a directory and contains a Python interpreter and packages that only that environment will use.
This can be extremly useful if you’re working on projects that use different versions of Python, or if you have different projects that depend on different packages. Once you are done with a virtual environment, you can remove it and not have to worry about unused packages hanging around in your system-wide environment.
Creating a Virtual Environment
We’ll be creating a Python virtual environment to house Flask in order to avoid installing Flask across the entire system. The venv
module of Python 3 allows you to create virtual environments. This module can be invoked on the command line, or you can set up the virtual environment from within PyCharm. Both methods are discussed below.
On the Command Line
To create a virtual environment using the command line, you want to run a command like this:
python3 -m venv path/to/new/virtual/environment
For example, to create a new virtuan environment named flask_env
within the current directory:
python3 -m venv flask_env
This creates a directory called flask_env
in the current directory which holds all the info needed for the virtual environment.
Next you need to activate the virtual environment for the current terminal session:
source flask_env/bin/activate
You can tell that a virtual environment has been activated because it will add the name of the virtual environment in parentheses to the beginning of your prompt. In macOS you will see something like this:
(flask_env) Your-MacBook:directory username$
Now any Python-related commands that are run in the terminal use the virtual environment’s Python iterpreter and packages.
Within PyCharm
You can create a virtual environment within PyCharm when creating a project. Instead of selecting an existing interpreter when creating the project, you want to create a new environment using Virtualenv. If you have already created the project and want to switch to using a virtual environment, you should open up the PyCharm settings, go to “Project Interpreter”, click on the gear icon, and select “Add”. You want the base interpreter to be the Python 3 that you have on your computer.
Installing Flask
Once the virtual environment is activated, you need to install Flask. Again, this is different depending on whether you do it on the command line or through PyCharm, but will have the same effect.
On the Command Line
installing Flask is as easy as running the following:
pip install flask
Within PyCharm
Open up the “Project Interpreter” settings. Be sure you are using a virtual environment as your interpreter. Click on the “+” icon. This will show you a list of all the available packages. Search for flask, select the package “Flask”, and click “Install Package”.
Running a Flask App
Here is a very simple “Hello, world!” Flask app. Place this code in a file named simple_app.py
:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "Hello, world!"
if __name__ == '__main__':
app.run()
Now you can run simple_app.py
from within PyCharm, or on the command line like this:
python3 simple_app.py
If all goes well, you should see output that looks like this:
* Serving Flask app "simple_app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
By default, Flask apps listen for incoming connections on your computer’s port 5000. Once the server is running, go into a browser and navigate to http://127.0.0.1:5000/
or http://localhost:5000
. You should see the text “Hello, world!” appear.