Python API Development Fundamentals
上QQ阅读APP看书,第一时间看更新

Virtual Environment

PyCharm will help us create a virtual environment. We want to develop our project in its own virtual environment in order to keep it isolated. Due to this, we will have absolute control over the versions of the packages that we are going to use.

The best way to learn is through practice. Let's get our hands dirty now!

Exercise 5: Creating a Development Project in PyCharm

Before you start developing the Python application, you'll need to create a development project in PyCharm. PyCharm manages things using projects. In this exercise, you will learn how to create a new development project in PyCharm called Smilecook. You will also need to install the necessary packages for this project. Let's get started:

  1. Create the project and name it smilecook:

    Figure 2.2: Creating a project

  2. Check the project structure and ensure that the virtual environment has been created. Once the module has been created, we will be able to see the project's hierarchy on the left-hand side panel. We can see the venv folder under the project folder, which was created and activated by PyCharm. Now, when we write code under this project, it will be run in the virtual environment:

    Figure 2.3: Checking the project structure and ensuring that the virtual environment has been created

  3. Install the required packages for this chapter. To do this, create a file called requirements.txt under our project folder. Type in the following code to specify the packages you want to install:

    Flask==1.0.3

    Flask-RESTful==0.3.7

    httpie==1.0.3

  4. Use the pip command to install these packages. After that, in the Terminal tab, at the bottom of Pycharm, use the following pip command to install the packages that we specified in the requirements.txt file:

    pip install -r requirements.txt

  5. You should now see something similar in the following screenshot. Here, we can see that the packages are being installed on the virtual environment:

Figure 2.4: Installing the packages on the virtual environment

Congratulations! You have created a PyCharm project for our Smilecook application. This is the first step of you embarking on your journey as a developer!