简体   繁体   English

通过带有命令行参数的Cython运行python代码

[英]Running python code through Cython with command line arguments

I have a Python code that uses two command line arguments. 我有一个使用两个命令行参数的Python代码。 I am using linux terminal for all command line tasks. 我正在使用Linux终端执行所有命令行任务。 Now I am trying to use Cython to speed up my Python code. 现在,我正在尝试使用Cython来加快我的Python代码的速度。 For that I have compiled the Python code to C using build_ext module by creating this setup.py file: 为此,我通过创建以下setup.py文件使用build_ext模块将Python代码编译为C:

setup.py setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup (
cmdclass = {'build_ext': build_ext } ,
ext_modules = [
Extension ("myCode", ["myCode.py"]) ,
])

And then compiling my Python code into C using: 然后使用以下命令将我的Python代码编译为C:

python setup.py build_ext -i

The following were generated: 生成了以下内容:

[file]myCode.c
[file]myCode.so
[folder]build
--[folder]temp.linux-x86_64-2.7
----[file]myCode.o

I want to run the generated file with command line arguments. 我想使用命令行参数运行生成的文件。 Till now in Python I was using the usual command 到目前为止,在Python中,我使用的是常规命令

>> python myCode.py arg1 arg2

I am very new to Cython, infact I started using it to address the inherent speed issue of Python after code level algorithm optimization. 我对Cython并不陌生,实际上我开始使用它来解决代码级算法优化后Python固有的速度问题。 I need inputs on which files to run, and how to run the converted C code and with command line arguments. 我需要输入要运行的文件,以及如何运行转换后的C代码以及使用命令行参数。 Thanks in advance. 提前致谢。

As mentioned you compiled a Python module. 如前所述,您编译了一个Python模块。 So to call from Linux you have to write a .py script that imports your compiled module and does any calculations needed. 因此,要从Linux进行调用,您必须编写一个.py脚本,该脚本会导入您的已编译模块并进行所需的任何计算。 Then you can run it with your typical Linux command. 然后,您可以使用典型的Linux命令运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM