Skip to content

Python: TypeError: Object of type DictValue is not JSON serializable #491

Closed
@Datseris

Description

@Datseris

Affects: PythonCall

Describe the bug

I am converting some old code I had with PyCall.jl to PythonCall.jl. Unfortunately, the PythonCall version does not work and gives error:

Python: TypeError: Object of type DictValue is not JSON serializable  

I am not sure how easy it is to make a MWE, as it requires downloading some climate data from copernicus that requires an account. Here is the setup I have:

installation:

using CondaPkg
CondaPkg.add("cdsapi"; channel = "conda-forge")
using PythonCall
cdsapi = pyimport("cdsapi")

then I must make a dictionary that stores the configuration options for what data to download. I do this using something like:

config =
Dict(
    "product_type" => "monthly_averaged_reanalysis",
    "variable" => [
        "2m_temperature", "surface_pressure", "sea_surface_temperature",
    ],
    # more options...
)

then, I download the data by calling the command:

c = cdsapi.Client()
c.retrieve("reanalysis-era5-single-levels-monthly-means", Py(config), "filename.nc")

I also tried without wrapping the config to Py, but I got the same error.

Exactly the same piece of code worked with PyCall (with the same pyimport statement.

I am not sure what the problem is or what the bug is. I am happy to help to solve this if you need more information

Here I am attaching the Python code version that would work, and comes from this webpage.

import cdsapi

c = cdsapi.Client()

c.retrieve(
    'reanalysis-era5-single-levels-monthly-means',
    {
        'format': 'netcdf',
        'product_type': 'monthly_averaged_reanalysis',
        'variable': [
            '10m_wind_speed', '2m_temperature', 'cloud_base_height',
        ],
        'year': [
            '2018',
        ],
        'month': [
            '01', 
        ],
        'time': '00:00',
        'area': [
            90, -180, -90,
            180,
        ],
    },
    'download.nc')

Your system
Please provide detailed information about your system:

  • Windows 10
  • Julia = v1.10, PythonCall = v0.9.19,
    Conda status:
CondaPkg Status ...\CondaPkg.toml
Not Resolved (resolve first for more information)
Packages
  cdsapi (channel=conda-forge)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions