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

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

You have a file called
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 in your home
directory and thinks that is the one you wanted to import.


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


More information about the Python-list mailing list