Setting up the environment

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
I recommend installing and using virtualenvwrapper, which is an extension of the virtualenv tool. This makes it very simple to manage, create, and delete virtual environments as well as quickly switch between them. If you wish to investigate this further, visit:  https://virtualenvwrapper.readthedocs.io/en/latest/#.

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.

I am setting  the environment and running all the examples in a machine with Debian 9.2 installed, and at the time of writing, I am running the latest Python version (3.6.2). If you are a Mac user, it shouldn't be so different; however, if you are on Windows, the steps can be slightly different, but it is not hard to find information on how to set up virtual environments on it. A Python 3 installation works nicely on Windows nowadays.

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
It is always a good idea to make use of version-control tools like GIT or Mercurial. It is very helpful to control changes, check history, rollback changes, and more. If you are not familiar with any of these tools, there are plenty of tutorials on the internet. You can get started by checking the documentation for GIT at:  https://git-scm.com/book/en/v1/Getting-Started.

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!