iiasa / ixmp4

A data warehouse for high-powered scenario analysis in the domain of integrated assessment of climate change and energy systems modeling
https://docs.ece.iiasa.ac.at/ixmp4
MIT License
11 stars 6 forks source link

pydantic error #93

Closed PBL-markroelfsema closed 4 months ago

PBL-markroelfsema commented 4 months ago

When I try to install (via Conda) the ixmp4 package, I get the following error

Traceback (most recent call last):
  File "X:\user\roelfsemam\environments\glasgow_init_env\Scripts\ixmp4-script.py", line 5, in <module>
    from ixmp4.__main__ import app
  File "C:\Users\roelfsemam\AppData\Roaming\Python\Python311\site-packages\ixmp4\__init__.py", line 4, in <module>
    from ixmp4.core import Model as Model
  File "C:\Users\roelfsemam\AppData\Roaming\Python\Python311\site-packages\ixmp4\core\__init__.py", line 2, in <module>
    from .iamc.variable import Variable as Variable
  File "C:\Users\roelfsemam\AppData\Roaming\Python\Python311\site-packages\ixmp4\core\iamc\__init__.py", line 3, in <module>
    from .data import RunIamcData, PlatformIamcData
  File "C:\Users\roelfsemam\AppData\Roaming\Python\Python311\site-packages\ixmp4\core\iamc\data.py", line 9, in <module>
    from ixmp4.data.backend import Backend
  File "C:\Users\roelfsemam\AppData\Roaming\Python\Python311\site-packages\ixmp4\data\backend\__init__.py", line 3, in <module>
    from .api import RestBackend, RestTestBackend
  File "C:\Users\roelfsemam\AppData\Roaming\Python\Python311\site-packages\ixmp4\data\backend\api.py", line 8, in <module>
    from ixmp4.conf import settings
  File "C:\Users\roelfsemam\AppData\Roaming\Python\Python311\site-packages\ixmp4\conf\__init__.py", line 3, in <module>
    from ixmp4.conf.settings import Settings
  File "C:\Users\roelfsemam\AppData\Roaming\Python\Python311\site-packages\ixmp4\conf\settings.py", line 8, in <module>
    from pydantic import Field, HttpUrl, field_validator
ImportError: cannot import name 'field_validator' from 'pydantic' (X:\user\roelfsemam\environments\glasgow_init_env\Lib\site-packages\pydantic\__init__.cp311-win_amd64.pyd)

On StackOverlfow they seem to suggest that the call to pydantic has changed to from pydantic.functional_validators import field_validator https://stackoverflow.com/questions/77172420/importerror-cannot-import-name-field-validator-from-pydantic

PBL-markroelfsema commented 4 months ago

The pydantic version that was installed on my machine is

pydantic 2.7.4 pypi_0 pypi

glatterf42 commented 4 months ago

Thanks for reporting this issue, @PBL-markroelfsema, and sorry for the late reply! Unfortunately, I'm unable to reproduce your error on my local system. Even after updating to pydantic 2.7.4, all tests run correctly. So I'm going to need more information to help you solve your problem.

First of, your information about pydantic seems to indicate that it was installed from pypi. Conda and pypi (installing with pip) don't mix very well and it is generally recommended to use pip after you installed as much as possible with conda, and then stay with pip. Please also see conda's guide on this. In fact, I don't immediately see why anything other than this mixture might cause your issue. You're using Python 3.11, which is fairly recent, on Windows, which is fine, and your ixmp4 version is recent enough (we still have this exact line in our latest code). Could you please confirm which version of ixmp4 you're using?

It even looks like you're using a virtual environment glasgow_init_env. So my current best guess is that you installed ixmp4 via conda, but other packages like pydantic via pip and for some reason, the conda-installed ixmp4 doesn't know where to find those. Because even the latest pydantic version still has field_validator importable from its top level. So please make sure to follow the guide and use, if possible, either conda or pip in any one virtual environment.

If this makes you want to choose between conda and pip, I would highly recommend you use pip. It is just much more user-friendly in my experience.

PBL-markroelfsema commented 4 months ago

Thanks @glatterf42, I have reinstalled my environment avoiding pip, and now it works. So, the confusion between conda and pip must have resulted in the error.

glatterf42 commented 4 months ago

Glad to hear that! I'm closing this issue, if you encounter any other issues, please open a new one :)