The above method did work and the key was to use "from osgeo import gdal" instead of "import gdal". The gdal code that I copied from elsewhere had errors. When I corrected the code it worked.