有没有办法将文件导入Cython代码并删除之前的文件

问题描述 投票:0回答:1

我想使用 cython 在我的 python 代码中嵌入一个文件。如果我删除这个文件,我的程序仍然可以运行。

我尝试了这段代码,但我没有找到如何导入 .pyx 文件。

from setuptools import setup
from Cython.Build import cythonize

setup(
    ext_modules=cythonize("streaming_frames.pyx"),
    package_data={'': ['test.pt']}
)

python cython
1个回答
0
投票

确保您已安装

setuptools
软件包,如果没有,则使用以下命令安装:

pip install setuptools

修改您的

setup.py
文件,例如:

from setuptools import setup
from Cython.Build import cythonize

setup(
    ext_modules=cythonize("streaming_frames.pyx"),
    package_data={'': ['test.pt']},
    include_package_data=True,
)

在您的代码中,您可以使用

pkg_resources
访问嵌入文件,例如:

import pkg_resources

def read_embedded_file():
    file_content = pkg_resources.resource_string(__name__, 'test.pt')
    # Do something with the file content
    print(file_content)

# Your other code here
© www.soinside.com 2019 - 2024. All rights reserved.