【问题标题】:UnboundLocalError: Local Variable Referenced Before Assignment in Python/DockerUnboundLocalError:在 Python/Docker 中赋值之前引用的局部变量
【发布时间】:2021-12-24 06:46:34
【问题描述】:

在构建 docker 容器映像后,我正在尝试运行一个简单的 Python 脚本以在本地桌面目录中打印/写入 GeoTIFF 的名称。该脚本作为独立脚本执行并生成所需的输出,但是当我尝试通过“docker run”命令运行 docker 容器映像时,我收到一个错误,指出变量是“UnboundLocalError:在分配之前引用了局部变量 'rows' ”。我不确定为什么它在运行容器但作为独立脚本运行时显示此错误。下面是我的 Dockerfile 以及脚本本身。

Dockerfile

FROM continuumio/miniconda3

ADD docker_test.py .

RUN /opt/conda/bin/conda install gdal=3.0.2

CMD [ "python", "./docker_test.py" ]

Python 脚本 (docker_test.py)

import glob
from osgeo import gdal

def image_processing():

    for file in glob.glob("*.tif"): # points to image located in the directory for parsing
        dem = gdal.Open(file)
        rows = dem.RasterYSize
        cols = dem.RasterXSize
 
  return rows, cols

rows, cols = image_processing()

with open('gdal_docker_test.txt', 'w') as f:
    f.write(f'The number of rows is: {rows}')

我觉得它应该是一个简单的修复,但我无法弄清楚调整。非常感谢任何帮助!

【问题讨论】:

    标签: python docker dockerfile containers gdal


    【解决方案1】:

    如果glob.glob("*.tif") 没有返回任何内容,则永远不会创建row 对象。

    还请注意,您的版本只会返回最后一个文件的 rows, cols

    下面的修改版本有望帮助突出问题。

    import glob
    from osgeo import gdal
    
    def image_processing():
        files = glob.glob("*.tif"): # points to image located in the directory for parsing
    
        if not files:
            raise FileNotFoundError("*.tif file not found in: {}".format(glob.glob("*")))
        else:
            for file in files:
                print(file)
                dem = gdal.Open(file)
                rows = dem.RasterYSize
                cols = dem.RasterXSize
            return rows, cols
    
    rows, cols = image_processing()
    
    with open('gdal_docker_test.txt', 'w') as f:
        f.write(f'The number of rows is: {rows}')
    

    最小修改版本:

    import glob
    from osgeo import gdal
    
    def image_processing():
        rows = -1
        cols = -1
    
        for file in glob.glob("*.tif"): # points to image located in the directory for parsing
            dem = gdal.Open(file)
            # This code is never reached in your example because the image isnt' found.
            rows = dem.RasterYSize
            cols = dem.RasterXSize
     
        return rows, cols
    
    rows, cols = image_processing()
    
    with open('gdal_docker_test.txt', 'w') as f:
        f.write(f'The number of rows is: {rows}')
    

    【讨论】:

    • Kapocsi - 感谢您指出这一点,但这实际上是我原来的帖子(现已更新)中的一个错误,而不是代码本身。您建议的编辑实际上是我的脚本显示并作为独立文件成功运行的内容,但在 docker 容器中运行时没有。我相信这是我无法弄清楚的 Docker 特有的东西,再次感谢您的见解!
    • 我明白了。但是,我关于永远不会创建行对象的观点仍然相关。您的程序找不到您的图像文件,并试图在创建/初始化之前返回/引用图像尺寸。请参阅上面的修改版本。
    • Kapocsi - 感谢您的澄清,我认为我们正在接近解决方案。您的编辑确实引发了我的脚本无法通过“FileNotFoundError: *.tif file not found in: ['home', 'srv', 'etc', 'opt', 'root', 'lib','mnt','usr','media','lib64','sys','dev','sbin','boot','bin','run','proc','tmp ', 'var', 'docker_test.py']" 我的 .tif 文件位于绑定到容器的本地桌面文件夹中,所以我不确定它为什么找不到该文件。也许它缺少与 GDAL 的其他依赖项?再次感谢您的帮助!
    • 需要更多关于如何调用 docker 容器的信息。那是一个单独的问题/问题。请参阅文档以获取更多信息:docs.docker.com/storage/bind-mounts
    猜你喜欢
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    相关资源
    最近更新 更多