{"id":933220,"date":"2024-12-26T18:12:23","date_gmt":"2024-12-26T10:12:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/933220.html"},"modified":"2024-12-26T18:12:25","modified_gmt":"2024-12-26T10:12:25","slug":"python-%e5%a6%82%e4%bd%95%e5%bc%95%e5%8c%85","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/933220.html","title":{"rendered":"python \u5982\u4f55\u5f15\u5305"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070736\/0984397d-bef5-4042-99e0-754658a4140a.webp\" alt=\"python \u5982\u4f55\u5f15\u5305\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5f15\u5165\u5305\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5f15\u5165\u6574\u4e2a\u6a21\u5757\u3001\u4f7f\u7528<code>from ... import<\/code>\u8bed\u53e5\u5f15\u5165\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u90e8\u5206\u3001\u4f7f\u7528<code>as<\/code>\u5173\u952e\u5b57\u4e3a\u5f15\u5165\u7684\u6a21\u5757\u6216\u90e8\u5206\u63d0\u4f9b\u4e00\u4e2a\u522b\u540d\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u8ba9\u4f60\u8bbf\u95ee\u6574\u4e2a\u6a21\u5757\u7684\u6240\u6709\u529f\u80fd\uff1b\u800c<code>from ... import<\/code>\u8bed\u53e5\u5219\u5141\u8bb8\u4f60\u53ea\u5f15\u5165\u6a21\u5757\u4e2d\u7684\u7279\u5b9a\u90e8\u5206\uff0c\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u51cf\u5c11\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u5197\u4f59\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u5f15\u5165\u5305\u7684\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>import<\/code>\u5f15\u5165\u6574\u4e2a\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u53ef\u4ee5\u5f15\u5165\u6574\u4e2a\u6a21\u5757\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u4f60\u8bbf\u95ee\u6a21\u5757\u4e2d\u7684\u6240\u6709\u529f\u80fd\u3002\u901a\u5e38\u7528\u4e8e\u5f15\u5165\u8f83\u5c0f\u7684\u6a21\u5757\u6216\u8005\u5728\u9700\u8981\u4f7f\u7528\u6a21\u5757\u5927\u90e8\u5206\u529f\u80fd\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570<\/strong><\/h2>\n<p>result = math.sqrt(16)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u5f15\u5165\u7684\u6a21\u5757\u540d\u9700\u8981\u5728\u8c03\u7528\u65f6\u52a0\u4e0a\u6a21\u5757\u540d\u4f5c\u4e3a\u524d\u7f00\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>from ... import<\/code>\u5f15\u5165\u6a21\u5757\u4e2d\u7684\u90e8\u5206<\/p>\n<\/p>\n<p><p>\u5f53\u4f60\u53ea\u9700\u8981\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u90e8\u5206\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>from ... import<\/code>\u8bed\u53e5\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u7b80\u5316\u4ee3\u7801\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from math import sqrt<\/p>\n<h2><strong>\u76f4\u63a5\u4f7f\u7528\u5bfc\u5165\u7684\u51fd\u6570<\/strong><\/h2>\n<p>result = sqrt(16)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u76f4\u63a5\u5f15\u5165\u4e86<code>sqrt<\/code>\u51fd\u6570\uff0c\u65e0\u9700\u518d\u901a\u8fc7\u6a21\u5757\u540d\u8fdb\u884c\u8c03\u7528\u3002\u4f46\u8981\u6ce8\u610f\uff0c\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\uff0c\u5982\u679c\u591a\u4e2a\u6a21\u5757\u4e2d\u6709\u540c\u540d\u7684\u51fd\u6570\u6216\u53d8\u91cf\uff0c\u5c31\u8981\u683c\u5916\u5c0f\u5fc3\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>as<\/code>\u5173\u952e\u5b57\u4e3a\u5f15\u5165\u7684\u6a21\u5757\u6216\u90e8\u5206\u63d0\u4f9b\u522b\u540d<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6a21\u5757\u540d\u53ef\u80fd\u4f1a\u6bd4\u8f83\u957f\uff0c\u6216\u8005\u4e0e\u73b0\u6709\u7684\u53d8\u91cf\u540d\u51b2\u7a81\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>as<\/code>\u5173\u952e\u5b57\u4e3a\u5f15\u5165\u7684\u6a21\u5757\u6216\u90e8\u5206\u63d0\u4f9b\u4e00\u4e2a\u522b\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528\u522b\u540d\u8c03\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570<\/strong><\/h2>\n<p>array = np.array([1, 2, 3])<\/p>\n<p>print(array)  # \u8f93\u51fa\uff1a[1 2 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>as<\/code>\u5173\u952e\u5b57\u4e0d\u4ec5\u53ef\u4ee5\u5e2e\u52a9\u7b80\u5316\u4ee3\u7801\uff0c\u8fd8\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5f15\u5165\u81ea\u5b9a\u4e49\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u5141\u8bb8\u7528\u6237\u521b\u5efa\u81ea\u5df1\u7684\u6a21\u5757\uff0c\u5e76\u5728\u5176\u4ed6\u811a\u672c\u4e2d\u8fdb\u884c\u5f15\u5165\u3002\u8981\u5f15\u5165\u81ea\u5b9a\u4e49\u6a21\u5757\uff0c\u9700\u8981\u786e\u4fdd\u6a21\u5757\u6587\u4ef6\u4e0e\u811a\u672c\u5728\u540c\u4e00\u4e2a\u76ee\u5f55\u4e0b\uff0c\u6216\u8005\u6a21\u5757\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\u5728<code>sys.path<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6709\u4e00\u4e2a\u540d\u4e3amy_module.py\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u51fd\u6570greet<\/p>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<h2><strong>\u5728\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\u5f15\u5165\u81ea\u5b9a\u4e49\u6a21\u5757<\/strong><\/h2>\n<p>import my_module<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u6a21\u5757\u4e2d\u7684\u51fd\u6570<\/strong><\/h2>\n<p>print(my_module.greet(&quot;Alice&quot;))  # \u8f93\u51fa\uff1aHello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5f15\u5165\u5305\u4e2d\u7684\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5305\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u901a\u5e38\u7528\u6765\u7ec4\u7ec7\u5927\u578b\u9879\u76ee\u3002\u5728\u5f15\u5165\u5305\u4e2d\u7684\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u70b9\u53f7<code>.<\/code>\u6765\u8bbf\u95ee\u5b50\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6709\u4e00\u4e2a\u5305my_package\uff0c\u5176\u4e2d\u5305\u542b\u6a21\u5757module1<\/p>\n<p>from my_package import module1<\/p>\n<h2><strong>\u4f7f\u7528\u5305\u4e2d\u7684\u6a21\u5757<\/strong><\/h2>\n<p>module1.some_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5f15\u5165\u5305\u4e2d\u7684\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u7ec4\u7ec7\u4ee3\u7801\uff0c\u63d0\u9ad8\u9879\u76ee\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u52a8\u6001\u5f15\u5165\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5728\u8fd0\u884c\u65f6\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u52a8\u6001\u5f15\u5165\u6a21\u5757\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7<code>importlib<\/code>\u6a21\u5757\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib<\/p>\n<h2><strong>\u6839\u636e\u6761\u4ef6\u52a8\u6001\u5f15\u5165\u6a21\u5757<\/strong><\/h2>\n<p>module_name = &#39;math&#39; if use_math else &#39;cmath&#39;<\/p>\n<p>module = importlib.import_module(module_name)<\/p>\n<h2><strong>\u4f7f\u7528\u52a8\u6001\u5f15\u5165\u7684\u6a21\u5757<\/strong><\/h2>\n<p>result = module.sqrt(16)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u52a8\u6001\u5f15\u5165\u6a21\u5757\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\uff0c\u4f46\u4e5f\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u590d\u6742\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u7ba1\u7406\u4f9d\u8d56\u548c\u865a\u62df\u73af\u5883<\/p>\n<\/p>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u7ba1\u7406\u4f9d\u8d56\u548c\u865a\u62df\u73af\u5883\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u6765\u5b89\u88c5\u7b2c\u4e09\u65b9\u5305\uff0c\u5e76\u4f7f\u7528<code>venv<\/code>\u6216<code>virtualenv<\/code>\u521b\u5efa\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u521b\u5efa\u865a\u62df\u73af\u5883<\/p>\n<p>python -m venv myenv<\/p>\n<h2><strong>\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/strong><\/h2>\n<p>source myenv\/bin\/activate  # \u5728Linux\u6216Mac\u4e0a<\/p>\n<p>myenv\\Scripts\\activate     # \u5728Windows\u4e0a<\/p>\n<h2><strong>\u5b89\u88c5\u5305<\/strong><\/h2>\n<p>pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u865a\u62df\u73af\u5883\u53ef\u4ee5\u907f\u514d\u4f9d\u8d56\u51b2\u7a81\uff0c\u5e76\u786e\u4fdd\u9879\u76ee\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5f15\u5165\u5305\u662f\u4e00\u4e2a\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u7406\u89e3\u5982\u4f55\u6b63\u786e\u5730\u5f15\u5165\u548c\u4f7f\u7528\u5305\u5bf9\u4e8e\u7f16\u5199\u9ad8\u6548\u3001\u53ef\u7ef4\u62a4\u7684Python\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002\u901a\u8fc7\u7075\u6d3b\u4f7f\u7528<code>import<\/code>\u3001<code>from ... import<\/code>\u3001<code>as<\/code>\u5173\u952e\u5b57\uff0c\u4ee5\u53ca\u52a8\u6001\u5f15\u5165\u6a21\u5757\u548c\u7ba1\u7406\u4f9d\u8d56\uff0c\u4f60\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406Python\u9879\u76ee\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5f15\u5165\u7b2c\u4e09\u65b9\u5e93\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5f15\u5165\u7b2c\u4e09\u65b9\u5e93\u901a\u5e38\u901a\u8fc7\u4f7f\u7528<code>pip<\/code>\u5de5\u5177\u8fdb\u884c\u5b89\u88c5\u3002\u9996\u5148\uff0c\u9700\u8981\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c<code>pip install \u5e93\u540d<\/code>\u6765\u5b89\u88c5\u6240\u9700\u7684\u5e93\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528<code>import \u5e93\u540d<\/code>\u6765\u5f15\u5165\u8be5\u5e93\u3002\u4f8b\u5982\uff0c\u8981\u5b89\u88c5\u5e76\u5f15\u5165<code>requests<\/code>\u5e93\uff0c\u53ef\u4ee5\u6267\u884c<code>pip install requests<\/code>\uff0c\u7136\u540e\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528<code>import requests<\/code>\u3002<\/p>\n<p><strong>Python\u5185\u7f6e\u6a21\u5757\u5982\u4f55\u5f15\u5165\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u6a21\u5757\uff0c\u65e0\u9700\u5355\u72ec\u5b89\u88c5\u3002\u8981\u5f15\u5165\u8fd9\u4e9b\u6a21\u5757\uff0c\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8981\u4f7f\u7528\u968f\u673a\u6570\u751f\u6210\u5668\uff0c\u53ef\u4ee5\u901a\u8fc7<code>import random<\/code>\u5f15\u5165<code>random<\/code>\u6a21\u5757\u3002\u5f15\u5165\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\uff0c\u5982<code>random.randint(1, 10)<\/code>\u6765\u751f\u6210\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n<p><strong>\u5f15\u5165\u6a21\u5757\u65f6\u5982\u4f55\u5904\u7406\u547d\u540d\u51b2\u7a81\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5982\u679c\u5f15\u5165\u7684\u6a21\u5757\u4e0e\u73b0\u6709\u53d8\u91cf\u6216\u6a21\u5757\u540c\u540d\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u547d\u540d\u51b2\u7a81\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>as<\/code>\u5173\u952e\u5b57\u4e3a\u5f15\u5165\u7684\u6a21\u5757\u6307\u5b9a\u4e00\u4e2a\u522b\u540d\u3002\u4f8b\u5982\uff0c<code>import numpy as np<\/code>\u5c06<code>numpy<\/code>\u5e93\u5f15\u5165\u5e76\u8d4b\u4e88\u522b\u540d<code>np<\/code>\uff0c\u8fd9\u6837\u5728\u540e\u7eed\u4ee3\u7801\u4e2d\u5c31\u53ef\u4ee5\u7528<code>np<\/code>\u6765\u4ee3\u66ff<code>numpy<\/code>\uff0c\u4ece\u800c\u907f\u514d\u4e86\u51b2\u7a81\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5f15\u5165\u5305\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528import\u8bed\u53e5\u5f15\u5165\u6574\u4e2a\u6a21\u5757\u3001\u4f7f\u7528from &#8230; import\u8bed\u53e5\u5f15 [&hellip;]","protected":false},"author":3,"featured_media":933226,"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\/933220"}],"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=933220"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933220\/revisions"}],"predecessor-version":[{"id":933231,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/933220\/revisions\/933231"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/933226"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=933220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=933220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=933220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}