1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Creating library using Cython and getting link error [duplicate]

Discussão em 'Python' iniciado por Stack, Outubro 4, 2024 às 02:52.

  1. Stack

    Stack Membro Participativo

    I have to create a windows library from python code. To hide my source code, I want to cythonize the code and create a single library which can be used by everyone.

    My folder structure

    manager/
    ├── sample/
    │ ├── __init__.py
    │ ├── a.py
    │ ├── b.py
    │ └── c.py

    │── __init__.py
    ├── main.py
    └── setup.py # For building the shared library


    a.py -

    class a:
    def test(self):
    raise NotImplementedError("Subclasses should implement this!")


    b.py -

    from sample import a

    class b(a):
    def __init__(self):
    return

    def test(self):
    return True


    c.py -

    from sample import a

    class c(a):
    def __init__(self):
    return

    def test(self):

    return True


    init.py and main.py are empty files

    I am using Visual Studio code.

    When I run this command : python setup.py build_ext --inplace , I get error :

    LINK : error LNK2001: unresolved external symbol PyInit_sample build\temp.win-amd64-cpython-312\Release\temp_build\sample\sample.cp312-win_amd64.lib : fatal error LNK1120: 1 unresolved externals error: command 'C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.33.31629\bin\HostX86\x64\link.exe' failed with exit code 1120

    My setup.py :

    from setuptools import setup, Extension
    from Cython.Build import cythonize
    import os

    def find_py_files():
    current_dir = os.getcwd() # Use current directory
    print(f"Searching for Python files in: {current_dir}")
    files = []
    for root, _, filenames in os.walk(current_dir):
    if 'venv' in root: # Ignore the venv directory
    continue
    for filename in filenames:
    if filename.endswith(".py") and filename != "__init__.py" and filename != "setup.py": # Skip __init__.py files
    file_path = os.path.join(root, filename)
    print(f"Found: {os.path.relpath(file_path, start=current_dir)}")
    files.append(file_path)
    return files

    temp_build_dir = "temp_build"

    # Combine all found Python files into a single extension module
    py_files = find_py_files()
    if py_files:
    extension = Extension(
    name = "sample",
    sources = py_files,
    language='c++'
    )

    setup(
    name='sample',
    ext_modules=cythonize(
    [extension],
    build_dir=temp_build_dir,
    ),
    zip_safe=False,
    )
    else:
    print("No Python files found to compile.")


    My issue is that .pyd is only not getting created. The Collapse multiple submodules to one Cython extension tells about import from cython modules after pyd is created

    Continue reading...

Compartilhe esta Página