[Python] Library 버전 오류?

띠로리·2024년 11월 15일

AttributeError: 'EntryPoints' object has no attribute 'get'

.nc, 즉 NetCDF 파일을 분석하기 위해 .csv 파일로 변경하는 파이썬 코드를 실행시켰더니 만난 오류이다. 여기저기 검색해봐도 다들 버전 문제라고 해서 지구끝까지 다운그레이드를 했는데도 오류가 해결이 안 됐다... 혹시 너무 내려가도 안 되는 건가 싶어 온갖 버전을 다 해봤지만 어떤 버전에서도 'get'이 존재하지 않는다는 오류가 계속해서 떴다.

from netcdf2csv import convert_dir
import xarray as xr
import scipy
import os

netcdf_dir = '/Users/UserName/Downloads/test/nc';
csv_dir = '/Users/UserName/Downloads/test/csv';
clean_csv_dir='/Users/UserName/Downloads/test/csv/clean'

if not os.path.exists(csv_dir) :
    os.makedirs(csv_dir)
if not os.path.exists(clean_csv_dir) :
    os.makedirs(clean_csv_dir)
convert_dir(netcdf_dir, csv_dir, clean_csv_dir, clean_choice=1)

1. 경로

일단, 라이브러리 에러가 나기 전 경로 에러가 계속해서 떴다. 권한 문제라고 하던데, 권한은 제대로 설정이 되어 있었고, Jupyter Notebook으로 실행시켜서 그런 건가 싶었다. 노트북에는 Python 세팅이 안 되어 있는 상태라 방법을 찾다가 상대 경로를 절대 경로로 바꾸자 해결 됐다. 하지만 이건 에러의 시작일 뿐...이었다...

2. Get이 없어요 ㅠ.ㅠ

--------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[18], line 14
     12 if not os.path.exists(clean_csv_dir) :
     13     os.makedirs(clean_csv_dir)
---> 14 convert_dir(netcdf_dir, csv_dir, cleaned_csv_dir=clean_csv_dir, clean_choice = 1)

File /opt/anaconda3/lib/python3.12/site-packages/netcdf2csv/__init__.py:47, in convert_dir(netcdf_dir, csv_dir, cleaned_csv_dir, clean_choice)
     45 for i,filename in enumerate(file_list):
     46     printProgressBar(i + 1, l, prefix = 'Progress:', suffix = 'Complete')
---> 47     ds = xr.open_dataset(os.path.join(netcdf_dir, filename))
     48     df = ds.to_dataframe()
     49     df.to_csv(os.path.join(csv_dir,'uncleaned_'+ filename[:-3] + '.csv'))

File /opt/anaconda3/lib/python3.12/site-packages/xarray/backends/api.py:479, in open_dataset(filename_or_obj, engine, chunks, cache, decode_cf, mask_and_scale, decode_times, decode_timedelta, use_cftime, concat_characters, decode_coords, drop_variables, backend_kwargs, *args, **kwargs)
    476     kwargs.update(backend_kwargs)
    478 if engine is None:
--> 479     engine = plugins.guess_engine(filename_or_obj)
    481 backend = plugins.get_backend(engine)
    483 decoders = _resolve_decoders_kwargs(
    484     decode_cf,
    485     open_backend_dataset_parameters=backend.open_dataset_parameters,
   (...)
    491     decode_coords=decode_coords,
    492 )

File /opt/anaconda3/lib/python3.12/site-packages/xarray/backends/plugins.py:103, in guess_engine(store_spec)
    102 def guess_engine(store_spec):
--> 103     engines = list_engines()
    105     for engine, backend in engines.items():
    106         try:

File /opt/anaconda3/lib/python3.12/site-packages/xarray/backends/plugins.py:98, in list_engines()
     96 @functools.lru_cache(maxsize=1)
     97 def list_engines():
---> 98     entrypoints = entry_points().get("xarray.backends", ())
     99     return build_engines(entrypoints)

AttributeError: 'EntryPoints' object has no attribute 'get'

오류 코드 마지막 쪽을 잘 읽어보면 entrypoints = entry_points().get("xarray.backends", ()) 라인에서 오류가 났다고 적혀있다. 즉, importlib-metadata에 Get 메서드가 존재하지 않아서 생기는 오류인 것이다.

하하 내 코드가 문제가 아니었네

첫 번째로 시도해 본 방법은 4.13.0 버전으로 다운그레이드 하는 것이었다. 특정 버전이 되는 건지 그냥 5버전만 넘기지 않으면 되는 건지는 잘 모르겠지만, 앞서 얘기한 것처럼 어느 버전으로 실행을 시켜도 여전히 오류가 났다 ^^ get은 어디로 숨은걸까?

3. 해결

두 번째로 시도한 방법은 해당 코드를 내가 직접 고치는 것이었다. 결론부터 말하자면, 코드 수정으로 해결했다.

get이 없어졌다는 건 대체할 다른 메서드가 존재한다라는 생각 하나로 인터넷과 지피티를 활용해 새로운 메서드를 찾았다. 그건 바로 select() 메서드.

entrypoints = entry_points().select(group="xarray.backends")

오류가 났던 라인을 위 코드로 변경해주니 문제가 바로 해결되었다 ^_ㅠ..... csv로 변환도 아주~ 깔끔하게 잘 됐다...

주피터 노트북에 변경 사항을 적용시키기 위해 꼭 재시작 할 것.

profile
차곡 차곡 기록 쌓기

0개의 댓글