---
# System prepended metadata

title: 'Python 3 Upgrade: [INVESTIGATION] ckanext-datajson compatibility'

---

# Python 3 Upgrade: [INVESTIGATION] ckanext-datajson compatibility
> https://open-data-ed.atlassian.net/browse/OD-1339

**Previous version**: [ckanext-datajson `0.5.1`](https://github.com/datopian/ckanext-datajson/tree/0.5.1)

**Latest version**: [ckanext-datajson `datagov`](https://github.com/datopian/ckanext-datajson/tree/808b211ba50e681e3b146c76443c456262836d0f)

This extension would need to be updated for python 3 as it does not currently have any python 3 compatible versions.

The ckanext-ed extension imports plugin and controller classes from this extension, so this could be a blocker for the ckanext-ed extension.

## Work that needs to be done

* [Updating the extension to be python 3 compatible](#Updating-the-extension-to-be-python-3-compatible)
* [Updating the extension to be compatible with ckan python 3 features](#Updating-the-extension-to-be-compatible-with-ckan-python-3-features)
* Test that the existing features of the extension all work as expected with python 3 and ckan 2.9.x
* Update the extension documentation to reflect the new changes

### Updating the extension to be python 3 compatible

The latest version of the extension still has python 2 code that needs to be refactored to work with python 3. These are some of the changes (there might also be some required changes that can only be found when the extension is running):

* **Refactoring Import formats**
    * https://github.com/datopian/ckanext-datajson/blob/808b211ba50e681e3b146c76443c456262836d0f/ckanext/datajson/plugin.py#L1
        ```python
        import StringIO
        ```
        would need to be refactored to:
        ```python
        from io import StringIO
        ```
    
    * https://github.com/datopian/ckanext-datajson/blob/808b211ba50e681e3b146c76443c456262836d0f/ckanext/datajson/package2pod.py#L8
        ```python
        from helpers import *
        ```
        would need to be refactored to:
        ```python
        from ckanext.datajson.helpers import *
        ```
    * https://github.com/datopian/ckanext-datajson/blob/808b211ba50e681e3b146c76443c456262836d0f/ckanext/datajson/plugin.py#L15-L16
        ```python
        from helpers import get_export_map_json, detect_publisher, get_validator
        from package2pod import Package2Pod
        ```
        would need to be refactored to:
        ```python
        from ckanext.datajson.helpers import get_export_map_json, detect_publisher, get_validator
        from ckanext.datajson.package2pod import Package2Pod
        ```

### Updating the extension to be compatible with ckan python 3 features

The latest version of the extension still calls ckan code that is not available in a python 3 environment. These are some of the changes (there might also be some required changes that can only be found when the extension is running):

* **Refactor from ckan Controllers to Blueprints**: 
   The extension still uses controllers: https://github.com/datopian/ckanext-datajson/blob/808b211ba50e681e3b146c76443c456262836d0f/ckanext/datajson/plugin.py#L167
    ```python
    class DataJsonController(BaseController):
    ```
    which would not work as controllers ar only available in a python 2 environment: https://github.com/ckan/ckan/blob/ckan-2.9.5/ckan/lib/base.py#L276-L277
    ```python
    if six.PY2:
        class BaseController(WSGIController):
    ```
    For python 3 with ckan, controllers have to be converted to views/Blueprints.

* **Fix broken ckan imports**
    https://github.com/datopian/ckanext-datajson/blob/808b211ba50e681e3b146c76443c456262836d0f/ckanext/datajson/helpers.py#L8-L9
    ```python
    from pylons import config
    from ckan import plugins as p
    ```
    would need to be refactored to
    ```python
    import ckan.plugins as p
    config = p.toolkit.config
    ```