{"id":2685,"date":"2023-09-06T16:52:28","date_gmt":"2023-09-06T08:52:28","guid":{"rendered":"https:\/\/www.tra56.com\/?p=2685"},"modified":"2023-09-06T16:52:28","modified_gmt":"2023-09-06T08:52:28","slug":"2685","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/2685.html","title":{"rendered":"python\u4e2dos.environ\u7684\u4f7f\u7528\u8be6\u89e3"},"content":{"rendered":"
1. \u7b80\u4ecb<\/span><\/p>\n \u5728python \u4e2d\uff0c\u901a\u8fc7 os.environ \u83b7\u53d6\u73af\u5883\u53d8\u91cf\u3002<\/span><\/p>\n \u4ec0\u4e48\u662f\u73af\u5883\u53d8\u91cf\u5462\uff1f\u73af\u5883\u53d8\u91cf\u662f\u7a0b\u5e8f\u548c\u64cd\u4f5c\u7cfb\u7edf\u4e4b\u95f4\u7684\u901a\u4fe1\u65b9\u5f0f\u3002\u6709\u4e9b\u5b57\u7b26\u4e0d\u5b9c\u660e\u6587\u5199\u8fdb\u4ee3\u7801\u91cc\uff0c\u6bd4\u5982\u6570\u636e\u5e93\u5bc6\u7801\uff0c\u4e2a\u4eba\u8d26\u6237\u5bc6\u7801\uff0c\u5982\u679c\u5199\u8fdb\u81ea\u5df1\u672c\u673a\u7684\u73af\u5883\u53d8\u91cf\u91cc\uff0c\u7a0b\u5e8f\u7528\u7684\u65f6\u5019\u901a\u8fc7 os.environ.get() \u53d6\u51fa\u6765\u5c31\u884c\u4e86\u3002\u8fd9\u6837\u5f00\u53d1\u4eba\u5458\u672c\u673a\u6d4b\u8bd5\u7684\u65f6\u5019\u7528\u7684\u662f\u81ea\u5df1\u672c\u673a\u7684\u4e00\u5957\u5bc6\u7801\uff0c\u751f\u4ea7\u73af\u5883\u90e8\u7f72\u7684\u65f6\u5019\uff0c\u7528\u7684\u662f\u516c\u53f8\u7684\u516c\u5171\u8d26\u53f7\u548c\u5bc6\u7801\uff0c\u8fd9\u6837\u5c31\u80fd\u589e\u52a0\u5b89\u5168\u6027\u3002os.environ \u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u662f\u73af\u5883\u53d8\u91cf\u7684\u5b57\u5178\u3002\u901a\u8fc7os.environ.get(\u201cHOME\u201d)\uff0c\u5c31\u53ef\u4ee5\u83b7\u53d6\u73af\u5883\u53d8\u91cfHOME\u7684\u503c\uff0c\u5982\u679c\u6709\u8fd9\u4e2a\u952e\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u503c\uff1b\u5982\u679c\u6ca1\u6709\uff0c\u8fd4\u56de none<\/span><\/p>\n 2. key \u5b57\u6bb5\u8be6\u89e3<\/span><\/p>\n os.environ.keys() \u4e3b\u76ee\u5f55\u4e0b\u6240\u6709\u7684 key<\/span><\/p>\n <\/span><\/p>\n 2.1 \u5e38\u89c1 key \u5b57\u6bb5<\/span><\/p>\n windows\uff1a<\/span><\/p>\n <\/span><\/p>\n <\/span><\/p>\n linux\uff1a<\/span><\/p>\n <\/span><\/p>\n <\/span><\/p>\n 3. os.environ.get() \u7528\u6cd5<\/span><\/p>\n <\/span><\/p>\n import os<\/span><\/p>\n <\/span><\/p>\n \u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff0c\u5f53\u952e\u5b58\u5728\u65f6\u8fd4\u56de\u5bf9\u5e94\u7684\u503c\uff0c\u4e0d\u5b58\u5728\u65f6\uff0c\u8fd4\u56de\u9ed8\u8ba4\u503c<\/span><\/p>\n <\/span><\/p>\n 4. \u73af\u5883\u53d8\u91cf\u7684\u589e\u5220\u6539\u67e5\u548c\u5224\u65ad\u662f\u5426\u5b58\u5728<\/span><\/p>\n 4.1 \u65b0\u589e\u73af\u5883\u53d8\u91cf<\/span><\/p>\n <\/span><\/p>\n <\/span><\/p>\n 4.2 \u66f4\u65b0\u73af\u5883\u53d8\u91cf<\/span><\/p>\n <\/span><\/p>\n 4.3 \u83b7\u53d6\u73af\u5883\u53d8\u91cf<\/span><\/p>\n <\/span><\/p>\n <\/span><\/p>\n 4.4 \u5220\u9664\u73af\u5883\u53d8\u91cf<\/span><\/p>\n <\/span><\/p>\n <\/span><\/p>\n 4.5 \u5224\u65ad\u73af\u5883\u53d8\u91cf\u662f\u5426\u5b58\u5728<\/span><\/p>\n <\/span><\/p>\n <\/span><\/p>\n","protected":false},"excerpt":{"rendered":" 1. \u7b80\u4ecb \u5728python \u4e2d\uff0c\u901a\u8fc7 os.environ \u83b7\u53d6\u73af\u5883\u53d8\u91cf\u3002 \u4ec0\u4e48\u662f\u73af\u5883\u53d8\u91cf\u5462\uff1f\u73af\u5883\u53d8\u91cf\u662f\u7a0b\u5e8f\u548c […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-2685","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2685"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=2685"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2685\/revisions"}],"predecessor-version":[{"id":2686,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2685\/revisions\/2686"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=2685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=2685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=2685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
<\/span><\/p>\n
<\/span><\/p>\n
<\/span><\/p>\n
<\/span><\/p>\nos.environ['HOMEPATH']:\u5f53\u524d\u7528\u6237\u4e3b\u76ee\u5f55\u3002\r\nos.environ['TEMP']:\u4e34\u65f6\u76ee\u5f55\u8def\u5f84\u3002\r\nos.environ["PATHEXT"]:\u53ef\u6267\u884c\u6587\u4ef6\u3002\r\nos.environ['SYSTEMROOT']:\u7cfb\u7edf\u4e3b\u76ee\u5f55\u3002\r\nos.environ['LOGONSERVER']:\u673a\u5668\u540d\u3002\r\nos.environ['PROMPT']:\u8bbe\u7f6e\u63d0\u793a\u7b26\u3002<\/pre>\n
os.environ['USER']:\u5f53\u524d\u4f7f\u7528\u7528\u6237\u3002\r\nos.environ['LC_COLLATE']:\u8def\u5f84\u6269\u5c55\u7684\u7ed3\u679c\u6392\u5e8f\u65f6\u7684\u5b57\u6bcd\u987a\u5e8f\u3002\r\nos.environ['SHELL']:\u4f7f\u7528shell\u7684\u7c7b\u578b\u3002\r\nos.environ['LAN']:\u4f7f\u7528\u7684\u8bed\u8a00\u3002\r\nos.environ['SSH_AUTH_SOCK']:ssh\u7684\u6267\u884c\u8def\u5f84\u3002<\/pre>\n
os.environ \u662f\u4e00\u4e2a\u73af\u5883\u53d8\u91cf\u7684\u5b57\u5178\uff0c\u53ef\u4ee5\u901a\u8fc7 get \u65b9\u6cd5\u83b7\u53d6\u952e\u5bf9\u5e94\u7684\u503c\u3002\u5982\u679c\u6709\u8fd9\u4e2a\u952e\uff0c\u8fd4\u56de\u5bf9\u5e94\u7684\u503c\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u5219\u8fd4\u56de none<\/pre>\n
<\/span><\/p>\nprint(os.environ.get("HOME"))<\/pre>\n
<\/span><\/p>\nprint(os.environ.get("HOME", "default"))#\u73af\u5883\u53d8\u91cfHOME\u4e0d\u5b58\u5728\uff0c\u8fd4\u56dedefault<\/pre>\n
os.environ['\u73af\u5883\u53d8\u91cf\u540d\u79f0']='\u73af\u5883\u53d8\u91cf\u503c' #\u5176\u4e2dkey\u548cvalue\u5747\u4e3astring\u7c7b\u578b\r\nos.putenv('\u73af\u5883\u53d8\u91cf\u540d\u79f0', '\u73af\u5883\u53d8\u91cf\u503c')\r\nos.environ.setdefault('\u73af\u5883\u53d8\u91cf\u540d\u79f0', '\u73af\u5883\u53d8\u91cf\u503c')<\/pre>\n
os.environ['\u73af\u5883\u53d8\u91cf\u540d\u79f0']='\u65b0\u73af\u5883\u53d8\u91cf\u503c'<\/pre>\n
os.environ['\u73af\u5883\u53d8\u91cf\u540d\u79f0']\r\nos.getenv('\u73af\u5883\u53d8\u91cf\u540d\u79f0')\r\nos.environ.get('\u73af\u5883\u53d8\u91cf\u540d\u79f0', '\u9ed8\u8ba4\u503c') #\u9ed8\u8ba4\u503c\u53ef\u7ed9\u53ef\u4e0d\u7ed9\uff0c\u73af\u5883\u53d8\u91cf\u4e0d\u5b58\u5728\u8fd4\u56de\u9ed8\u8ba4\u503c<\/pre>\n
del os.environ['\u73af\u5883\u53d8\u91cf\u540d\u79f0']\r\ndel(os.environ['\u73af\u5883\u53d8\u91cf\u540d\u79f0'])<\/pre>\n
'\u73af\u5883\u53d8\u91cf\u503c' in os.environ # \u5b58\u5728\u8fd4\u56de True\uff0c\u4e0d\u5b58\u5728\u8fd4\u56de False<\/pre>\n