Before we get right into writing our first example, we need to set up an environment to work and install any dependencies that the project may have. Luckily, Python has a really nice tooling system to work with virtual environments.
Virtual environments in Python are a broad subject, and beyond the scope of this book. However, if you are not familiar with virtual environments, it will suffice to know that a virtual environment is a contained Python environment that is isolated from your global Python installation. This isolation allows developers to easily work with different versions of Python, install packages within the environment, and manage project dependencies without interfering with Python's global installation.
Python's installation comes with a module called venv, which you can use to create virtual environments; the syntax is fairly straightforward. The application that we are going to create is called weatherterm (weather terminal), so we can create a virtual environment with the same name to make it simple.
To create a new virtual environment, open a terminal and run the following command:
$ python3 -m venv weatherterm
If everything goes well, you should see a directory called weatherterm in the directory you are currently at. Now that we have the virtual environment, we just need to activate it with the following command:
$ . weatherterm/bin/activate
Now, we need to create a directory where we are going to create our application. Don't create this directory in the same directory where you created the virtual environment; instead, create a projects directory and create the directory for the application in there. I would recommend you name it with the same name as the virtual environment for simplicity.
Go into the project's directory that you just created and create a file named requirements.txt with the following content:
beautifulsoup4==4.6.0
selenium==3.6.0
These are all the dependencies that we need for this project:
- BeautifulSoup: This is a package for parsing HTML and XML files. We will be using it to parse the HTML that we fetch from weather sites and to get the weather data we need on the terminal. It is very simple to use and it has a great documentation available online at: http://beautiful-soup-4.readthedocs.io/en/latest/.
- Selenium: This is a well-known set of tools for testing. There are many applications, but it is mostly used for the automated testing of web applications.
To install the required packages in our virtual environment, you can run the following command:
pip install -r requirements.txt
One last tool that we need to install is PhantomJS; you can download it from: http://phantomjs.org/download.html
After downloading it, extract the contents inside the weatherterm directory and rename the folder to phantomjs.
With our virtual environment set up and PhantomJS installed, we are ready to start coding!