cdsapi icon indicating copy to clipboard operation
cdsapi copied to clipboard

ImportError: No module named cdsapi

Open mqadimi opened this issue 4 years ago • 7 comments

Greeting wizards; I'm using code below to download ERA5 data:

=========================================== #!/usr/bin/env python import cdsapi c = cdsapi.Client()

c.retrieve( 'reanalysis-era5-single-levels', { 'product_type':'reanalysis', 'format':'grib', 'variable':[ '10m_u_component_of_wind','10m_v_component_of_wind','2m_dewpoint_temperature', '2m_temperature','land_sea_mask','mean_sea_level_pressure', 'sea_ice_cover','sea_surface_temperature','skin_temperature', 'snow_depth','soil_temperature_level_1','soil_temperature_level_2', 'soil_temperature_level_3','soil_temperature_level_4','surface_pressure', 'volumetric_soil_water_layer_1','volumetric_soil_water_layer_2','volumetric_soil_water_layer_3', 'volumetric_soil_water_layer_4' ], 'date':'20170101/20170331', 'area':'60/80/10/150', 'time':[ '00:00','01:00','02:00', '03:00','04:00','05:00', '06:00','07:00','08:00', '09:00','10:00','11:00', '12:00' ] }, 'ERA5-20170101-20170331-sl.grib')

but there is an error saying:

File "GetERA5-20170101-20170331-sl.py", line 2, in import cdsapi ImportError: No module named cdsapi

I've installed cdsapi via "pip install cdsapi" and it's installed correctly. Any idea what's my problem?

mqadimi avatar Dec 08 '19 17:12 mqadimi

That's because you're running pip for python 2.7. Instead pip go pip3 :

pip3 install cdsapi

Now try to run your test api *.py ;)

c4rt0 avatar Jun 12 '20 09:06 c4rt0

The same with pip3

sottsiougkos avatar Feb 04 '21 14:02 sottsiougkos

The first line in your script should be "#!/usr/bin/env python3" instead of "#!/usr/bin/env python"

ndmsc avatar Jun 30 '21 11:06 ndmsc

I tried both ndmsc and c4rt0's solutions. The suggestions did not work.

I also tried the 3.9 and it's still suggesting no cdsapi module.

#!/users/neuro python3.9

import cdsapi cds = cdsapi.Client() cds.retrieve('reanalysis-era5-pressure-levels', { "variable": "temperature", "pressure_level": "1000", "product_type": "reanalysis", "date": "2017-12-01/2017-12-31", "time": "12:00", "format": "grib" }, 'download.grib')

fhcampbell avatar Oct 18 '21 21:10 fhcampbell

Hi, I had same issue. I saved the lines code into a python script(myscript.py) then I run it by using the terminal with following comand : python myscript.py (make sure in running this command in the folder your have saved the python script). This works for my PC (I have MacBook).

vladamihaesei avatar May 03 '22 20:05 vladamihaesei

Try these steps:

  1. Open cmd in the same file by shortcuts "shift" and right click your mouse at the same time;
  2. Enter command "pip install cdsapi" , and wait until completing installation;
  3. Run your python file in cmd. It won't show the ImportError agian.

hulitiao avatar Jul 28 '22 10:07 hulitiao

It looks like a computer setting which may cause this issue. I finally get my py script running on Command Prompt after adding the ".cdsapirc" file to the following path C:\Users\username.cdsapirc. The funny side is that the same script does not proceed on Spyder showing error "No module name 'cdsapi' ".

JJJ2021 avatar Sep 09 '22 01:09 JJJ2021

Hi, This looks like you have a mix up with your python environments, as opposed to any specific issue with the cdsapi package.

There are several things you could try to solve the issue:

  1. From the command line, execute the request script with your python3 executable, this is equvilent to https://github.com/ecmwf/cdsapi/issues/10#issuecomment-871326986 but more explicit: python3 cds_request.py
  2. You could do the pip install within your request script using !pip install cdsapi. This would ensure that the cdsapi was installed on the environment that is invoked. This is probably considered bad practice.

As there is no specific change to the cdsapi code required for this issue I am going to close the ticket.

I will also highlight the C3S user support channels who are generally quite good at responding to these sort of problems: https://cds.climate.copernicus.eu/cdsapp#!/usersupport

All the best, Eddy

EddyCMWF avatar Feb 27 '23 11:02 EddyCMWF

I encountered the same issue yesterday, while trying to use pip3. Somehow the setuptools package had been deleted in my Python environment and I just needed to download it again using:

pip3 install setuptools

8painkiller8 avatar Oct 15 '23 07:10 8painkiller8

Hello, just for reference I had this error and I followed the solution proposed by @vladamihaesei and it worked. I work in a cluster using Centos. (Description: CentOS Linux release 7.9.2009 (Core) Release: 7.9.2009 )

ditsiaou avatar Feb 03 '24 10:02 ditsiaou

encountered the same problem of no module "cdsapi", @8painkiller8 solution worked for me

Talha2647 avatar Feb 22 '24 18:02 Talha2647