简体   繁体   中英

Cannot create a cdo instance in python

My python version is 3.10.6. I want to create an instance of a Cdo object in python ( https://pypi.org/project/cdo/ ).

from cdo import *
cdo = Cdo()

Howerver, I am getting the following error:

/usr/bin/python3.10 /home/iliamous/PycharmProjects/winbank-esg-risk-gis-platform/scratch_work.py 
Traceback (most recent call last):
  File "/home/iliamous/PycharmProjects/winbank-esg-risk-gis-platform/scratch_work.py", line 209, in <module>
    cdo = Cdo()
  File "/home/iliamous/.local/lib/python3.10/site-packages/cdo.py", line 190, in __init__
    self.operators         = self.__getOperators()
  File "/home/iliamous/.local/lib/python3.10/site-packages/cdo.py", line 283, in __getOperators
    version = parse_version(getCdoVersion(self.CDO))
  File "/home/iliamous/.local/lib/python3.10/site-packages/cdo.py", line 78, in getCdoVersion
    proc = subprocess.Popen([path2cdo, '-V'], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
  File "/usr/lib/python3.10/subprocess.py", line 969, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.10/subprocess.py", line 1720, in _execute_child
    and os.path.dirname(executable)
  File "/usr/lib/python3.10/posixpath.py", line 152, in dirname
    p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not NoneType

You seem to be missing the path2cdo parameter in your Cdo() call. The path2cdo parameter is a string that indicates the location of your cdo executable. You need to provide this in order for the Cdo() class to correctly initialize.

For example:

cdo = Cdo(path2cdo='/path/to/cdo')

The easiest way to find the path is to use the 'which' command in the terminal. For example:

which cdo

This should output the full path to the cdo executable. You can then pass this full path to the Cdo constructor as follows:

cdo = Cdo(path2cdo='/path/to/cdo')

Seems there was an issue with the installation of the package. First, I removed the package by writing in the terminal

pip uninstall cdo

Then, I installed cdo again, this time using the command line

sudo apt-get -y install cdo

For more info check https://installati.one/ubuntu/20.04/cdo/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM