{"id":949292,"date":"2024-12-27T00:11:27","date_gmt":"2024-12-26T16:11:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/949292.html"},"modified":"2024-12-27T00:11:30","modified_gmt":"2024-12-26T16:11:30","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%89%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/949292.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u6253"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084335\/d9921819-5437-4cf5-ad7f-e9c247d7bf2c.webp\" alt=\"python\u4e2d\u5982\u4f55\u6253\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5b9e\u73b0\u6253\u5305\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5e38\u89c1\u7684\u5305\u62ec<strong>\u4f7f\u7528<code>setuptools<\/code>\u521b\u5efaPython\u5305\u3001\u4f7f\u7528<code>PyInstaller<\/code>\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\u3001\u4f7f\u7528<code>cx_Freeze<\/code>\u6253\u5305\u5e94\u7528\u3001\u4f7f\u7528<code>zipapp<\/code>\u521b\u5efa\u53ef\u6267\u884c\u7684ZIP\u6587\u4ef6<\/strong>\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528<code>setuptools<\/code>\u521b\u5efaPython\u5305\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>setuptools<\/code>\u521b\u5efaPython\u5305\u662f\u6700\u5e38\u89c1\u4e14\u5e7f\u6cdb\u4f7f\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u9879\u76ee\u6587\u4ef6\u7ed3\u6784\u6b63\u786e\u3002\u901a\u5e38\uff0c\u4e00\u4e2aPython\u5305\u7684\u57fa\u672c\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>my_package\/<\/p>\n<p>\u2502<\/p>\n<p>\u251c\u2500\u2500 my_package\/<\/p>\n<p>\u2502   \u251c\u2500\u2500 __init__.py<\/p>\n<p>\u2502   \u251c\u2500\u2500 module1.py<\/p>\n<p>\u2502   \u2514\u2500\u2500 module2.py<\/p>\n<p>\u2502<\/p>\n<p>\u251c\u2500\u2500 setup.py<\/p>\n<p>\u2514\u2500\u2500 README.md<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7ed3\u6784\u4e2d\uff0c<code>my_package<\/code>\u662f\u5305\u7684\u540d\u5b57\uff0c<code>__init__.py<\/code>\u662f\u4e00\u4e2a\u7a7a\u6587\u4ef6\uff0c\u8868\u793a\u8be5\u76ee\u5f55\u662f\u4e00\u4e2aPython\u5305\u3002<code>setup.py<\/code>\u662f\u6700\u91cd\u8981\u7684\u6587\u4ef6\uff0c\u5b83\u5305\u542b\u4e86\u5305\u7684\u5143\u6570\u636e\u548c\u5b89\u88c5\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>setuptools<\/code>\u521b\u5efaPython\u5305<\/h3>\n<\/p>\n<p><h4>1. \u521b\u5efa<code>setup.py<\/code>\u6587\u4ef6<\/h4>\n<\/p>\n<p><p><code>setup.py<\/code>\u6587\u4ef6\u662f\u5b9a\u4e49\u5305\u7684\u5143\u6570\u636e\u548c\u4f9d\u8d56\u9879\u7684\u811a\u672c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684<code>setup.py<\/code>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from setuptools import setup, find_packages<\/p>\n<p>setup(<\/p>\n<p>    name=&#39;my_package&#39;,<\/p>\n<p>    version=&#39;0.1&#39;,<\/p>\n<p>    packages=find_packages(),<\/p>\n<p>    install_requires=[<\/p>\n<p>        # \u5728\u6b64\u5904\u5217\u51fa\u9879\u76ee\u7684\u4f9d\u8d56\u9879\uff0c\u5982 &#39;requests&#39;, &#39;numpy&#39;,<\/p>\n<p>    ],<\/p>\n<p>    author=&#39;Your Name&#39;,<\/p>\n<p>    author_em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l=&#39;your.email@example.com&#39;,<\/p>\n<p>    description=&#39;A brief description of the package&#39;,<\/p>\n<p>    long_description=open(&#39;README.md&#39;).read(),<\/p>\n<p>    long_description_content_type=&#39;text\/markdown&#39;,<\/p>\n<p>    url=&#39;https:\/\/github.com\/yourusername\/my_package&#39;,<\/p>\n<p>    classifiers=[<\/p>\n<p>        &#39;Programming Language :: Python :: 3&#39;,<\/p>\n<p>        &#39;License :: OSI Approved :: MIT License&#39;,<\/p>\n<p>        &#39;Operating System :: OS Independent&#39;,<\/p>\n<p>    ],<\/p>\n<p>    python_requires=&#39;&gt;=3.6&#39;,<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6784\u5efa\u548c\u5206\u53d1\u5305<\/h4>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u597d<code>setup.py<\/code>\u6587\u4ef6\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>setuptools<\/code>\u6765\u6784\u5efa\u548c\u5206\u53d1\u4f60\u7684\u5305\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u5b89\u88c5<code>setuptools<\/code>\u548c<code>wheel<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install setuptools wheel<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u4ee5\u6784\u5efa\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<p><p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a<code>dist<\/code>\u76ee\u5f55\uff0c\u5176\u4e2d\u5305\u542b<code>.tar.gz<\/code>\u548c<code>.whl<\/code>\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>3. \u53d1\u5e03\u5230PyPI<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u60f3\u5c06\u4f60\u7684\u5305\u53d1\u5e03\u5230Python Package Index (PyPI)\uff0c\u4f60\u9700\u8981\u5b89\u88c5<code>twine<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install twine<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4e0a\u4f20\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">twine upload dist\/*<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u9700\u8981\u5728PyPI\u4e0a\u6709\u4e00\u4e2a\u8d26\u6237\uff0c\u5e76\u5728\u9996\u6b21\u4e0a\u4f20\u65f6\u8f93\u5165\u4f60\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>PyInstaller<\/code>\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>PyInstaller<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u6253\u5305\u6210\u72ec\u7acb\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5de5\u5177\u3002\u5b83\u652f\u6301\u591a\u79cd\u64cd\u4f5c\u7cfb\u7edf\uff0c\u5305\u62ecWindows\u3001macOS\u548cLinux\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5<code>PyInstaller<\/code><\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyinstaller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528<code>PyInstaller<\/code>\u6253\u5305<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u7684Python\u811a\u672c\u662f<code>main.py<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile main.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>--onefile<\/code>\u9009\u9879\u4f1a\u5c06\u6240\u6709\u6587\u4ef6\u6253\u5305\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002\u6253\u5305\u5b8c\u6210\u540e\uff0c\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4f1a\u4f4d\u4e8e<code>dist<\/code>\u76ee\u5f55\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>cx_Freeze<\/code>\u6253\u5305\u5e94\u7528<\/h3>\n<\/p>\n<p><p><code>cx_Freeze<\/code>\u4e5f\u662f\u4e00\u4e2a\u53ef\u4ee5\u5c06Python\u7a0b\u5e8f\u6253\u5305\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5de5\u5177\uff0c\u7279\u522b\u9002\u7528\u4e8eWindows\u5e73\u53f0\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5<code>cx_Freeze<\/code><\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install cx_Freeze<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa<code>setup.py<\/code>\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4e0e<code>setuptools<\/code>\u7684<code>setup.py<\/code>\u7c7b\u4f3c\uff0c\u4f46\u9700\u8981\u6307\u5b9a<code>executables<\/code>\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from cx_Freeze import setup, Executable<\/p>\n<p>setup(<\/p>\n<p>    name=&#39;my_application&#39;,<\/p>\n<p>    version=&#39;0.1&#39;,<\/p>\n<p>    description=&#39;My Application Description&#39;,<\/p>\n<p>    executables=[Executable(&#39;main.py&#39;)],<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6784\u5efa\u53ef\u6267\u884c\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python setup.py build<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4f1a\u4f4d\u4e8e<code>build<\/code>\u76ee\u5f55\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>zipapp<\/code>\u521b\u5efa\u53ef\u6267\u884c\u7684ZIP\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>zipapp<\/code>\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u7528\u4e8e\u5c06Python\u5e94\u7528\u6253\u5305\u6210\u53ef\u6267\u884c\u7684ZIP\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u53ef\u6267\u884c\u7684ZIP\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u7684Python\u9879\u76ee\u5728<code>my_project<\/code>\u76ee\u5f55\u4e0b\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m zipapp my_project -o my_project.pyz<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210\u7684<code>my_project.pyz<\/code>\u6587\u4ef6\u662f\u4e00\u4e2a\u53ef\u6267\u884c\u7684ZIP\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python my_project.pyz<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u5b9e\u73b0\u6253\u5305\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528<code>setuptools<\/code>\u521b\u5efaPython\u5305\u3001<code>PyInstaller<\/code>\u548c<code>cx_Freeze<\/code>\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4ee5\u53ca\u4f7f\u7528<code>zipapp<\/code>\u521b\u5efa\u53ef\u6267\u884c\u7684ZIP\u6587\u4ef6\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06Python\u4ee3\u7801\u6253\u5305\u6210\u53ef\u5206\u53d1\u7684\u5f62\u5f0f\uff0c\u65e0\u8bba\u662f\u4f9b\u5176\u4ed6\u5f00\u53d1\u8005\u4f7f\u7528\u7684\u5e93\uff0c\u8fd8\u662f\u6700\u7ec8\u7528\u6237\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u6253\u5b57\u7ec3\u4e60\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>input()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6253\u5b57\u7ec3\u4e60\u7a0b\u5e8f\u3002\u901a\u8fc7\u63d0\u793a\u7528\u6237\u8f93\u5165\u7279\u5b9a\u7684\u6587\u672c\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4ed6\u4eec\u63d0\u9ad8\u6253\u5b57\u901f\u5ea6\u548c\u51c6\u786e\u6027\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6a21\u5757\u5982<code>time<\/code>\u6765\u6d4b\u91cf\u6253\u5b57\u6240\u9700\u7684\u65f6\u95f4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u5206\u949f\u7684\u5b57\u6570\uff08WPM\uff09\u3002\u53ef\u4ee5\u8003\u8651\u8bbe\u8ba1\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u53e5\u5b50\u7684\u7a0b\u5e8f\uff0c\u4ee5\u4fbf\u7528\u6237\u53ef\u4ee5\u53cd\u590d\u7ec3\u4e60\u3002<\/p>\n<p><strong>Python\u662f\u5426\u6709\u73b0\u6210\u7684\u6253\u5b57\u6e38\u620f\u6216\u7ec3\u4e60\u7a0b\u5e8f\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u793e\u533a\u4e2d\u6709\u8bb8\u591a\u5f00\u6e90\u9879\u76ee\u548c\u5e93\u53ef\u4ee5\u7528\u4e8e\u6253\u5b57\u7ec3\u4e60\u548c\u6e38\u620f\u3002\u4f8b\u5982\uff0c<code>pyttsx3<\/code>\u5e93\u53ef\u4ee5\u7528\u4e8e\u8bed\u97f3\u5408\u6210\uff0c\u8ba9\u7a0b\u5e8f\u8bfb\u51fa\u6587\u672c\uff0c\u7528\u6237\u8fdb\u884c\u6253\u5b57\u3002\u53e6\u5916\uff0c<code>pygame<\/code>\u5e93\u53ef\u4ee5\u5e2e\u52a9\u60a8\u521b\u5efa\u66f4\u590d\u6742\u7684\u6253\u5b57\u6e38\u620f\uff0c\u6dfb\u52a0\u56fe\u5f62\u548c\u97f3\u6548\uff0c\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u5728Python\u4e2d\u5982\u4f55\u8bb0\u5f55\u6211\u7684\u6253\u5b57\u901f\u5ea6\u548c\u51c6\u786e\u6027\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\u6765\u8bb0\u5f55\u6253\u5b57\u901f\u5ea6\u548c\u51c6\u786e\u6027\u3002\u60a8\u53ef\u4ee5\u5728\u7528\u6237\u8f93\u5165\u5b8c\u6bd5\u540e\uff0c\u8ba1\u7b97\u8f93\u5165\u7684\u603b\u5b57\u7b26\u6570\u548c\u6b63\u786e\u5b57\u7b26\u6570\uff0c\u7136\u540e\u5229\u7528\u8fd9\u4e9b\u6570\u636e\u6765\u8ba1\u7b97\u6bcf\u5206\u949f\u7684\u5b57\u6570\uff08WPM\uff09\u548c\u51c6\u786e\u6027\u767e\u5206\u6bd4\u3002\u901a\u8fc7\u5c06\u8fd9\u4e9b\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\uff0c\u60a8\u53ef\u4ee5\u8ddf\u8e2a\u8fdb\u5ea6\u5e76\u8bbe\u5b9a\u76ee\u6807\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u6253\u5305\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528setuptools\u521b\u5efaPython\u5305\u3001\u4f7f\u7528PyInstal [&hellip;]","protected":false},"author":3,"featured_media":949298,"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\/949292"}],"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=949292"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/949292\/revisions"}],"predecessor-version":[{"id":949301,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/949292\/revisions\/949301"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/949298"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=949292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=949292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=949292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}