Loading video player…

Setting Up Your Environment

00:00 Now let’s go ahead and set up the environment for your Flask project. Before you start writing any Flask code, you should set up a virtual environment. This step is crucial for organizing your project and managing its dependencies.

00:14 So why do you need to use virtual environments? With virtual environments, you can control the versions of packages used for your project and manage all the required Python dependencies for your project in one place.

00:26 Virtual environments help to prevent conflicts between projects that might want different versions of the same tools. In short, they provide a dedicated space for each project’s dependencies, making sure that the project dependencies don’t clash or interfere with each other.

00:41 This also helps to keep your system clean and organized. Now let’s go ahead and create and activate your first virtual environment.

00:51 Open Visual Studio Code, or any other IDE that you’re using, and let’s create a new project folder.

01:04 Now open the project folder that you have just created inside your IDE. You will create the virtual environment for your project inside the project folder. Open terminal, and navigate to your project directory.

01:16 If you open terminal from within VS Code, it’ll already be navigated to your project directory. To create a new virtual environment, enter the command python -m venv, and then write any name that you want to give to your virtual environment name.

01:33 I’ll just write venv.

01:36 Execute the command by hitting Enter, or the return key.

01:41 As the output of this command, a new folder called venv will be created in the root directory of your project. Once the virtual environment is created, you need to activate it so that you can install the required dependencies in the virtual environment.

01:56 First, let’s see how you can activate it on Windows, and then I’ll show you how you can activate it on Mac or Linux-based systems. In Windows, type the name of your virtual environment, backward slash, then Scripts.

02:11 Make sure the S is capital. Then backward slash again, and then type activate.

02:17 This will activate the virtual environment for you as denoted by the virtual environment name in front of your directory. If you are on Mac or Linux, the command to activate the virtual environment is source your virtual environment name, forward slash bin forward slash, and then activate.

02:37 After the virtual environment is activated, let’s install Flask. To install Flask, enter the command python -m pip install flask.

02:49 Now the Flask installation has started.

02:58 Congrats. You’ve successfully installed Flask in the virtual environment, which will only be available to your project. In a similar way, you can later on install any Python package required for your project.

Become a Member to join the conversation.