Мне нужно создать библиотеку Windows из кода Python. Чтобы скрыть исходный код, я хочу цитировать его и создать единую библиотеку, которую сможет использовать каждый.
Моя структура папок
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 и main.py — пустые файлы.
Я использую код Visual Studio.
Когда я запускаю эту команду: python setup.py build_ext --inplace, я получаю ошибку:
ССЫЛКА: ошибка LNK2001: неразрешенный внешний символ PyInit_sample
build\temp.win-amd64-cpython-312\Release\temp_build\sample\sample.cp312-win_amd64.lib: фатальная ошибка LNK1120: 1 неразрешенная внешняя ошибка
ошибка: команда 'C:\Program Files\Microsoft Visual Studio \2022\Professional\VC\Tools\MSVC\14.33.31629\bin\HostX86\x64\link.exe' произошел сбой с кодом выхода 1120
Мой 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.")
Подробнее здесь: https://stackoverflow.com/questions/790 ... link-error