{"id":1159009,"date":"2025-01-13T18:47:34","date_gmt":"2025-01-13T10:47:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1159009.html"},"modified":"2025-01-13T18:47:37","modified_gmt":"2025-01-13T10:47:37","slug":"%e5%a6%82%e4%bd%95%e7%94%a8ubuntu%e8%b7%91python%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1159009.html","title":{"rendered":"\u5982\u4f55\u7528ubuntu\u8dd1python\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25200834\/d314e570-f840-44b7-8b41-992a7192d4bb.webp\" alt=\"\u5982\u4f55\u7528ubuntu\u8dd1python\u4ee3\u7801\" \/><\/p>\n<p><p> \u8981\u5728Ubuntu\u4e0a\u8fd0\u884cPython\u4ee3\u7801\uff0c<strong>\u4f60\u9700\u8981\u5b89\u88c5Python\u3001\u914d\u7f6ePython\u73af\u5883\u3001\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u7f16\u5199\u4ee3\u7801\u3001\u5e76\u4f7f\u7528\u7ec8\u7aef\u8fd0\u884c\u811a\u672c<\/strong>\u3002\u5176\u4e2d\uff0c\u5b89\u88c5Python\u662f\u6700\u57fa\u7840\u7684\u4e00\u6b65\uff0c\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e00\u6b65\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5Python<\/h3>\n<\/p>\n<p><p>Ubuntu\u901a\u5e38\u9884\u88c5\u4e86Python\uff0c\u4f46\u53ef\u80fd\u4e0d\u662f\u6700\u65b0\u7248\u672c\u3002\u8981\u5b89\u88c5\u6700\u65b0\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u66f4\u65b0Ubuntu\u5305\u7ba1\u7406\u5668<\/strong>\uff1a\u9996\u5148\u66f4\u65b0\u5305\u7ba1\u7406\u5668\uff0c\u4ee5\u786e\u4fdd\u4f60\u53ef\u4ee5\u83b7\u53d6\u5230\u6700\u65b0\u7684\u8f6f\u4ef6\u5305\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get update<\/p>\n<p>sudo apt-get upgrade<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5Python<\/strong>\uff1a\u4f7f\u7528<code>apt-get<\/code>\u547d\u4ee4\u5b89\u88c5Python\u3002\u8fd9\u91cc\u4ee5\u5b89\u88c5Python 3\u4e3a\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install python3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9a8c\u8bc1\u5b89\u88c5<\/strong>\uff1a\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u9a8c\u8bc1\u662f\u5426\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u914d\u7f6ePython\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u914d\u7f6ePython\u73af\u5883\u662f\u786e\u4fddPython\u8fd0\u884c\u987a\u5229\u7684\u91cd\u8981\u4e00\u6b65\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u8fd9\u5305\u62ec\u5b89\u88c5pip\uff08Python\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff09\u3001\u521b\u5efa\u865a\u62df\u73af\u5883\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5pip<\/h4>\n<\/p>\n<p><p><strong>pip<\/strong> \u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u7684\u8f6f\u4ef6\u5305\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install python3-pip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p><strong>\u865a\u62df\u73af\u5883<\/strong> \u662fPython\u4e2d\u7528\u4e8e\u9694\u79bb\u9879\u76ee\u4f9d\u8d56\u7684\u5de5\u5177\u3002\u53ef\u4ee5\u4f7f\u7528<code>venv<\/code>\u6a21\u5757\u6765\u521b\u5efa\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 -m venv myenv<\/p>\n<p>source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7f16\u5199Python\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u6587\u672c\u7f16\u8f91\u5668\u6765\u7f16\u5199Python\u4ee3\u7801\u3002\u8fd9\u91cc\u63a8\u8350\u4e00\u4e9b\u5e38\u7528\u7684\u7f16\u8f91\u5668\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Vim<\/strong>\uff1a\u4e00\u4e2a\u5f3a\u5927\u7684\u7ec8\u7aef\u6587\u672c\u7f16\u8f91\u5668\uff0c\u9002\u5408\u9ad8\u7ea7\u7528\u6237\u3002<\/li>\n<li><strong>VS Code<\/strong>\uff1a\u529f\u80fd\u5f3a\u5927\u7684\u4ee3\u7801\u7f16\u8f91\u5668\uff0c\u9002\u7528\u4e8e\u6240\u6709\u7528\u6237\u3002<\/li>\n<li><strong>PyCharm<\/strong>\uff1a\u4e00\u4e2a\u4e13\u4e1a\u7684Python IDE\uff0c\u9002\u5408\u5927\u578b\u9879\u76ee\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528<code>nano<\/code>\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">nano hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u7f16\u8f91\u5668\u4e2d\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4fdd\u5b58\u5e76\u9000\u51fa\uff08\u6309 <code>Ctrl+X<\/code> \u7136\u540e\u6309 <code>Y<\/code> \u786e\u8ba4\u4fdd\u5b58\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8fd0\u884cPython\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u8fd0\u884cPython\u4ee3\u7801\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Jupyter Notebook<\/h3>\n<\/p>\n<p><p>Jupyter Notebook\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u8ba1\u7b97\u73af\u5883\uff0c\u9002\u5408\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u9886\u57df\u3002\u5b89\u88c5Jupyter Notebook\u5e76\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install jupyter<\/p>\n<p>jupyter notebook<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u8c03\u8bd5Python\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u8c03\u8bd5\u4ee3\u7801\u662f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u4e00\u6b65\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u8c03\u8bd5\u5de5\u5177\uff0c\u5982<code>pdb<\/code>\u3001<code>ipdb<\/code>\u7b49\u3002\u4f60\u4e5f\u53ef\u4ee5\u5728VS Code\u6216PyCharm\u4e2d\u4f7f\u7528\u5185\u7f6e\u7684\u8c03\u8bd5\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7ba1\u7406Python\u5305<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528pip\u53ef\u4ee5\u8f7b\u677e\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\u3002\u5e38\u7528\u7684\u547d\u4ee4\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5b89\u88c5\u5305<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install package_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5217\u51fa\u5df2\u5b89\u88c5\u7684\u5305<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip list<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5378\u8f7d\u5305<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip uninstall package_name<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u516b\u3001\u7248\u672c\u63a7\u5236<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u7248\u672c\u63a7\u5236\u5de5\u5177\uff08\u5982Git\uff09\u6765\u7ba1\u7406\u4ee3\u7801\u66f4\u6539\u662f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u5b89\u88c5\u5e76\u4f7f\u7528Git\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install git<\/p>\n<p>git init<\/p>\n<p>git add .<\/p>\n<p>git commit -m &quot;Initial commit&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u90e8\u7f72Python\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u5f00\u53d1\u540e\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5c06\u5e94\u7528\u90e8\u7f72\u5230\u670d\u52a1\u5668\u4e0a\u3002\u5e38\u7528\u7684\u90e8\u7f72\u5de5\u5177\u5305\u62ecDocker\u3001Ansible\u7b49\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\u5c06Python\u5e94\u7528\u5bb9\u5668\u5316\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaDockerfile<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-Dockerfile\">FROM python:3.8<\/p>\n<p>WORKDIR \/app<\/p>\n<p>COPY . .<\/p>\n<p>RUN pip install -r requirements.txt<\/p>\n<p>CMD [&quot;python&quot;, &quot;hello.py&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6784\u5efaDocker\u955c\u50cf<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">docker build -t my-python-app .<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884cDocker\u5bb9\u5668<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">docker run my-python-app<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5341\u3001\u5b66\u4e60\u8d44\u6e90<\/h3>\n<\/p>\n<p><p>\u5b66\u4e60Python\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u8d44\u6e90\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b98\u65b9\u6587\u6863<\/strong>\uff1aPython\u7684\u5b98\u65b9\u6587\u6863\u662f\u6700\u6743\u5a01\u7684\u5b66\u4e60\u8d44\u6e90\u3002<\/li>\n<li><strong>\u5728\u7ebf\u8bfe\u7a0b<\/strong>\uff1a\u5982Coursera\u3001edX\u7b49\u5e73\u53f0\u4e0a\u7684Python\u8bfe\u7a0b\u3002<\/li>\n<li><strong>\u4e66\u7c4d<\/strong>\uff1a\u5982\u300aLearning Python\u300b\u3001\u300aPython Cookbook\u300b\u7b49\u3002<\/li>\n<\/ul>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Ubuntu\u4e0a\u8fd0\u884cPython\u4ee3\u7801\u5e76\u4e0d\u590d\u6742\uff0c\u4f46\u9700\u8981\u7ecf\u8fc7\u4e00\u4e9b\u914d\u7f6e\u548c\u8bbe\u7f6e\u3002<strong>\u5b89\u88c5Python\u3001\u914d\u7f6e\u73af\u5883\u3001\u7f16\u5199\u4ee3\u7801\u3001\u8fd0\u884c\u811a\u672c<\/strong>\u662f\u57fa\u672c\u6d41\u7a0b\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u4f60\u53ef\u4ee5\u638c\u63e1\u8fd9\u4e9b\u6280\u80fd\uff0c\u5e76\u5728\u4e0d\u540c\u7684\u9879\u76ee\u4e2d\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Ubuntu\u4e0a\u5982\u4f55\u5b89\u88c5Python\u73af\u5883\u4ee5\u4fbf\u8fd0\u884c\u4ee3\u7801\uff1f<\/strong><br \/>\u5728Ubuntu\u4e0a\uff0cPython\u901a\u5e38\u9884\u88c5\u5728\u7cfb\u7edf\u4e2d\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u6253\u5f00\u7ec8\u7aef\u5e76\u8f93\u5165<code>python3 --version<\/code>\u6765\u68c0\u67e5Python\u7248\u672c\u3002\u5982\u679c\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4<code>sudo apt-get install python3<\/code>\u6765\u5b89\u88c5\u6700\u65b0\u7248\u672c\u3002\u6b64\u5916\uff0c\u5b89\u88c5<code>pip<\/code>\uff08Python\u5305\u7ba1\u7406\u5de5\u5177\uff09\u53ef\u4ee5\u901a\u8fc7<code>sudo apt-get install python3-pip<\/code>\u5b8c\u6210\uff0c\u4ee5\u4fbf\u8f7b\u677e\u7ba1\u7406Python\u5e93\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Ubuntu\u4e2d\u8fd0\u884cPython\u811a\u672c\uff1f<\/strong><br \/>\u8981\u5728Ubuntu\u4e2d\u8fd0\u884cPython\u811a\u672c\uff0c\u60a8\u9700\u8981\u5148\u521b\u5efa\u4e00\u4e2aPython\u6587\u4ef6\uff0c\u4f8b\u5982<code>script.py<\/code>\u3002\u5728\u7ec8\u7aef\u4e2d\uff0c\u5bfc\u822a\u5230\u8be5\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff0c\u4f7f\u7528\u547d\u4ee4<code>python3 script.py<\/code>\u6765\u6267\u884c\u4ee3\u7801\u3002\u5982\u679c\u60a8\u7684\u4ee3\u7801\u9700\u8981\u7279\u5b9a\u7684\u6743\u9650\uff0c\u786e\u4fdd\u4f7f\u7528<code>chmod +x script.py<\/code>\u547d\u4ee4\u4f7f\u5176\u53ef\u6267\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Ubuntu\u4e0a\u4f7f\u7528\u865a\u62df\u73af\u5883\u7ba1\u7406Python\u9879\u76ee\uff1f<\/strong><br \/>\u4f7f\u7528\u865a\u62df\u73af\u5883\u53ef\u4ee5\u5e2e\u52a9\u60a8\u4e3a\u4e0d\u540c\u7684<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/23316.html\" target=\"_blank\">\u9879\u76ee\u7ba1\u7406<\/a>\u4f9d\u8d56\u9879\u3002\u5728Ubuntu\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>sudo apt-get install python3-venv<\/code>\u6765\u5b89\u88c5\u865a\u62df\u73af\u5883\u652f\u6301\u3002\u521b\u5efa\u865a\u62df\u73af\u5883\u65f6\uff0c\u4f7f\u7528\u547d\u4ee4<code>python3 -m venv myenv<\/code>\u3002\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u53ef\u4ee5\u901a\u8fc7<code>source myenv\/bin\/activate<\/code>\u5b9e\u73b0\u3002\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u5728\u6bcf\u4e2a\u9879\u76ee\u4e2d\u72ec\u7acb\u7ba1\u7406\u5305\u548c\u4f9d\u8d56\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Ubuntu\u4e0a\u8fd0\u884cPython\u4ee3\u7801\uff0c\u4f60\u9700\u8981\u5b89\u88c5Python\u3001\u914d\u7f6ePython\u73af\u5883\u3001\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u7f16\u5199\u4ee3\u7801\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1159016,"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\/1159009"}],"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=1159009"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159009\/revisions"}],"predecessor-version":[{"id":1159017,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159009\/revisions\/1159017"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1159016"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1159009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1159009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1159009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}