{"id":1123166,"date":"2025-01-08T19:29:41","date_gmt":"2025-01-08T11:29:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1123166.html"},"modified":"2025-01-08T19:29:43","modified_gmt":"2025-01-08T11:29:43","slug":"python%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9a%84%e5%8c%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1123166.html","title":{"rendered":"Python\u5982\u4f55\u6dfb\u52a0\u6570\u636e\u5e93\u7684\u5305"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084741\/1ce3a29b-9102-47ec-83cf-cf7d70afa846.webp\" alt=\"Python\u5982\u4f55\u6dfb\u52a0\u6570\u636e\u5e93\u7684\u5305\" \/><\/p>\n<p><p> <strong>Python\u6dfb\u52a0\u6570\u636e\u5e93\u5305\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528pip\u5b89\u88c5\u3001\u4f7f\u7528conda\u5b89\u88c5\u3001\u4ece\u6e90\u4ee3\u7801\u5b89\u88c5\u3001\u4f7f\u7528Docker\u5bb9\u5668\u3002<\/strong>\u5728\u5177\u4f53\u7684\u9879\u76ee\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5b89\u88c5\u65b9\u6cd5\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f\u865a\u62df\u73af\u5883\uff0c\u4f7f\u7528pip\u53ef\u80fd\u66f4\u4e3a\u65b9\u4fbf\u548c\u8f7b\u91cf\u3002\u5982\u679c\u4f60\u9700\u8981\u66f4\u591a\u7684\u5e93\u548c\u4f9d\u8d56\u7ba1\u7406\uff0cconda\u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528pip\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>pip\u662fPython\u5b98\u65b9\u63a8\u8350\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u6570\u636e\u5e93\u9a71\u52a8\u5305\u7684\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>1.1\u3001\u5b89\u88c5MySQL\u6570\u636e\u5e93\u7684\u5305<\/h4>\n<\/p>\n<p><p>\u8981\u5b89\u88c5MySQL\u6570\u636e\u5e93\u7684Python\u5305\uff0c\u901a\u5e38\u4f7f\u7528<code>mysql-connector-python<\/code>\u6216<code>PyMySQL<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mysql-connector-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyMySQL<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2\u3001\u5b89\u88c5PostgreSQL\u6570\u636e\u5e93\u7684\u5305<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8ePostgreSQL\uff0c\u4f7f\u7528<code>psycopg2<\/code>\u662f\u4e00\u4e2a\u5e38\u89c1\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install psycopg2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.3\u3001\u5b89\u88c5SQLite\u6570\u636e\u5e93\u7684\u5305<\/h4>\n<\/p>\n<p><p>SQLite\u6570\u636e\u5e93\u7684\u5305\u901a\u5e38\u5df2\u7ecf\u5305\u542b\u5728Python\u7684\u6807\u51c6\u5e93\u4e2d\uff0c\u4f60\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u9700\u8981\u6269\u5c55\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sqlite3<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528conda\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4f7f\u7528Anaconda\u6216Miniconda\u4f5c\u4e3a\u4f60\u7684Python\u53d1\u884c\u7248\uff0cconda\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7ba1\u7406\u5e93\u548c\u4f9d\u8d56\u3002<\/p>\n<\/p>\n<p><h4>2.1\u3001\u5b89\u88c5MySQL\u6570\u636e\u5e93\u7684\u5305<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">conda install -c anaconda mysql-connector-python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2\u3001\u5b89\u88c5PostgreSQL\u6570\u636e\u5e93\u7684\u5305<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">conda install -c anaconda psycopg2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4ece\u6e90\u4ee3\u7801\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u67d0\u4e9b\u5e93\u7684\u6700\u65b0\u7248\u672c\u6216\u7279\u5b9a\u529f\u80fd\u53ef\u80fd\u5c1a\u672a\u53d1\u5e03\u5230PyPI\u6216conda\u4ed3\u5e93\uff0c\u8fd9\u65f6\u4f60\u53ef\u4ee5\u4ece\u6e90\u4ee3\u7801\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>3.1\u3001\u514b\u9686\u4ed3\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u514b\u9686\u4f60\u6240\u9700\u8981\u7684\u5305\u7684GitHub\u4ed3\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">git clone https:\/\/github.com\/psycopg\/psycopg2.git<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2\u3001\u5b89\u88c5<\/h4>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u8fdb\u5165\u4ed3\u5e93\u76ee\u5f55\u5e76\u8fd0\u884c\u5b89\u88c5\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">cd psycopg2<\/p>\n<p>python setup.py install<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Docker\u5bb9\u5668<\/h3>\n<\/p>\n<p><p>Docker\u5bb9\u5668\u53ef\u4ee5\u5e2e\u52a9\u4f60\u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684\u3001\u53ef\u79fb\u690d\u7684\u5f00\u53d1\u73af\u5883\u3002\u4f60\u53ef\u4ee5\u5728Dockerfile\u4e2d\u6307\u5b9a\u9700\u8981\u7684\u6570\u636e\u5e93\u5305\u3002<\/p>\n<\/p>\n<p><h4>4.1\u3001\u521b\u5efaDockerfile<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>Dockerfile<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-dockerfile\">FROM python:3.9<\/p>\n<p>RUN pip install mysql-connector-python<\/p>\n<p>RUN pip install psycopg2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2\u3001\u6784\u5efa\u5bb9\u5668<\/h4>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6784\u5efa\u548c\u542f\u52a8\u5bb9\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">docker build -t my-python-app .<\/p>\n<p>docker run -it my-python-app<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u914d\u7f6e<\/h3>\n<\/p>\n<p><p>\u5927\u591a\u6570\u73b0\u4ee3IDE\uff0c\u5982PyCharm\u3001VSCode\u7b49\uff0c\u63d0\u4f9b\u4e86\u5305\u7ba1\u7406\u7684GUI\u754c\u9762\u3002\u4f60\u53ef\u4ee5\u76f4\u63a5\u5728IDE\u4e2d\u641c\u7d22\u5e76\u5b89\u88c5\u9700\u8981\u7684\u6570\u636e\u5e93\u5305\u3002<\/p>\n<\/p>\n<p><h4>5.1\u3001PyCharm<\/h4>\n<\/p>\n<p><p>\u5728PyCharm\u4e2d\uff0c\u6253\u5f00<code>File &gt; Settings &gt; Project: YourProject &gt; Python Interpreter<\/code>\uff0c\u7136\u540e\u70b9\u51fb\u52a0\u53f7\u6309\u94ae\u641c\u7d22\u5e76\u5b89\u88c5\u9700\u8981\u7684\u5305\u3002<\/p>\n<\/p>\n<p><h4>5.2\u3001VSCode<\/h4>\n<\/p>\n<p><p>\u5728VSCode\u4e2d\uff0c\u6253\u5f00\u547d\u4ee4\u9762\u677f\uff08Ctrl+Shift+P\uff09\uff0c\u8f93\u5165\u5e76\u9009\u62e9<code>Python: Select Interpreter<\/code>\uff0c\u7136\u540e\u641c\u7d22\u5e76\u5b89\u88c5\u5305\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5982\u4f55\u6dfb\u52a0\u6570\u636e\u5e93\u5305\u65f6\uff0c<strong>\u9700\u8981\u8003\u8651\u9879\u76ee\u7684\u9700\u6c42\u3001\u5f00\u53d1\u73af\u5883\u4ee5\u53ca\u4f9d\u8d56\u7ba1\u7406<\/strong>\u3002pip\u548cconda\u662f\u6700\u5e38\u89c1\u548c\u65b9\u4fbf\u7684\u65b9\u6cd5\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u4ece\u6e90\u4ee3\u7801\u5b89\u88c5\u6216\u4f7f\u7528Docker\u53ef\u80fd\u4f1a\u66f4\u9002\u5408\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u4e3a\u4f60\u7684Python\u9879\u76ee\u6dfb\u52a0\u6240\u9700\u7684\u6570\u636e\u5e93\u5305\uff0c\u4e3a\u6570\u636e\u5904\u7406\u548c\u5e94\u7528\u5f00\u53d1\u6253\u4e0b\u575a\u5b9e\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u9002\u5408\u6211\u7684\u9879\u76ee\u7684\u6570\u636e\u5e93\u5305\uff1f<\/strong><br \/>\u9009\u62e9\u6570\u636e\u5e93\u5305\u65f6\uff0c\u8003\u8651\u9879\u76ee\u7684\u9700\u6c42\u975e\u5e38\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u9700\u8981\u9ad8\u6027\u80fd\u7684\u6570\u636e\u5904\u7406\uff0c\u53ef\u4ee5\u9009\u62e9\u50cfSQLAlchemy\u6216Peewee\u8fd9\u6837\u7684ORM\u5305\u3002\u5982\u679c\u9879\u76ee\u9700\u8981\u8f7b\u91cf\u7ea7\u7684\u89e3\u51b3\u65b9\u6848\uff0cSQLite\u53ef\u80fd\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u786e\u4fdd\u67e5\u770b\u6bcf\u4e2a\u5305\u7684\u6587\u6863\u548c\u793e\u533a\u652f\u6301\u60c5\u51b5\uff0c\u4ee5\u627e\u5230\u6700\u9002\u5408\u4f60\u9700\u6c42\u7684\u9009\u9879\u3002<\/p>\n<p><strong>\u5b89\u88c5Python\u6570\u636e\u5e93\u5305\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u5b89\u88c5\u6570\u636e\u5e93\u5305\u65f6\uff0c\u4f7f\u7528\u865a\u62df\u73af\u5883\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002\u8fd9\u53ef\u4ee5\u907f\u514d\u5305\u4e4b\u95f4\u7684\u51b2\u7a81\u548c\u4f9d\u8d56\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528<code>venv<\/code>\u6216<code>virtualenv<\/code>\u6765\u521b\u5efa\u865a\u62df\u73af\u5883\u3002\u5b8c\u6210\u73af\u5883\u8bbe\u7f6e\u540e\uff0c\u901a\u8fc7<code>pip install package_name<\/code>\u547d\u4ee4\u5b89\u88c5\u6240\u9700\u7684\u6570\u636e\u5e93\u5305\uff0c\u786e\u4fdd\u5728\u5b89\u88c5\u524d\u68c0\u67e5\u5305\u7684\u7248\u672c\u548c\u517c\u5bb9\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8fde\u63a5\u5230\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u5e93\uff1f<\/strong><br \/>\u8fde\u63a5\u5230\u6570\u636e\u5e93\u7684\u65b9\u5f0f\u53d6\u51b3\u4e8e\u4f60\u4f7f\u7528\u7684\u6570\u636e\u5e93\u7c7b\u578b\u3002\u5bf9\u4e8e\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0c\u5982MySQL\u6216PostgreSQL\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5e94\u7684\u5e93\uff08\u4f8b\u5982<code>mysql-connector-python<\/code>\u6216<code>psycopg2<\/code>\uff09\u3002\u5bf9\u4e8eNoSQL\u6570\u636e\u5e93\uff0c\u5982MongoDB\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pymongo<\/code>\u5e93\u3002\u5728\u8fde\u63a5\u65f6\uff0c\u9700\u8981\u63d0\u4f9b\u6570\u636e\u5e93\u7684\u5730\u5740\u3001\u7528\u6237\u540d\u548c\u5bc6\u7801\u7b49\u4fe1\u606f\uff0c\u5e76\u786e\u4fdd\u5904\u7406\u8fde\u63a5\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6dfb\u52a0\u6570\u636e\u5e93\u5305\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528pip\u5b89\u88c5\u3001\u4f7f\u7528conda\u5b89\u88c5\u3001\u4ece\u6e90\u4ee3\u7801\u5b89\u88c5\u3001\u4f7f\u7528Docker\u5bb9\u5668\u3002\u5728\u5177 [&hellip;]","protected":false},"author":3,"featured_media":1123175,"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\/1123166"}],"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=1123166"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1123166\/revisions"}],"predecessor-version":[{"id":1123177,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1123166\/revisions\/1123177"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1123175"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1123166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1123166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1123166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}