用 Python 和环境变量保护你的秘密
你建立的大多数 Python 程序都包括一些你不想与世界分享的秘密信息。想想你的网络服务调用的 API 密钥,数据库登录凭证,或者你的食谱生成器中的秘密酱汁的成分!这些都是你的秘密。
虽然Git和GitHub很好,但这些个人秘密不应该被带到开源社区中。
这就是环境变量的用武之地。在这篇博客中,你将学习如何在Bash和Python虚拟环境中使用Python和环境变量来保护你的秘密。到最后,你将知道如何
- 在Bash中设置一个变量
- 从你的Bash命令行中添加和删除 环境变量
- 在你的Python虚拟环境中创建 虚拟环境变量
- 当你激活或停用你的虚拟环境时,自动地设置和取消 这些虚拟环境变量
知道如何使用Python和环境变量是为网络构建时的一项重要技能,而且在其他许多需要一定程度保密的情况下也会有帮助。
.fusion-body .fusion-builder-column-23{width:100% !important;margin-top : 10px;margin-bottom : 10px;}.fusion-builder-column-23 > .fusion-column-wrapper {padding-top : 0px !important;padding-right : 0px !important;margin-right : 1.92%;padding-bottom : 0px !important;padding-left : 0px !margin-left : 1.92%;}@media only screen and (max-width:1024px) {.fusion-body .fusion-builder-column-23{width:100% !important;order : 0;}.fusion-builder-column-23 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-23{width:100% !important;order : 0;}.fusion-builder-column-23 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}.
.fusion-body .fusion-flex-container.fusion-builder-row-16{ padding-top : 0px;margin-top : 0px;padding-right : 0px;padding-bottom : 0px; margin-bottom : 0px;padding-left : 0px;}.
目录
.fusion-body .fusion-builder-column-24{width:100% !important;margin-top : 10px;margin-bottom : 10px;}.fusion-builder-column-24 > .fusion-column- wrapper {padding-top : 0px !important;padding-right : 0px !important;margin-right : 1.92%;padding-bottom : 0px !important;padding-left : 0px !margin-left : 1.92%;}@media only screen and (max-width:1024px) {.fusion-body .fusion-builder-column-24{width:100% !important;order : 0;}.fusion-builder-column-24 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-24{width:100% !important;order : 0;}.fusion-builder-column-24 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}.
.fusion-body .fusion-flex-container.fusion-builder-row-17{ padding-top : 0px;margin-top : 0px;padding-right : 0px;padding-bottom : 0px; margin-bottom : 0px;padding-left : 0px;}。
避免恐怖的情况发生
网络上有很多关于意外发布API密钥秘密的恐怖故事,最终让所有者损失惨重。如果你需要一些额外的说服力,或者只是想在今晚熬夜,可以看看下面的帖子。
快速的启示是,你不应该把你的秘密发布到GitHub上。
博狗娱乐网址是多少的速度很快,一个被破坏的提交就太多了。
有多种方法可以保证你的敏感信息的安全。在这篇文章中,你将学习如何在 UNIX 系统中使用 Python 和环境变量来做到这一点。
.fusion-body .fusion-builder-column-25{width:100% !important;margin-top : 10px;margin-bottom : 10px;}.fusion-builder-column-25 > .fusion-column-wrapper {padding-top : 0px !important;padding-right : 0px !important;margin-right : 1.92%;padding-bottom : 0px !important;padding-left : 0px !margin-left : 1.92%;}@media only screen and (max-width:1024px) {.fusion-body .fusion-builder-column-25{width:100% !important;order : 0;}.fusion-builder-column-25 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-25{width:100% !important;order : 0;}.fusion-builder-column-25 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}.
.fusion-body .fusion-flex-container.fusion-builder-row-18{ padding-top : 0px;margin-top : 0px;padding-right : 0px;padding-bottom : 0px; margin-bottom : 0px;padding-left : 0px;}.
在Bash中使用环境变量
环境变量 是动态命名的数值,你可以从当前环境中的任何地方访问它们。它们可以帮助你使你的脚本运行得更加友好和安全,并且在你当前环境中的所有应用程序中共享。
在UNIX系统中,其中最著名的是 $PATH,它指定了你的系统寻找可执行文件的文件路径。
你可以在你的项目中的任何地方访问你的环境变量的值,而不需要写出该变量的实际值。相反,你可以通过Bash中定义的环境变量来引用它。
这样,你就可以在整个项目中使用API机密和密码,并将所有项目相关的代码提交给GitHub,同时保证敏感信息的安全,并对自己负责。
.fusion-body .fusion-builder-column-26{width:100% !important;margin-top : 10px;margin-bottom : 10px;}.fusion-builder-column-26 > .fusion-column- wrapper {padding-top : 0px !important;padding-right : 0px !important;margin-right : 1.92%;padding-bottom : 0px !important;padding-left : 0px !margin-left : 1.92%;}@media only screen and (max-width:1024px) {.fusion-body .fusion-builder-column-26{width:100% !important;order : 0;}.fusion-builder-column-26 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-26{width:100% !important;order : 0;}.fusion-builder-column-26 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}.
.fusion-body .fusion-flex-container.fusion-builder-row-19{ padding-top : 0px;margin-top : 0px;padding-right : 0px;padding-bottom : 0px; margin-bottom : 0px;padding-left : 0px;}.
检查环境变量
打开你的CLI,输入Bash命令 **printenv**.这将给你一个当前存在于你的系统中的所有环境变量的列表。
复制到剪贴板
HOME=/Users/Martin LOGNAME=Martin USER=Martin PATH=/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/bin:/bin
这个例子的输出向你展示了目前在你的本地机器上定义的几个环境变量。你可能会在自己的输出中看到不同的名称和一些额外的行。
你可以用**echo $<NAME>** 来检查每个变量的值。因为在我的例子中,环境变量**$LOGNAME** 指向**Martin** ,我可以用echo 命令来确认。
复制到剪贴板
echo $LOGNAME
在运行同一命令时,你收到的输出将是你的**LOGNAME** 变量所指向的。你也可以通过运行**printenv** 并在输出中寻找**LOGNAME** 来确认这个值。
通过这两条命令,你可以检查目前在你的系统中定义的所有环境变量。但如果你想用Bash来改变、增加或删除一个环境变量呢?
.fusion-body .fusion-builder-column-27{width:100% !important;margin-top : 10px;margin-bottom : 10px;}.fusion-builder-column-27 > .fusion-column-wrapper {padding-top : 0px !important;padding-right : 0px !important;margin-right : 1.92%;padding-bottom : 0px !important;padding-left : 0px !margin-left : 1.92%;}@media only screen and (max-width:1024px) {.fusion-body .fusion-builder-column-27{width:100% !important;order : 0;}.fusion-builder-column-27 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-27{width:100% !important;order : 0;}.fusion-builder-column-27 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}.
.fusion-body .fusion-flex-container.fusion-builder-row-20{ padding-top : 0px;margin-top : 0px;padding-right : 0px;padding-bottom : 0px; margin-bottom : 0px;padding-left : 0px;}.
在Bash中设置一个变量并删除它
在你的CLI中使用Bash,你可以用以下命令添加一个新的环境变量。
复制到剪贴板
出口=
在这个例子中,你必须用你想添加的新环境变量替换**<NAME>** 。你还需要用你想分配给该环境变量的值来替换**<VALUE>** 。
例如,要添加一个新的变量,名称为**DAY** ,数值为**Sunday** ,你要这样拼写。
复制到剪贴板
export DAY=Sunday
执行这个命令后,你可以看到一个新的变量已经被添加到你的环境中。如上所述,用**printenv** 和**echo** 来看一下吧。
为了用Bash删除一个环境变量,你必须调用以下命令。
复制到剪贴板
取消设置
同样,你必须添加你想删除的变量的实际名称,而不是占位符**<NAME>** 。
复制到剪贴板
unset DAY
这个Bash命令删除了你之前设置的**DAY** 变量。
试着用这些Bash命令添加和删除一些环境变量。记住你可以随时用**
printenv** 或**echo <NAME>** 来检查发生了什么。
然而,当你在进行Python web开发项目时,你不希望在整个系统环境中设置你的环境变量。例如,只要你在一个以上的Django项目中工作,你在每个项目中需要的**SECRET_KEY** 变量就会相互冲突。这就是为什么你应该使用虚拟环境将你的环境变量分割开来。
.fusion-body .fusion-builder-column-28{width:100% !important;margin-top : 10px;margin-bottom : 10px;}.fusion-builder-column-28 > .fusion-column- wrapper {padding-top : 0px !important;padding-right : 0px !important;margin-right : 1.92%;padding-bottom : 0px !important;padding-left : 0px !margin-left : 1.92%;}@media only screen and (max-width:1024px) {.fusion-body .fusion-builder-column-28{width:100% !important;order : 0;}.fusion-builder-column-28 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-28{width:100% !important;order : 0;}.fusion-builder-column-28 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}.
.fusion-body .fusion-flex-container.fusion-builder-row-21{ padding-top : 0px;margin-top : 0px;padding-right : 0px;padding-bottom : 0px; margin-bottom : 0px;padding-left : 0px;}.
.fusion-body .fusion-builder-column-29{width:100% !important;margin-top : 10px; margin-bottom : 10px;}.fusion-builder-column-29 > .fusion-column-wrapper {padding-top : 0px !important;padding-right : 0px !important;margin-right : 1.92%;padding-bottom : 0px !important;padding-left : 0px !important;margin-left : 1.92%;}@media only screen and (max-width : 1024px) {.fusion-body .fusion-builder-column-29{width:100% !important;order : 0;}.fusion-builder-column-29 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-29{width:100% !important;order : 0;}.fusion-builder-column-29 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}.
.fusion-body .fusion-builder-column-30{width:50% !important;margin-top : 10px; margin-bottom : 10px;}.fusion-builder-column-30 > .fusion-column-wrapper {padding-top : 0px !important;padding-right : 0px !important;margin-right : 3.84%;padding-bottom : 0px !important;padding-left : 0px !important;margin-left : 3.84%;}@media only screen and (max-width : 1024px) {.fusion-body .fusion-builder-column-30{width:100% !important;order : 0;}.fusion-builder-column-30 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-30{width:100% !important;order : 0;}.fusion-builder-column-30 > .fusion-column-wrapper {margin-right : 1.92%;margin-left : 1.92%;}。
学习从初级到高级的Python技能。
- **培训1:**Python脚本
- 培训2 :Python APIs
- 培训3 :Python OOP
@import url(static.mailerlite.com/assets/plug…); .ml-form-embedSubmitLoad{display:inline-block;width:20px;height:20px}.sr-only{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden; clip:rect(0,0,0,0);border:0}.ml-form-embedSubmitLoad:after{内容:" ";显示:block;宽度:11px;高度:11px;margin:1px;border-radius:50%;border:4px solid #fff;border-color:#fff #fff透明;动画:ml-form-embedSubmitLoad 1.2s linear infinite}@keyframes ml-form-embedSubmitLoad{0%{transform:rotate(0)}100%{transform:rotate(360deg)}#mlb2-3502078.ml-form-embedContainer{box-sizing:border-box;display:table;margin:0 auto;position:static;width:100%!important}#mlb2-3502078.ml-form-embedContainer按钮,#mlb2-3502078.ml-form-embedContainer h4,#mlb2-3502078.ml-form-embedContainer p,#mlb2-3502078.ml-form-embedContainer span{text-transform:none!important;letter-spacing:normal!important}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper{background-color:#fff;border-width:0;border-color:transparent;border-radius:4px;border-style:solid;box-sizing:border-box;display:in line-block!important;margin:0;padding:0;position:relative}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper.embedDefault,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper.embedPopup{width:400px}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper.embedForm{max-width:400px;width:100%}#mlb2-3502078.ml-form-embedContainer .ml-form-align-left{text-align:left}#mlb2-3502078.ml-form-embedContainer .ml-form-align-center{text-align:center}#mlb2-3502078.ml-form-embedContainer .ml-form-align-default{display:table-cell! important;垂直对齐:middle!important;text-align:center! important}#mlb2-3502078.ml-form-embedContainer .ml-form-align-right{text-align:right}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedHeader img{border-top-left-radius:4px;border-top-right-radius:4px;height:auto;margin:0 auto!important;max-width:100%;width:undefinedpx}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody,#mlb2-3502078。ml-form-embedContainer .ml-form-embedWrapper .ml-form-successBody{padding:20px 20px 0 20px}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody。ml-form-embedBodyHorizontal{padding-bottom:0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedContent,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-successBody .ml-form-successContent{text-align:left;margin:0 0 20px 0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedContent h4,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper 。ml-form-successBody .ml-form-successContent h4{color:#000;font-family:Roboto,Arial,Helvetica,sans-serif;font-size:30px;font-weight:400;margin:0 0 10px 0;text-align:left;word-break:break-word}#mlb2-3502078。ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedContent p,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-successBody .ml-form-successContent p{color:#000;font-family:Roboto,Arial,Helvetica,sans-serif;font-size:14px;font-weight:400;line-height:20px;margin:0 0 10px 0;text-align:left}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedContent ol,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedContent ul,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-successBody .ml-form-successContent ol,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-successBody .ml-form-successContent ul{color:#000;font-family:Roboto,Arial,Helvetica,sans-serif;font-size:14px}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedContent ol ol,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-successBody .ml-form-successContent ol{list-style-type:lower-alpha}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedContent ol ol,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-successBody .ml-form-successContent ol ol{list-style-type:lower-roman}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedContent p a,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-successBody .ml-form-successContent p a{color:#000;text-decoration:underline}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-block-form .ml-field-group{text-align:left! important}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-block-form .ml-field-group label{margin-bottom:5px;color:#333;font-size:14px;font-family:Roboto,Arial,Helvetica,sans-serif;font-weight:700;font-style:normal;text-decoration:none;display:inline-block;line-height:20px}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedContent p:last-child,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-successBody .ml-form-successContent p:last-child{margin:0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody form{margin:0;width:100%}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-Content{margin:0 0 20px 0;width:100%}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow{float:left}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-Content.horozintalForm{margin:0;padding:0 0 20px 0;width:100%;height:auto;float:left}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow{margin:0 0 10px 0;width:100%}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow.ml-last-item{margin:0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow.ml-formfieldHorizintal{margin:0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow input{background-color:#fff!important;color:#333!important;border-color:#0467ab; border-radius:4px!important;font-family:Roboto,Arial,Helvetica,sans-serif;font-size:14px!important;height:21px!important;margin-bottom:0; margin-top:0;margin-left:0; margin-right:0;padding:10px 10px!important;width:100%!important;box-sizing:border-box!important;max-width:100%!important}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow input::-webkit-input-placeholder,#mlb2-3502078。ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow input::-webkit-input-placeholder{color:#333}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow input::-moz-placeholder,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow input::-moz-placeholder{color:#333}#mlb2-3502078。ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow input:-ms-input-placeholder,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow input:-ms-input-placeholder{color:#333}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow input:-moz-placeholder,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow input:-moz-placeholder{color:#333}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow textarea,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow textarea{background-color:#fff! important;color:#333!important;border-radius:4px!important;border-style:solid!important;border-width:1px!important;font-family:Roboto,Arial,Helvetica,sans-serif;font-size:14px;important;height:auto;line-height:21px!important;margin-bottom:0;margin-top:0;padding:10px 10px!important;width:100%!important;box-sizing:border-box!important;max-width:100%!important}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper 。ml-form-embedBody .ml-form-checkboxRow .label-description::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsOptionsCheckBox .label-description::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-checkbox .custom-control-label::before,#mlb2-3502078。ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-radio .custom-control-label::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow .custom-checkbox .custom-control-label::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow .custom-radio .custom-control-label::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-interestGroupsRow .ml-form-interestGroupsRowCheckbox .label-description::before{边界颜色:#0467ab!important;background-color:#fff! important}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow input.custom-control-input[type=checkbox]{box-sizing:border-box;padding:0;position:absolute;z-index:-1;opacity:0;margin-top:5px; margin-left:-24px; overflow:visible}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow .label-description::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsOptionsCheckbox . label-description::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-checkbox .custom-control-label::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow .custom-checkbox .custom-control-label::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-interestGroupsRow .ml-form-interestGroupsRowCheckbox .label-description::before{border-radius:4px!important}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow input[type=checkbox]: checked~.label-description::after,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsOptionsCheckbox input[type=checkbox] :checked~.label-description::after,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-checkbox .custom-control-input:checked~.custom control-label::after,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow .custom-checkbox .custom-control-input:checked~.custom-control-label::after,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-interestGroupsRow .ml-form-interestGroupsRowCheckbox input[type=checkbox]:checked~.label-description::after{background-color:#fff;mask-image:url(cdn.mailerlite.com/images/defa… .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-radio .custom-control-input:checked~.custom-control-label: :after{背景色:#fff; mask-image:url(cdn.mailerlite.com/images/defa… .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow input[type=checkbox]:checked~。label-description::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsOptionsCheckbox input[type=checkbox] :checked~.label-description::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-checkbox .custom-control-input:checked~.custom control-label::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-radio .custom-control-input:checked~.custom-control-label::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow .custom-checkbox .custom-control-input:checked~.custom-control-label::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow .custom-radio .custom-control-input:checked~.custom-control-label::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-interestGroupsRow .ml-form-interestGroupsRowCheckbox input[type=checkbox]:checked~.label-description::before{边框颜色:#0467ab!important;背景颜色:#0467ab!important}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-checkbox .custom-control-label::after,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-checkbox .custom-control-label::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-radio .custom-control-label::after,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-radio .custom-control-label::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow .custom-checkbox .custom-control-label::after,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow .custom-checkbox .custom-control-label::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow .custom-radio .custom-control-label::after,#mlb2-3502078。ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow .custom-radio .custom-control-label::before{top:2;box-sizing:border-box}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow .label-description::after,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow .label-description::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsOptionsCheckbox . label-description::after,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsOptionsCheckbox .label-description::before{top:0! important;box-sizing:border-box!important}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow .label-description::after,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody.ml-form-checkboxRow .label-description::before{top:0!important;box-sizing:border-box!important}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-interestGroupsRow .ml-form-interestGroupsRowCheckbox .label-description::after{top:3px!important;box-sizing:border-box!important;position:absolute;left:-21px;display:block;width:10px;height:10px;content:"" }#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-interestGroupsRow .ml-form-interestGroupsRowCheckbox .label-description::before{top:0!important;box-sizing:border-box! important}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .custom-control-label::before{position:absolute;top:4px;left:-24px;display:block;width:16px;height:16px;pointer-events:none;content:"" ; background-color:#fff; border:#adb5bd solid 1px; border-radius:50%}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .custom-control-label::after{位置:绝对;top:5px!important;left:-21px;display:block;width:10px;height:10px;content:""}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow .label-description::before,#mlb2-3502078。ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsOptionsCheckbox .label-description::before,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-interestGroupsRow .ml-form-interestGroupsRowCheckbox .label-description::before{position:absolute;top:4px;left:-24px;display:block;width:16px;height:16px;pointer-events:none;content:""; background-color:#fff;border:#adb5bd solid 1px; border-radius:50%}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions.ml-form-embedPermissionsOptionsCheckbox .label-description::after{position:absolute;top:3px!important;left:-21px;display:block;width:10px;height:10px;content:""}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow .label-description::after{position:absolute;top:3px!important;left:-21px;display:block;width:10px;height:10px;content:"" }#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .custom-radio .custom-control-label::after{background:no-repeat 50%/50%}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .custom-checkbox .custom-control-label::after,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow .标签-描述::after,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsOptionsCheckbox .label-description::after,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-interestGroupsRow .ml-form-interestGroupsRowCheckbox .label-description::after{background:no-repeat 50%/50%}#mlb2-3502078.ml-form-embedContainer.ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-control,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow .custom-control{position:relative;display:block;min-height:1.5rem;padding-left:1.5rem}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-checkbox .custom-control-input,#mlb2-3502078。ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-radio .custom-control-input,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow .custom-checkbox .custom-control-input,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow .custom-radio .custom-control-input{position:absolute;z-index:-1;opacity:0;box-sizing:border-box;padding:0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-checkbox .custom-control-label,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-radio .custom-control-label,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow .custom-checkbox .custom-control-label,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow .custom-radio .custom-control-label{color:#000;font-size:12px!important;font-family:'Open Sans',Arial,Helvetica,sans-serif;line-height:22px;margin-bottom:0;position:relative;vertical-align:top;font-style:normal;font-weight:700}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-fieldRow .custom-select,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow .custom-select{background-color:#fff!important;color:#333!important;border-color:#0467ab!important;border-radius:4px!important;border-style:solid!important;font-family:Roboto,Arial,Helvetica,sans-serif;font-size:14px!important;line-height:20px!important;margin-bottom:0;margin-top:0;padding:10px 28px 10px 12px!important;width:100%!important;box-sizing:border-box!important;max-width:100%;heightant;display:ine-block;vertical-align:middle;background:url(cdn.mailerlite.com/images/defa…) no-repeat right .75rem center/8px 10px;-webkit-appearance:none;-moz-appearance:none;appearance:none}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow{高度:自动;宽度:100%;浮动:左侧}。ml-form-Content.horozintalForm .ml-form-horizontalRow .ml-input-horizontal{width:70%;float:left}.ml-form-Content.horozintalForm .ml-form-horizontalRow .ml-button-horizontal{width:30%;float:left}.ml-form-formContent.horozintalForm .ml-form-horizontalRow .ml-button-horizontal.labelsOn{padding-top:25px}.ml-form-formContent.horozintalForm .ml-form-horizontalRow .horizontal-fields{box-sizing:border-box; float:left;padding-right:10px}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow input{background-color:#fff;color:#333;border-color:#0467ab;border-radius:4px; border-style:solid; border-width:1px;font-family:Roboto,Arial,Helvetica,sans-serif;font-size:14px;line-height:20px;margin-bottom:0;margin-top:0;padding:10px 10px;width:100%;box-sizing:border-box;overflow-y:initial}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-horizontalRow button{background-color:#0467ab!important;border-color:#0467ab;border-style:solid;border-width:1px;border-radius:4px;box-shadow:none;color:#fff! important;cursor:pointer;font-family:Roboto,Arial,Helvetica,sans-serif;font-size:16px!important;font-weight:700;line-height:20px;margin:0!important;padding:10px!important;width:100%;height:auto}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody 。ml-form-horizontalRow button:hover{background-color:#f9b240!important;border-color:#f9b240!important}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow input[type=checkbox]{box-sizing:border-box;padding:0;position:absolute;z-index:-1;opacity:0;margin-top:5px; margin-left:-24px;overflow:visible}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow .label-description{color:#000;display:block;font-family:'Open Sans',Arial,Helvetica,sans-serif;font-size:12px; text-align:left; margin-bottom:0;position:relative;vertical-align:top}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow label{font-weight:400;margin:0;padding:0;position:relative;display:block;min-height:24px;padding-left:24px}#mlb2-3502078.ml-form-embedContainer.ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow label a{color:#000;text-decoration:underline}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow label p{color:#000!important;font-family:'Open Sans',Arial,Helvetica,sans-serif!important;font-size:12px!important;font-weight:400!important;line-height:18px!important;padding:0:margin:0 5px 0 0!important}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow label p:last-child{margin:0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedSubmit{margin:0 0 20px 0;float:left;width:100%}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedSubmit按钮{background-color:#0467ab!important;border:none!important;border-radius:4px!important;box-shadow:none!important;color:#fff!important;cursor:pointer;font-family:Roboto,Arial,Helvetica,sans-serif;important;font-size:16px!important;font-weight:700!important;line-height:21px!important;height:auto;padding:10px!important;width:100%!important;box-sizing:border-box! important}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedSubmit button.loading{display:none}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedSubmit button:hover{background-color:#f9b240! important}.ml-subscription-close{width:30px;height:30px;background:url(cdn.mailerlite.com/images/defa…) no-repeat;background-size:30px;cursor:pointer;margin-top:-10px; margin-right:-10px;position:absolute;top:0; right:0}.ml-error input{border-color:red!important}.ml-error .label-description, .ml-error .label-description p, .ml-error .label-description p a, .ml-error label:first-child{color:red! important}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow.ml-error .label-description p,#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-checkboxRow.ml-error .label-description p:first-letter{color:red!important}@media only screen and (max-width:400px){.ml-form-embedWrapper.embedDefault,.ml-form-embedWrapper.embedPopup{width:100%!important}.ml-form-Content.horozintalForm{float:left!important}.ml-form-Content.horozintalForm .ml-form-horizontalRow{height:auto!important;width:100%!important}.ml-form-formContent.horozintalForm .ml-form-horizontalRow .ml-input-horizontal{width:100%!important}.ml-form-formContent.horozintalForm .ml-form-horizontalRow.ml-input-horizontal>div{padding-right:0!important;padding-bottom:10px}.ml-form-Content.horozintalForm .ml-button-horizontal{width:100%!important}.ml-form-Content.horozintalForm .ml-button-horizontal.labsOn{padding-top:0!} #mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions{text-align:left;float:left;width:100%}#mlb2-3502078。ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsContent{margin:0 0 15px 0;text-align:left}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsContent.horizontal{margin:0 0 15px 0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions.ml-form-embedPermissionsContent h4{color:#000;font-family:'Open Sans',Arial,Helvetica,sans-serif;font-size:12px;font-weight:700;line-height:18px;margin:0 0 10px 0;word-break:break-word}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions 。ml-form-embedPermissionsContent p{color:#000;font-family:'Open Sans',Arial,Helvetica,sans-serif;font-size:12px;line-height:18px; margin:0 0 10px 0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsContent。privacy-policy p{color:#000;font-family:'Open Sans',Arial,Helvetica,sans-serif;font-size:12px;line-height:22px; margin:0 0 10px 0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsContent.privacy-policy p a{color:#000}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsContent.privacy-policy p:last-child{margin:0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsContent p a{color:#000;text-decoration:underline}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsContent p:last-child{margin:0 0 15px 0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper.ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsOptions{margin:0;padding:0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsOptionsCheckbox{margin:0 0 10px 0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsOptionsCheckbox:last-child{margin:0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsOptionsCheckbox label{font-weight:400;margin:0;padding:0;position:relative;display:block;min-height:24px;padding-left:24px}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsOptionsCheckbox .label-description{color:#000;font-family:'Open Sans',Arial,Helvetica,sans-serif;font-size:12px; line-height:18px;text-align:left;margin-bottom:0;position:relative;vertical-align:top;font-style:normal;font-weight:700}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsOptionsCheckbox .description{color:#000;font-family:'Open Sans',Arial,Helvetica,sans-serif;font-size:12px;font-style:italic;font-weight:400;line-height:18px;margin:5px 0 0 0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsOptionsCheckbox input[type=checkbox]{box-sizing:border-box;padding:0;position:absolute;z-index:-1;opacity:0;margin-top:5px;margin-left:-24px;overflow:visible}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedMailerLite-GDPR{padding-bottom:20px}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedMailerLite-GDPR p{color:#000;font-family:'Open Sans',Arial,Helvetica,sans-serif;font-size:10px;line-height:14px; margin:0;padding:0}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedMailerLite-GDPR p a{color:#000;text-decoration:underline}@media (max-width:768px){#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedPermissionsContent p{font-size:12px!important;line-height:18px!important}#mlb2-3502078.ml-form-embedContainer .ml-form-embedWrapper .ml-form-embedBody .ml-form-embedPermissions .ml-form-embedMailerLite-GDPR p{font-size:10px!}
通过注册,您同意接收来自CodingNomads的电子邮件。你可以在任何时候取消订阅。隐私政策。
现在观看
正在加载...
很好!
请查看您的收件箱中的Python培训系列链接。编码愉快!
function ml_webform_success_3502078(){var r=ml_jQuery||jQuery;r(".ml-subscribe-form-3502078 .row-success").show(),r(".ml-subscribe-form-3502078 .row-form").hide() }
.fusion-body .fusion-builder-column-31{width:50% !important;margin-top : 0px;margin-bottom : 10px;}.fusion-builder-column-31 > .fusion-column-wrapper {padding-top : 0px !important; padding-right : 0px !important;margin-right : 3.84%;padding-bottom : 0px !important;padding-left : 0px !important; margin-left : 3.84%;}@media only screen and (max-width:1024px) {.fusion-body .fusion-builder-column-31{width:100% !important;order : 0;}.fusion-builder-column-31 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-31{width:100% !important;order : 0;}.fusion-builder-column-31 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}.
.fusion-body .fusion-builder-column-32{width:100% !important;margin-top : 10px;margin-bottom : 10px;}.fusion-builder-column-32 > .fusion-column-wrapper {padding-top : 0px !important;padding-right : 0px !important;margin-right : 1.92%;padding-bottom : 0px !important;padding-left : 0px !margin-left : 1.92%;}@media only screen and (max-width:1024px) {.fusion-body .fusion-builder-column-32{width:100% !important;order : 0;}.fusion-builder-column-32 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-32{width:100% !important;order : 0;}.fusion-builder-column-32 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}.
.fusion-body .fusion-flex-container.fusion-builder-row-22{ padding-top : 0px;margin-top : 0px;padding-right : 0px;padding-bottom : 0px;margin-bottom : 0px;padding-left : 0px;}.
在Python虚拟环境中使用环境变量
当做任何项目的开发时,你总是希望避免为整个系统设置任何东西。对于秘密也是如此,它通常是项目专用的。
对于项目特定的秘密,在虚拟环境中的Bash中设置一个变量是一个更好的主意,这使得变量变成了虚拟环境变量。
在Python虚拟环境里面使用环境变量,比每次要在项目上工作时先**export** ,然后再**unset** ,要容易得多。
.fusion-body .fusion-builder-column-33{width:100% !important;margin-top : 10px; margin-bottom : 10px;}.fusion-builder-column-33 > .fusion-column-wrapper {padding-top : 0px !important;padding-right : 0px !important;margin-right : 1.92%;padding-bottom : 0px !important;padding-left : 0px !margin-left : 1.92%;}@media only screen and (max-width:1024px) {.fusion-body .fusion-builder-column-33{width:100% !important;order : 0;}.fusion-builder-column-33 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-33{width:100% !important;order : 0;}.fusion-builder-column-33 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}.
.fusion-body .fusion-flex-container.fusion-builder-row-23{ padding-top : 0px;margin-top : 0px;padding-right : 0px;padding-bottom : 0px; margin-bottom : 0px;padding-left : 0px;}.
编辑你的激活脚本
首先,为你的Python项目创建一个虚拟环境。
复制到剪贴板上
python3 -m venv venv
这个命令在你当前的项目文件夹中创建一个虚拟环境。你可以在Python工程课程中了解更多关于使用虚拟环境和Python的工作。
在你成功创建了一个虚拟环境之后,在你最喜欢的文本编辑器中打开**activate** 脚本。你可以在**venv** 文件夹中找到这个文件,这个文件夹是通过运行上面的命令创建的。这个脚本的相对路径是**venv/bin/activate** 。
这个Bash脚本在每次你的**venv** 被激活时都会运行,这使得它成为让你的计算机知道你希望拥有哪些环境变量,以及一旦你退出虚拟环境,哪些变量应该被删除的好地方。
你现在要编辑这个脚本,在Bash中设置你想要的环境变量。首先,你需要确保你的虚拟环境变量不会在你停用它们后继续存在,所以你首先要取消设置你甚至还没有创建的环境变量。
.fusion-body .fusion-builder-column-34{width:100% !important;margin-top : 10px;margin-bottom : 10px;}.fusion-builder-column-34 > .fusion-column-wrapper {padding-top : 0px !important;padding-right : 0px !important;margin-right : 1.92%;padding-bottom : 0px !important;padding-left : 0px !margin-left : 1.92%;}@media only screen and (max-width:1024px) {.fusion-body .fusion-builder-column-34{width:100% !important;order : 0;}.fusion-builder-column-34 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-34{width:100% !important;order : 0;}.fusion-builder-column-34 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}.
.fusion-body .fusion-flex-container.fusion-builder-row-24{ padding-top : 0px;margin-top : 0px;padding-right : 0px;padding-bottom : 0px; margin-bottom : 0px;padding-left : 0px;}.
取消设置虚拟环境变量
要取消虚拟环境变量,你可以在Bash**activate** 脚本中的**deactivate** 命令部分添加一个**unset** 命令。每次你**deactivate** 你的虚拟环境时,脚本这部分的代码就会运行。
例如,要取消设置变量**MY_SUPER_SECRET_SECRET** ,你需要添加以下一行Bash代码。
复制到剪贴板
deactivate () { unset MY_SUPER_SECRET_SECRET # Lots of other code }
在**deactivate** 部分添加这一行,可以确保你的虚拟环境变量不会泄漏到你的系统环境中。相反,它们将只存在于你的虚拟环境中。
一旦你写了解除变量的代码,就应该确保你也设置了它,所以它首先会存在。
.fusion-body .fusion-builder-column-35{width:100% !important;margin-top : 10px; margin-bottom : 10px;}.fusion-builder-column-35 > .fusion-column-wrapper {padding-top : 0px !important;padding-right : 0px !important;margin-right : 1.92%;padding-bottom : 0px !important;padding-left : 0px !margin-left : 1.92%;}@media only screen and (max-width:1024px) {.fusion-body .fusion-builder-column-35{width:100% !important;order : 0;}.fusion-builder-column-35 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-35{width:100% !important;order : 0;}.fusion-builder-column-35 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}.
.fusion-body .fusion-flex-container.fusion-builder-row-25{ padding-top : 0px;margin-top : 0px;padding-right : 0px;padding-bottom : 0px; margin-bottom : 0px;padding-left : 0px;}.
设置虚拟环境变量
你可以用之前使用Bash CLI的方式来设置虚拟环境变量,与之前练习的方式相同。然而,你不是直接在终端输入**export** 命令,而是在**activate** 脚本的结尾处作为新的一行代码添加它。
复制到剪贴板
# 脚本的其余部分 export MY_SUPER_SECRET_SECRET="OMG这太秘密了,我都不敢说!"
保存Bash脚本并关闭它。现在你可以激活你的虚拟环境了。
复制到剪贴板上
source venv/bin/activate
一旦虚拟环境被成功激活,你现在可以运行**printenv** 命令来检查当前环境中的环境变量的状态。
MY_SUPER_SECRET_SECRET 应该显示出来,你分配给它的值也应该显示出来。
在你确认激活你的虚拟环境使你的虚拟环境变量存在之后,继续停用它。再次使用**printenv** 。你的秘密应该已经消失了。
正如你所看到的,这种设置可以使你的项目特定的秘密在他们自己舒适的虚拟环境中保持安全。
.fusion-body .fusion-builder-column-36{width:100% !important;margin-top : 10px; margin-bottom : 10px;}.fusion-builder-column-36 > .fusion-column-wrapper {padding-top : 0px !important;padding-right : 0px !important;margin-right : 1.92%;padding-bottom : 0px !important;padding-left : 0px !margin-left : 1.92%;}@media only screen and (max-width:1024px) {.fusion-body .fusion-builder-column-36{width:100% !important;order : 0;}.fusion-builder-column-36 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-36{width:100% !important;order : 0;}.fusion-builder-column-36 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}.
.fusion-body .fusion-flex-container.fusion-builder-row-26{ padding-top : 0px;margin-top : 0px;padding-right : 0px;padding-bottom : 0px; margin-bottom : 0px;padding-left : 0px;}.
访问虚拟环境变量
是时候在Python中访问它们了。环境变量可以在你的Python项目中使用。为此,你需要用标准库中的Python的**os** 模块来访问它们。
复制到剪贴板
import os secret = os.environ['MY_SUPER_SECRET_SECRET'] print(secret)
只要你的虚拟环境被激活,并且定义了一个名为**MY_SUPER_SECRET_SECRET** 的环境变量,你就可以从你项目中的任何文件中运行这段代码。
如果你不希望你的脚本在没有定义环境变量时以异常结束,那么你可以使用 Python 的**dict.get()** 方法,而不是直接查找。
总结
有些秘密是注定要保持秘密的。你可以在 Bash 中设置一个变量,然后在 Python 中使用它。从Python虚拟环境中访问的环境变量允许你使用项目特定的秘密,而不会遇到意外提交到公共版本控制中的危险。
请确保将你的虚拟环境文件夹添加到你的**
.gitignore** 文件中,否则你最终会将你的秘密推送到 GitHub 中去的
在这篇博文中,你学会了如何。
- 在Bash中设置一个 变量,并删除它
- 在你的Python虚拟环境中设置特定项目的环境变量
- 在Python中访问环境变量,就像它们是Python环境变量一样
如果你有兴趣从头开始学习Python网络开发,从一开始就钻研最佳实践,请查看CodingNomads的Python工程和Django网络开发课程。
.fusion-body .fusion-builder-column-38{width:100% !important;margin-top : 10px;margin-bottom : 10px;}.fusion-builder-column-38 > .fusion-column- wrapper {padding-top : 0px !important;padding-right : 0px !important;margin-right : 1.92%;padding-bottom : 0px !important;padding-left : 0px !important;margin-left : 1.92%;}@media only screen and (max-width : 1024px) {.fusion-body .fusion-builder-column-38{width:100% !important;order : 0;}.fusion-builder-column-38 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-38{width:100% !important;order : 0;}.fusion-builder-column-38 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}.
.fusion-body .fusion-flex-container.fusion-builder-row-28{ padding-top : 0px;margin-top : 0px;padding-right : 0px;padding-bottom : 0px; margin-bottom : 0px;padding-left : 0px;}.
.fusion-button.button-2 {border-radius:0px;}我想学习Python
.fusion-body .fusion-builder-column-39{width:40% !important;margin-top : 10px;margin-bottom : 10px;}.fusion-builder-column-39 > .fusion-column-wrapper {padding-top : 0px !important;padding-right : 0px !important;margin-right : 4.8%;padding-bottom : 0px !important;padding-left : 0px !important;margin-left : 4.8%;}@media only screen and (max-width : 1024px) {.fusion-body .fusion-builder-column-39{width:100% !important;order : 0;}.fusion-builder-column-39 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-39{width:100% !important;order : 0;}.fusion-builder-column-39 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}.
.fusion-button.button-3 {border-radius:0px;}我想学习Django
.fusion-body .fusion-builder-column-40{width:40% !important;margin-top : 10px; margin-bottom : 10px;}.fusion-builder-column-40 > .fusion-column-wrapper {padding-top : 0px !important;padding-right : 0px !important;margin-right : 4.8%;padding-bottom : 0px !important;padding-left : 0px !important;margin-left : 4.8%;}@media only screen and (max-width:1024px) {.fusion-body .fusion-builder-column-40{width:100% !important;order : 0;}.fusion-builder-column-40 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}@media only screen and (max-width:640px) {.fusion-body .fusion-builder-column-40{width:100% !important;order : 0;}.fusion-builder-column-40 > .fusion-column-wrapper {margin-right : 1.92%; margin-left : 1.92%;}.
.fusion-body .fusion-flex-container.fusion-builder-row-29{ padding-top : 0px;margin-top : 0px;padding-right : 0px;padding-bottom : 0px; margin-bottom : 0px;padding-left : 0px;}.
The postPython Environment Variables:在Bash中设置一个变量首次出现在CodingNomads上。