Skip to content

FIX for cannot import name 'parse_qs' from 'cgi' ERROR in Tilestache/Mapnik/Ubuntu-20.04/Python-3.8 #367

@The-GeoStack-Project

Description

@The-GeoStack-Project

The following is for anyone who uses TileStache in combination with Mapnik and Python 3.8 (e.g. in Ubuntu 20.4) and encounters the following error:

ImportError: cannot import name 'parse_qs' from 'cgi' (/usr/lib/python3.8/cgi.py)

The reason this error occurs is as follows:
parse_qs, parse_qsl, and escape are removed from the cgi module. They are deprecated in Python 3.2 or older. They should be imported from the urllib.parse and html modules instead. Source

The following steps should be performed to fix this:

  1. Open a Terminal (Ctrl + alt + t on your keyboard)

  2. Open the TileStache Python 3 compatibility file by running the following command

    sudo nano /home/geostack/.local/lib/python3.8/site-packages/TileStache/py3_compat.py

  3. Change the following:

try:
   import urllib.request as urllib2
   import http.client as httplib
   from urllib.parse import urlparse, urljoin, parse_qsl
   from urllib.request import urlopen
   from cgi import parse_qs
   from _thread import allocate_lock
   unichr = chr
except ImportError:
   # Python 2
   import httplib
   import urllib2
   from urlparse import urlparse, urljoin, parse_qs, parse_qsl
   from urllib import urlopen
   from thread import allocate_lock
   unichr = unichr

TO:

import urllib.request as urllib2
import http.client as httplib
from urllib.parse import urlparse, urljoin, parse_qsl, parse_qs
from urllib.request import urlopen
# from cgi import parse_qs
from _thread import allocate_lock
unichr = chr
  1. Save the file (Ctrl + s on your keyboard)

  2. Run Tilestache

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions