error

Laura Creighton lac at openend.se
Wed Sep 2 20:42:11 CEST 2015


>import numpy as np
>
>import netCDF4
>
>f = netCDF4.Dataset('uwnd.mon.ltm.nc','r')
>
>
>f.variables
>
>
>and I had the message:
>
>
>netcdf4.py
>Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
>NameError: name 'netcdf4' is not defined

You have a file called netcdf4.py
It contains the line:
import netCDF4

python comes along, reads
"import netCDF4"
and goes looking for something named netCDF4 or netCdf4 or netcdf4
or NETDCF4 or NeTcDf4  -- the rules of your file system say that
filenames are case-insensitive ... so all the above are equivalent.
The first place it looks is your home directory.  Then it looks in
your PYTHONPATH.  Then it looks in the system modules you nicely
loaded for it.

Unfortunately for you, it finds the file netcdf4.py in your home
directory and thinks that is the one you wanted to import.

BANG.

Solution -- stop naming your python files as lower-cased versions
of things you want to import.  Renaming it to mynetcdf4.py should
work fine, for instance.

Laura


More information about the Python-list mailing list