{"id":1154595,"date":"2025-01-13T17:53:11","date_gmt":"2025-01-13T09:53:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1154595.html"},"modified":"2025-01-13T17:53:13","modified_gmt":"2025-01-13T09:53:13","slug":"%e5%a6%82%e4%bd%95%e5%bc%80%e5%8f%91python%e6%89%a9%e5%b1%95%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1154595.html","title":{"rendered":"\u5982\u4f55\u5f00\u53d1python\u6269\u5c55\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25184144\/977f9dba-5d72-4a6a-9323-580c099bafa1.webp\" alt=\"\u5982\u4f55\u5f00\u53d1python\u6269\u5c55\u5e93\" \/><\/p>\n<p><p> \u5f00\u53d1Python\u6269\u5c55\u5e93\u9700\u8981\u9075\u5faa\u4e00\u5b9a\u7684\u6b65\u9aa4\u548c\u6d41\u7a0b\uff0c\u5176\u4e2d\u5305\u62ec<strong>\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u3001\u7f16\u5199\u6269\u5c55\u4ee3\u7801\u3001\u7f16\u8bd1\u548c\u5b89\u88c5\u6269\u5c55\u5e93\u3001\u6d4b\u8bd5\u548c\u53d1\u5e03\u6269\u5c55\u5e93<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u63cf\u8ff0\u6bcf\u4e2a\u6b65\u9aa4\uff0c\u4ee5\u5e2e\u52a9\u4f60\u4e86\u89e3\u5982\u4f55\u5f00\u53d1\u4e00\u4e2a\u9ad8\u6548\u7684Python\u6269\u5c55\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u5f00\u53d1Python\u6269\u5c55\u5e93\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u662f\u5173\u952e\u3002\u5e38\u7528\u7684\u5de5\u5177\u6709<strong>Cython\u3001SWIG\u3001Boost.Python<\/strong>\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>Cython<\/strong>\uff1a<\/p>\n<p>Cython \u662f\u4e00\u79cd\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u8f6c\u5316\u4e3aC\u4ee3\u7801\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8Python\u4ee3\u7801\u7684\u6267\u884c\u901f\u5ea6\u3002Cython\u5141\u8bb8\u4f60\u5728Python\u4e2d\u5d4c\u5165C\u4ee3\u7801\uff0c\u4ece\u800c\u4f7f\u5f97\u4f60\u53ef\u4ee5\u5c06\u6027\u80fd\u5173\u952e\u7684\u90e8\u5206\u7528C\u8bed\u8a00\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p><strong>Cython\u7684\u4f18\u52bf\u5728\u4e8e\uff0c\u5b83\u53ef\u4ee5\u65e0\u7f1d\u5730\u4e0e\u73b0\u6709\u7684Python\u4ee3\u7801\u96c6\u6210\uff0c\u4f7f\u5f97\u5f00\u53d1\u8fc7\u7a0b\u66f4\u52a0\u7b80\u4fbf\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>SWIG<\/strong>\uff1a<\/p>\n<p>SWIG\uff08Simplified Wrapper and Interface Generator\uff09\u662f\u4e00\u6b3e\u7528\u4e8e\u8fde\u63a5C\/C++\u4ee3\u7801\u548c\u5404\u79cd\u9ad8\u7ea7\u7f16\u7a0b\u8bed\u8a00\uff08\u5982Python\u3001Perl\u3001Java\u7b49\uff09\u7684\u5de5\u5177\u3002SWIG\u53ef\u4ee5\u81ea\u52a8\u751f\u6210\u5c06C\/C++\u4ee3\u7801\u5c01\u88c5\u5230Python\u6269\u5c55\u6a21\u5757\u4e2d\u6240\u9700\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>SWIG\u7684\u4e3b\u8981\u4f18\u52bf\u662f\u5b83\u7684\u8de8\u8bed\u8a00\u652f\u6301\uff0c\u53ef\u4ee5\u4e00\u6b21\u6027\u751f\u6210\u591a\u79cd\u8bed\u8a00\u7684\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><p><strong>Boost.Python<\/strong>\uff1a<\/p>\n<p>Boost.Python\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u5c06C++\u548cPython\u7ed3\u5408\u8d77\u6765\u7684\u5e93\u3002\u5b83\u8ba9\u4f60\u53ef\u4ee5\u5728C++\u4e2d\u975e\u5e38\u65b9\u4fbf\u5730\u5b9a\u4e49Python\u6a21\u5757\uff0c\u5e76\u4e14\u53ef\u4ee5\u5728Python\u4e2d\u76f4\u63a5\u8c03\u7528C++\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>Boost.Python\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u548cC++\u7684\u7d27\u5bc6\u7ed3\u5408\uff0c\u53ef\u4ee5\u5145\u5206\u5229\u7528C++\u7684\u7279\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7f16\u5199\u6269\u5c55\u4ee3\u7801<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528Cython\u7f16\u5199\u6269\u5c55\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Cython\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install cython<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2aCython\u6587\u4ef6\uff08\u4f8b\u5982<code>mymodule.pyx<\/code>\uff09\uff0c\u7f16\u5199\u4f60\u7684Cython\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mymodule.pyx<\/p>\n<p>def say_hello_to(name):<\/p>\n<p>    print(f&quot;Hello {name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u7528\u4e8e\u7f16\u8bd1\u4f60\u7684Cython\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># setup.py<\/p>\n<p>from setuptools import setup<\/p>\n<p>from Cython.Build import cythonize<\/p>\n<p>setup(<\/p>\n<p>    ext_modules = cythonize(&quot;mymodule.pyx&quot;)<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u7f16\u8bd1\u4f60\u7684Cython\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py build_ext --inplace<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u4f60\u7684\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule<\/p>\n<p>mymodule.say_hello_to(&quot;World&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528SWIG\u7f16\u5199\u6269\u5c55\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5SWIG\uff0c\u53ef\u4ee5\u4ece\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aC\u6587\u4ef6\uff08\u4f8b\u5982<code>mymodule.c<\/code>\uff09\uff0c\u7f16\u5199\u4f60\u7684C\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/* mymodule.c *\/<\/p>\n<p>#include &lt;stdio.h&gt;<\/p>\n<p>void say_hello_to(const char *name) {<\/p>\n<p>    printf(&quot;Hello %s!\\n&quot;, name);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aSWIG\u63a5\u53e3\u6587\u4ef6\uff08\u4f8b\u5982<code>mymodule.i<\/code>\uff09\uff0c\u63cf\u8ff0\u4f60\u7684C\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-swig\">\/* mymodule.i *\/<\/p>\n<p>%module mymodule<\/p>\n<p>%{<\/p>\n<p>#include &quot;mymodule.c&quot;<\/p>\n<p>%}<\/p>\n<p>void say_hello_to(const char *name);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u7528\u4e8e\u7f16\u8bd1\u4f60\u7684SWIG\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># setup.py<\/p>\n<p>from setuptools import setup, Extension<\/p>\n<p>mymodule = Extension(&#39;_mymodule&#39;,<\/p>\n<p>                     sources=[&#39;mymodule_wrap.c&#39;, &#39;mymodule.c&#39;])<\/p>\n<p>setup(<\/p>\n<p>    name=&#39;mymodule&#39;,<\/p>\n<p>    ext_modules=[mymodule],<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884cSWIG\u751f\u6210\u5305\u88c5\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">swig -python mymodule.i<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u7f16\u8bd1\u4f60\u7684\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py build_ext --inplace<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u4f60\u7684\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule<\/p>\n<p>mymodule.say_hello_to(&quot;World&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528Boost.Python\u7f16\u5199\u6269\u5c55\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Boost.Python\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aC++\u6587\u4ef6\uff08\u4f8b\u5982<code>mymodule.cpp<\/code>\uff09\uff0c\u7f16\u5199\u4f60\u7684C++\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">\/* mymodule.cpp *\/<\/p>\n<p>#include &lt;boost\/python.hpp&gt;<\/p>\n<p>#include &lt;iostream&gt;<\/p>\n<p>void say_hello_to(const std::string&amp; name) {<\/p>\n<p>    std::cout &lt;&lt; &quot;Hello &quot; &lt;&lt; name &lt;&lt; &quot;!&quot; &lt;&lt; std::endl;<\/p>\n<p>}<\/p>\n<p>BOOST_PYTHON_MODULE(mymodule) {<\/p>\n<p>    using namespace boost::python;<\/p>\n<p>    def(&quot;say_hello_to&quot;, say_hello_to);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u7528\u4e8e\u7f16\u8bd1\u4f60\u7684Boost.Python\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># setup.py<\/p>\n<p>from setuptools import setup, Extension<\/p>\n<p>mymodule = Extension(&#39;mymodule&#39;,<\/p>\n<p>                     sources=[&#39;mymodule.cpp&#39;],<\/p>\n<p>                     libraries=[&#39;boost_python&#39;])<\/p>\n<p>setup(<\/p>\n<p>    name=&#39;mymodule&#39;,<\/p>\n<p>    ext_modules=[mymodule],<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7f16\u8bd1\u4f60\u7684\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py build_ext --inplace<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u4f60\u7684\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule<\/p>\n<p>mymodule.say_hello_to(&quot;World&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7f16\u8bd1\u548c\u5b89\u88c5\u6269\u5c55\u5e93<\/h3>\n<\/p>\n<p><h4>1. \u7f16\u8bd1\u6269\u5c55\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u5b8c\u6269\u5c55\u4ee3\u7801\u5e76\u914d\u7f6e<code>setup.py<\/code>\u6587\u4ef6\u540e\uff0c\u4f60\u9700\u8981\u7f16\u8bd1\u6269\u5c55\u5e93\u3002\u7f16\u8bd1\u8fc7\u7a0b\u901a\u5e38\u901a\u8fc7\u8fd0\u884c<code>setup.py<\/code>\u6587\u4ef6\u6765\u5b8c\u6210\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u901a\u7528\u7684\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py build_ext --inplace<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u6839\u636e<code>setup.py<\/code>\u6587\u4ef6\u4e2d\u7684\u914d\u7f6e\u7f16\u8bd1\u4f60\u7684\u6269\u5c55\u5e93\uff0c\u5e76\u5c06\u751f\u6210\u7684\u5171\u4eab\u5e93\u6587\u4ef6\u653e\u5728\u5f53\u524d\u76ee\u5f55\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b89\u88c5\u6269\u5c55\u5e93<\/h4>\n<\/p>\n<p><p>\u7f16\u8bd1\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u5b89\u88c5\u6269\u5c55\u5e93\uff0c\u4f7f\u5176\u53ef\u4ee5\u5728\u4f60\u7684Python\u73af\u5883\u4e2d\u4f7f\u7528\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py install<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u5c06\u6269\u5c55\u5e93\u5b89\u88c5\u5230\u4f60\u7684Python\u73af\u5883\u4e2d\uff0c\u4f7f\u5176\u53ef\u4ee5\u901a\u8fc7<code>import<\/code>\u8bed\u53e5\u8fdb\u884c\u5bfc\u5165\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6d4b\u8bd5\u548c\u53d1\u5e03\u6269\u5c55\u5e93<\/h3>\n<\/p>\n<p><h4>1. \u6d4b\u8bd5\u6269\u5c55\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u53d1\u5e03\u6269\u5c55\u5e93\u4e4b\u524d\uff0c\u786e\u4fdd\u5bf9\u5176\u8fdb\u884c\u5168\u9762\u7684\u6d4b\u8bd5\u3002\u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u811a\u672c\uff0c\u9a8c\u8bc1\u6269\u5c55\u5e93\u7684\u6240\u6709\u529f\u80fd\u662f\u5426\u6b63\u5e38\u5de5\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># test_mymodule.py<\/p>\n<p>import mymodule<\/p>\n<p>def test_say_hello_to():<\/p>\n<p>    mymodule.say_hello_to(&quot;World&quot;)<\/p>\n<p>    print(&quot;Test passed&quot;)<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    test_say_hello_to()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u6d4b\u8bd5\u811a\u672c\uff0c\u786e\u4fdd\u6ca1\u6709\u9519\u8bef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python test_mymodule.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u53d1\u5e03\u6269\u5c55\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u6d4b\u8bd5\u901a\u8fc7\u540e\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u53d1\u5e03\u6269\u5c55\u5e93\uff0c\u4f7f\u5176\u4ed6\u7528\u6237\u4e5f\u80fd\u4f7f\u7528\u3002\u5e38\u7528\u7684\u53d1\u5e03\u5e73\u53f0\u662fPython Package Index\uff08PyPI\uff09\u3002\u4ee5\u4e0b\u662f\u53d1\u5e03\u6269\u5c55\u5e93\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u5305\u542b\u6269\u5c55\u5e93\u7684\u5143\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># setup.py<\/p>\n<p>from setuptools import setup, Extension<\/p>\n<p>mymodule = Extension(&#39;mymodule&#39;,<\/p>\n<p>                     sources=[&#39;mymodule.c&#39;])<\/p>\n<p>setup(<\/p>\n<p>    name=&#39;mymodule&#39;,<\/p>\n<p>    version=&#39;1.0&#39;,<\/p>\n<p>    description=&#39;A simple example Python extension module&#39;,<\/p>\n<p>    ext_modules=[mymodule],<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2a<code>README.md<\/code>\u6587\u4ef6\uff0c\u5305\u542b\u6269\u5c55\u5e93\u7684\u8bf4\u660e\u6587\u6863\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2a<code>MANIFEST.in<\/code>\u6587\u4ef6\uff0c\u5305\u542b\u9700\u8981\u5305\u542b\u5728\u53d1\u5e03\u5305\u4e2d\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code>include README.md<\/p>\n<p>include mymodule.c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u751f\u6210\u5206\u53d1\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py sdist bdist_wheel<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u5b89\u88c5<code>twine<\/code>\u5de5\u5177\uff0c\u7528\u4e8e\u4e0a\u4f20\u5206\u53d1\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install twine<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528<code>twine<\/code>\u4e0a\u4f20\u5206\u53d1\u5305\u5230PyPI\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">twine upload dist\/*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u6269\u5c55\u5e93\u5f00\u53d1\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>1. \u7f16\u5199\u6e05\u6670\u7684\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u6e05\u6670\u7684\u6587\u6863\u662f\u786e\u4fdd\u5176\u4ed6\u7528\u6237\u80fd\u591f\u6b63\u786e\u4f7f\u7528\u4f60\u7684\u6269\u5c55\u5e93\u7684\u5173\u952e\u3002\u6587\u6863\u5e94\u5305\u62ec\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u88c5\u6307\u5357<\/strong>\uff1a\u5982\u4f55\u5b89\u88c5\u6269\u5c55\u5e93\u3002<\/li>\n<li><strong>\u4f7f\u7528\u793a\u4f8b<\/strong>\uff1a\u5982\u4f55\u4f7f\u7528\u6269\u5c55\u5e93\u4e2d\u7684\u5404\u4e2a\u529f\u80fd\u3002<\/li>\n<li><strong>API\u53c2\u8003<\/strong>\uff1a\u8be6\u7ec6\u63cf\u8ff0\u6269\u5c55\u5e93\u4e2d\u6bcf\u4e2a\u51fd\u6570\u548c\u7c7b\u7684\u7528\u6cd5\u3002<\/li>\n<\/ul>\n<p><h4>2. \u7f16\u5199\u5168\u9762\u7684\u6d4b\u8bd5\u7528\u4f8b<\/h4>\n<\/p>\n<p><p>\u7f16\u5199\u5168\u9762\u7684\u6d4b\u8bd5\u7528\u4f8b\uff0c\u786e\u4fdd\u6269\u5c55\u5e93\u7684\u6bcf\u4e2a\u529f\u80fd\u90fd\u7ecf\u8fc7\u6d4b\u8bd5\u3002\u4f7f\u7528\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\uff08\u5982<code>unittest<\/code>\u6216<code>pytest<\/code>\uff09\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\uff0c\u5e76\u5728\u53d1\u5e03\u524d\u8fd0\u884c\u6240\u6709\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><h4>3. \u9075\u5faa\u4ee3\u7801\u98ce\u683c\u89c4\u8303<\/h4>\n<\/p>\n<p><p>\u9075\u5faa\u4ee3\u7801\u98ce\u683c\u89c4\u8303\uff08\u5982PEP 8\uff09\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u4f7f\u7528\u4ee3\u7801\u68c0\u67e5\u5de5\u5177\uff08\u5982<code>pylint<\/code>\u6216<code>flake8<\/code>\uff09\u68c0\u67e5\u4ee3\u7801\u98ce\u683c\u3002<\/p>\n<\/p>\n<p><h4>4. \u4f18\u5316\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u4f18\u5316\u6269\u5c55\u5e93\u7684\u6027\u80fd\uff0c\u786e\u4fdd\u5176\u9ad8\u6548\u8fd0\u884c\u3002\u4f7f\u7528\u6027\u80fd\u5206\u6790\u5de5\u5177\uff08\u5982<code>cProfile<\/code>\u6216<code>gprof<\/code>\uff09\u5206\u6790\u4ee3\u7801\u6027\u80fd\uff0c\u5e76\u4f18\u5316\u5173\u952e\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u4f8b\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5f00\u53d1Python\u6269\u5c55\u5e93\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u4e00\u4e2a\u5b9e\u4f8b\u6765\u8be6\u7ec6\u8bf4\u660e\u3002\u5047\u8bbe\u6211\u4eec\u8981\u5f00\u53d1\u4e00\u4e2a\u8ba1\u7b97\u4e24\u4e2a\u6570\u4e4b\u548c\u7684\u6269\u5c55\u5e93\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528Cython\u7f16\u5199\u6269\u5c55\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2aCython\u6587\u4ef6\uff08\u4f8b\u5982<code>mymodule.pyx<\/code>\uff09\uff0c\u7f16\u5199\u6211\u4eec\u7684Cython\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mymodule.pyx<\/p>\n<p>def add_numbers(int a, int b):<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u7528\u4e8e\u7f16\u8bd1\u6211\u4eec\u7684Cython\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># setup.py<\/p>\n<p>from setuptools import setup<\/p>\n<p>from Cython.Build import cythonize<\/p>\n<p>setup(<\/p>\n<p>    ext_modules = cythonize(&quot;mymodule.pyx&quot;)<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7f16\u8bd1Cython\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py build_ext --inplace<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u6211\u4eec\u7684\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule<\/p>\n<p>print(mymodule.add_numbers(3, 5))  # \u8f93\u51fa\uff1a8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528SWIG\u7f16\u5199\u6269\u5c55\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aC\u6587\u4ef6\uff08\u4f8b\u5982<code>mymodule.c<\/code>\uff09\uff0c\u7f16\u5199\u6211\u4eec\u7684C\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/* mymodule.c *\/<\/p>\n<p>int add_numbers(int a, int b) {<\/p>\n<p>    return a + b;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aSWIG\u63a5\u53e3\u6587\u4ef6\uff08\u4f8b\u5982<code>mymodule.i<\/code>\uff09\uff0c\u63cf\u8ff0\u6211\u4eec\u7684C\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-swig\">\/* mymodule.i *\/<\/p>\n<p>%module mymodule<\/p>\n<p>%{<\/p>\n<p>#include &quot;mymodule.c&quot;<\/p>\n<p>%}<\/p>\n<p>int add_numbers(int a, int b);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u7528\u4e8e\u7f16\u8bd1\u6211\u4eec\u7684SWIG\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># setup.py<\/p>\n<p>from setuptools import setup, Extension<\/p>\n<p>mymodule = Extension(&#39;_mymodule&#39;,<\/p>\n<p>                     sources=[&#39;mymodule_wrap.c&#39;, &#39;mymodule.c&#39;])<\/p>\n<p>setup(<\/p>\n<p>    name=&#39;mymodule&#39;,<\/p>\n<p>    ext_modules=[mymodule],<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884cSWIG\u751f\u6210\u5305\u88c5\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">swig -python mymodule.i<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7f16\u8bd1\u6211\u4eec\u7684\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py build_ext --inplace<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u6211\u4eec\u7684\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule<\/p>\n<p>print(mymodule.add_numbers(3, 5))  # \u8f93\u51fa\uff1a8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528Boost.Python\u7f16\u5199\u6269\u5c55\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aC++\u6587\u4ef6\uff08\u4f8b\u5982<code>mymodule.cpp<\/code>\uff09\uff0c\u7f16\u5199\u6211\u4eec\u7684C++\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">\/* mymodule.cpp *\/<\/p>\n<p>#include &lt;boost\/python.hpp&gt;<\/p>\n<p>int add_numbers(int a, int b) {<\/p>\n<p>    return a + b;<\/p>\n<p>}<\/p>\n<p>BOOST_PYTHON_MODULE(mymodule) {<\/p>\n<p>    using namespace boost::python;<\/p>\n<p>    def(&quot;add_numbers&quot;, add_numbers);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a<code>setup.py<\/code>\u6587\u4ef6\uff0c\u7528\u4e8e\u7f16\u8bd1\u6211\u4eec\u7684Boost.Python\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># setup.py<\/p>\n<p>from setuptools import setup, Extension<\/p>\n<p>mymodule = Extension(&#39;mymodule&#39;,<\/p>\n<p>                     sources=[&#39;mymodule.cpp&#39;],<\/p>\n<p>                     libraries=[&#39;boost_python&#39;])<\/p>\n<p>setup(<\/p>\n<p>    name=&#39;mymodule&#39;,<\/p>\n<p>    ext_modules=[mymodule],<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7f16\u8bd1\u6211\u4eec\u7684\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py build_ext --inplace<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u6211\u4eec\u7684\u6269\u5c55\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule<\/p>\n<p>print(mymodule.add_numbers(3, 5))  # \u8f93\u51fa\uff1a8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5f00\u53d1Python\u6269\u5c55\u5e93\u662f\u63d0\u5347Python\u7a0b\u5e8f\u6027\u80fd\u7684\u4e00\u79cd\u6709\u6548\u65b9\u6cd5\u3002\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff08\u5982Cython\u3001SWIG\u6216Boost.Python\uff09\uff0c\u7f16\u5199\u6269\u5c55\u4ee3\u7801\uff0c\u7f16\u8bd1\u548c\u5b89\u88c5\u6269\u5c55\u5e93\uff0c\u5e76\u8fdb\u884c\u6d4b\u8bd5\u548c\u53d1\u5e03\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u9ad8\u6548\u3001\u53ef\u9760\u7684Python\u6269\u5c55\u5e93\u3002\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\uff0c\u5982\u7f16\u5199\u6e05\u6670\u7684\u6587\u6863\u3001\u5168\u9762\u7684\u6d4b\u8bd5\u7528\u4f8b\u3001\u9075\u5faa\u4ee3\u7801\u98ce\u683c\u89c4\u8303\u548c\u4f18\u5316\u6027\u80fd\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u6269\u5c55\u5e93\u7684\u8d28\u91cf\u548c\u7528\u6237\u4f53\u9a8c\u3002\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u5982\u4f55\u5f00\u53d1\u4e00\u4e2aPython\u6269\u5c55\u5e93\u7684\u57fa\u672c\u6d41\u7a0b\u548c\u6280\u5de7\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5bf9\u4f60\u5728\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u6846\u67b6\u6765\u5f00\u53d1Python\u6269\u5c55\u5e93\uff1f<\/strong><br \/>\u5728\u5f00\u53d1Python\u6269\u5c55\u5e93\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u6846\u67b6\u81f3\u5173\u91cd\u8981\u3002\u5e38\u7528\u7684\u5de5\u5177\u5305\u62ecCython\u3001SWIG\u548cctypes\u3002Cython\u80fd\u591f\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u4e3aC\uff0c\u63d0\u4f9b\u66f4\u9ad8\u7684\u6027\u80fd\u3002SWIG\u5219\u9002\u7528\u4e8e\u5c06C\/C++\u4ee3\u7801\u4e0e\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u7ed1\u5b9a\uff0c\u800cctypes\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528DLL\u6216\u5171\u4eab\u5e93\u3002\u9009\u62e9\u65f6\u5e94\u8003\u8651\u9879\u76ee\u9700\u6c42\u3001\u6027\u80fd\u8981\u6c42\u4ee5\u53ca\u56e2\u961f\u7684\u6280\u672f\u6808\u3002<\/p>\n<p><strong>\u5728\u5f00\u53d1Python\u6269\u5c55\u5e93\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5185\u5b58\u7ba1\u7406\u548c\u6027\u80fd\u4f18\u5316\uff1f<\/strong><br \/>\u5185\u5b58\u7ba1\u7406\u662f\u5f00\u53d1Python\u6269\u5c55\u5e93\u65f6\u7684\u4e00\u9879\u91cd\u8981\u4efb\u52a1\u3002\u4f7f\u7528C\/C++\u65f6\uff0c\u5f00\u53d1\u8005\u9700\u8981\u624b\u52a8\u7ba1\u7406\u5185\u5b58\uff0c\u786e\u4fdd\u5206\u914d\u548c\u91ca\u653e\u5185\u5b58\u7684\u6b63\u786e\u6027\u3002\u4f7f\u7528\u667a\u80fd\u6307\u9488\u7b49\u6280\u672f\u53ef\u4ee5\u5e2e\u52a9\u51cf\u5c11\u5185\u5b58\u6cc4\u6f0f\u7684\u98ce\u9669\u3002\u540c\u65f6\uff0c\u901a\u8fc7\u5206\u6790\u4ee3\u7801\u7684\u6027\u80fd\u74f6\u9888\uff0c\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982Valgrind\u548cgprof\u8fdb\u884c\u6027\u80fd\u4f18\u5316\uff0c\u786e\u4fdd\u6269\u5c55\u5e93\u5728\u6548\u7387\u4e0a\u8fbe\u5230\u6700\u4f73\u72b6\u6001\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u6269\u5c55\u5e93\u4e2d\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u548c\u8c03\u8bd5\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u6269\u5c55\u5e93\u7684\u8d28\u91cf\uff0c\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u548c\u8c03\u8bd5\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684unittest\u6846\u67b6\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\uff0c\u540c\u65f6\u7ed3\u5408pytest\u7b49\u5de5\u5177\u8fdb\u884c\u6d4b\u8bd5\u8986\u76d6\u7387\u7684\u5206\u6790\u3002\u8c03\u8bd5\u65b9\u9762\uff0c\u53ef\u4ee5\u5229\u7528gdb\u7b49\u8c03\u8bd5\u5de5\u5177\u5bf9C\/C++\u90e8\u5206\u8fdb\u884c\u8c03\u8bd5\u3002\u540c\u65f6\uff0c\u901a\u8fc7Python\u7684logging\u6a21\u5757\u53ef\u4ee5\u8bb0\u5f55\u8fd0\u884c\u65f6\u4fe1\u606f\uff0c\u5e2e\u52a9\u5206\u6790\u95ee\u9898\u3002\u786e\u4fdd\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4fdd\u6301\u826f\u597d\u7684\u6d4b\u8bd5\u4e60\u60ef\uff0c\u5c06\u6709\u52a9\u4e8e\u63d0\u5347\u6269\u5c55\u5e93\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u53d1Python\u6269\u5c55\u5e93\u9700\u8981\u9075\u5faa\u4e00\u5b9a\u7684\u6b65\u9aa4\u548c\u6d41\u7a0b\uff0c\u5176\u4e2d\u5305\u62ec\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u3001\u7f16\u5199\u6269\u5c55\u4ee3\u7801\u3001\u7f16\u8bd1\u548c\u5b89\u88c5\u6269\u5c55\u5e93\u3001\u6d4b\u8bd5\u548c [&hellip;]","protected":false},"author":3,"featured_media":1154600,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154595"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1154595"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154595\/revisions"}],"predecessor-version":[{"id":1154603,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154595\/revisions\/1154603"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1154600"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1154595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1154595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1154595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}