Categories
Tech

Getting Started with Python

Piedmont, California street network created in Python with OSMnx, networkx, matplotlibThis is a guide for absolute beginners to get started using Python. Since releasing OSMnx a few weeks ago, I’ve received a lot of comments from people who would love to try it out, but don’t know where to begin with Python. I’ll demonstrate how to get Python up and running on your system, how to install packages, and how to run code.

Step 1: download and install Miniconda

Python is a programming language. Beyond its built-in functionality, there are thousands of additional packages available for you to install and use. To use Python, you need to install a Python distribution on your computer. Anaconda is a popular and easy to use distribution and package manager. We’ll install Miniconda, a lightweight version of the full Anaconda.

First, download the Miniconda installer. Make sure to get the one for your operating system and architecture. Also make sure you download the installer for Python 3.6, not the outdated 2.7 version.

Run the Miniconda installer. In the installer dialog, set the destination folder to C:\Anaconda (or the equivalent if you’re on a Mac, like ~/anaconda). Make sure both boxes are ticked to add Anaconda to the system path and to register Anaconda as the system default Python.

Step 2: install a package

Ok, Python itself is now installed. Now we want to get some useful packages to play around with. We’ll add the conda-forge channel to our conda package manager so we can have access to their massive repository of packages.

Open your computer’s command prompt (Windows) or terminal (Mac/Linux) and run the following two commands:

conda update -n base conda
conda config --prepend channels conda-forge

This adds the conda-forge channel and then updates conda to the latest version. Now let’s install a couple of new packages into an isolated conda environment. The first is OSMnx, a package to download, analyze, and visualize street networks from OpenStreetMap. The second is jupyterlab, which lets us interact with Python code in handy Jupyter notebooks. Run the command:

conda create -n ox --strict-channel-priority osmnx jupyterlab
conda activate ox

Anytime you want to install another package, you can use the conda install package-name command syntax.

Step 3: run some Python code

At your command prompt/terminal window, run the command:

jupyter lab

This launches the Jupyter lab interactive Python environment in your web browser (read more about using Jupyter notebooks). In the top-right, click new > Python 3 notebook to open a new notebook. In the empty cell, paste this snippet of Python code:

import osmnx as ox
%matplotlib inline
G = ox.graph_from_place('Piedmont, California, USA', network_type='drive')
fig, ax = ox.plot_graph(ox.project_graph(G))

This little snippet comprises just four lines of code. The first imports OSMnx (the package we just installed). The second tells matplotlib (a data visualization package) to display any visuals it produces inline within the notebook. The third line tells OSMnx to download the drivable street network for the city of Piedmont, California. The fourth line tells OSMnx to project the street network and use matplotlib to visualize it.

To run this code cell, click it, then press shift + enter. When OSMnx finishes downloading and plotting the street network, you should see something like this:

Piedmont, California street network created in Python with OSMnx, networkx, matplotlib

Next steps

There are lots of examples of stuff you can do with OSMnx in its examples repo. All these examples come packaged as Jupyter notebooks that you can download and run, just like we’ve done in this tutorial.

Similarly, you can install any other Python packages available in conda the same way we demonstrated here. If a package isn’t in conda, read its documentation to see how they recommend you install it. If you’re interested in data science, pandas and scikit-learn are a couple of great places to start playing with code. You might also be interested in my course series on urban data science: all of its materials are available as Jupyter notebooks on GitHub.

Lastly, there are lots of great Python tutorials to help you take your next steps.

10 replies on “Getting Started with Python”

Hi Geoff,

First, thanks for sharing all of this.
I seem to be having trouble. I’ve followed the steps up to step 3, but when I attempt to run the code in the Jupyter notebook, I receive the error: ModuleNotFoundError: No module named ‘osmnx’. Any thoughts? Thanks!

I followed steps you instructed here but failed when I import osmnx in jupyter. I am a newbie. Any advice, please? Thank!

Dear Boeing,

Thanks for the contribution! it’s a great job!

I installed OSMnx using follwoing commands:
conda update -n base conda
conda config –prepend channels conda-forge
conda create -n ox -c conda-forge osmnx jupyterlab
conda activate ox

There were warnings and errors when I tried to use OSMnx in Jupyter-lab. It says, “FutureWarning: ‘+init=:' syntax is deprecated. ':' is the preferred initialization method. When making the change, be mindful of axis order changes:”, and “TypeError: argument of type 'CRS' is not iterable”.

Is there any solution? Appreciate your help!

David

Hey Geoff,

Thank you very much for this step-by-step guide.
Was wondering if you could guide us as to how we could extract the above output as shapefiles.
Tried a couple of suggestions, but they don’t seem to work for me.
Thanks in advance.

Hi Geoff,

First, thanks for sharing all of this.
I seem to be having trouble.
The following errors occurred during my usage(even when i use the example in official document):“ConnectionError: HTTPSConnectionPool(host=’nominatim.openstreetmap.org’, port=443): Max retries exceeded with url: /search?format=json&polygon_geojson=1&dedupe=0&limit=50&q=Modena%2C+Italy (Caused by NewConnectionError(‘<urllib3.connection.HTTPSConnection object at 0x000001F7D96DDEE0>: Failed to establish a new connection: [WinError 10060] ”. Any thoughts? Thanks!

Leave a Comment