Search code examples
pythonc++shared-librariesimporterror

Why I canot import _crop_and_resize in python?


I have header and C++ files, and I built them in Linux to the "_crop_and_resize.so" file (c++ code is here). But I am receiving importError:file too short.

crop_and_resize.c

crop_and_resize.h

    sources = ['src/crop_and_resize.c']
    headers = ['src/crop_and_resize.h']
    
    extra_compile_args = ['-fopenmp', '-std=c99']
    sources = [os.path.join(this_file, fname) for fname in sources]
    headers = [os.path.join(this_file, fname) for fname in headers]
    extra_objects = [os.path.join(this_file, fname) for fname in extra_objects]
    
    ffi = create_extension(
        '_ext.crop_and_resize',
        headers=headers,
        sources=sources,
        define_macros=defines,
        relative_to=__file__,
        with_cuda=with_cuda,
        extra_objects=extra_objects,
        extra_compile_args=extra_compile_args)

After compilation, I am trying to import "_crop_and_resize.so" in python, but I am receiving an error.

>>> import _crop_and_resize as _backend

the error:

ImportError:_crop_and_resize.so: file too short

specifications:

g++ (GCC)= 9.3.0

python= 3.6.10

Ubuntu 20.04.4 LTS 64bit


Solution

  • Per @AMIRABBAS's comment, the output of stat _crop_and_resize.so on the Ubuntu 20.04.4 LTS, 64 bit terminal is:

    File: _crop_and_resize.so
    Size: 0 Blocks: 40 IO Block: regular empty file
    Device: Inode: Links: 1
    Access: (0640/-rw-r-----) Uid: (000000/ my username) Gid:   
    (000000/ my username)
    Access: 2022-05-11 10:12:48.000000000 -0700
    Modify: 2022-05-11 10:51:27.000000000 -0700
    Change: 2022-05-11 10:51:27.000000000 -0700
    

    For whatever reason, when downloading the repository, it did not clone the file directly. Needless to say, you cannot link to a zero-length file.

    For reference, having downloaded the same file directly from GitHub, the output should be:

    ~/downloads> stat _crop_and_resize.so
    File: _crop_and_resize.so
    Size: 101304        Blocks: 200        IO Block: 4096   regular file
    Device: 19h/25d   Inode: 5602044     Links: 1
    Access: (0644/-rw-r--r--)  Uid: (1000/me)   Gid: (1000/me)
    Access: 2022-05-11 14:24:56.233800387 -0400
    Modify: 2022-05-11 14:22:48.968012681 -0400
    Change: 2022-05-11 14:24:53.433609539 -0400
    Birth: 2022-05-11 14:22:48.940010253 -0400
    

    Update:_crop_and_resize.so was empty, and I successfully downloaded the file, "_crop_and_resize.so" from its repository, and run it again and the current error gone.