Python Programming

Lesson 2

Python virtual environments

Each project you work on should ideally have it's own environment.

Within Python, you can install software from the web and you can use modules and frameworks developed by other programmers.

Imaging this situation.

You're a programmer, working on two projects. Web App #1 and Web App #2

For Web App #1 you're using framework version #10 while for Web App #2 you're using framework version #8.

How would you work on both on the same machine?

/------------------/     /----------------/
|   Web App # 1    |     |   Web App # 2  |
|                  |     |                |
|  Framework # 10  |     |  Framework # 8 |
|                  |     |                |
|                  |     |                |
|                  |     /----------------/
/------------------/

The answer to this is that you can use Python virtualenvironments to keep these projects separate and install different versions of your framework, one in each folder.

Windows console

To start off, we need to get a little more familiar with the windows console

Windows Console

  • dir - lists contents of folder
  • mkdir <Directory Name> - creates new folder
  • cd <Directory Name> - enters a folder
  • cd .. - enters a folder above the current one

Windows 10 makes it easy to get to a console. Just open your project folder, then click <Shift> + <Right Click> and select the option Open command window here.

Windows Console from Folder

Setting up Virtual environment

Open up your console.

First, type python -V (lowercase) to make sure that you can access the Python command from your console.

Python Version

You should see the current version of Python on your system.

If that works, you can proceed. The first command to execute is the one to create the virtual environment.

Creating a virtual environment

python -m venv env

When it finishes, you should have a new folder, env. This folder will hold any external libraries that you install.

Activating a virtual environment

To use the environment, you need to execute this command on Windows:

env\Scripts\activate.bat

or MacOS or Linux:

source env/bin/activate

Once you've done this, you're now in a Python virtual environment

In order to deactivate your virtual environment, run the command

Windows:

env\Scripts\deactivate

MacOS or Linux:

deactivate

Installing dependencies with Pip

Python has a tool for installing software from the web called "Pip." You invoke it from the console.

First, you need to open a console in your project folder, then activate your virtual environment.

Then, you run the pip command.

pip install requests

Installing Wheels

You should have copied the folder python-project to a directory on your machine as well as activated a virtual environment.

You'll have to run the following command after activating:

pip install -f windows-wheels -r requirements.txt

Or, use the provided scripts

  • Windows: install.bat
  • MacOS or Linux: bash install.sh

This will install all of the dependencies in your environment.

Launching IDLE from the Console

In order to make sure IDLE can see your installed libraries, you need to launch it from the console

  • Activate the virtual environment

env\Scripts\activate.bat

  • Launch IDLE

python -m idlelib

Opening a web browser with Python

In [5]:
import webbrowser

webbrowser.open("http://sensi-sl.org")
Out[5]:
True

Getting a webpage with Python

In [6]:
import requests

res = requests.get("http://sensi-sl.org")
In [17]:
for header, value in res.headers.items():
    print("Header: {}".format(header))
    print("Value: {}".format(value))
Header: Date
Value: Mon, 23 Oct 2017 19:06:30 GMT
Header: Server
Value: Apache
Header: X-Powered-By
Value: PHP/5.4.45
Header: Expires
Value: Thu, 19 Nov 1981 08:52:00 GMT
Header: Cache-Control
Value: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Header: Pragma
Value: no-cache
Header: Link
Value: <http://sensi-sl.org/wp-json/>; rel="https://api.w.org/", <http://sensi-sl.org/>; rel=shortlink
Header: X-TEC-API-VERSION
Value: v1
Header: X-TEC-API-ROOT
Value: http://sensi-sl.org/wp-json/tribe/events/v1/
Header: X-TEC-API-ORIGIN
Value: http://sensi-sl.org
Header: Set-Cookie
Value: PHPSESSID=1edc987619ba6ea90de71f914aac38cf; path=/
Header: Vary
Value: Accept-Encoding,User-Agent
Header: Content-Encoding
Value: gzip
Header: Keep-Alive
Value: timeout=5
Header: Connection
Value: Keep-Alive
Header: Transfer-Encoding
Value: chunked
Header: Content-Type
Value: text/html; charset=UTF-8
In [18]:
print(res.ok)
True
In [19]:
print(res.status_code)
200
In [8]:
print(len(res.text))
74075
In [28]:
non_existant = "http://sensi-sl.org/a-page-that-doesn't-exist"

res = requests.get(non_existant)
In [24]:
print(res.ok)
False
In [22]:
print(res.status_code)
404

Scraping Sensi's website with Python

In [29]:
import requests
import bs4

res = requests.get('http://sensi-sl.org/upcoming-events/')
calendar_events = bs4.BeautifulSoup(res.text, 'html5lib')

event_titles = calendar_events.select('.tribe-events-list-event-title')

print("Found {} events on the website".format(len(event_titles)))

for title in event_titles:
    cleaned_title = title.getText().strip()
    print(cleaned_title)
Found 8 events on the website
Game Changing: Creating A Game Changing Team For Lasting Impact
Possibility Mind-Set: Becoming Remarkable
Data Science: Civil Society and Data
Law Clinic
Show and Tell: Open Interaction Nights
Vultures and the City: Are Foreign Businesses Pulling Their Weight In Sierra Leone?
Micro-finance and Its Impact In Africa
Elections and Civil Society