在我的上一个个人项目中,我需要安全地存储 API 密钥。最推荐的方法似乎是将它们存储为环境变量。由于在我的计算机上存储来自不同项目的大量环境变量很麻烦,因此我找到了一个简单的替代方案来处理这种情况。
解决方案是使用 python-dotenv 模块,它支持我们的代码使用存储在单独的 .env 文件中的变量,就像它们是常规环境变量一样。
过程非常简单...
1. 创建 .env 文件并为变量赋值。
首先我们创建一个 .env 文件来存储变量:
# Definimos las variables en el archivo .env VARIABLE1 = "Valor 1" VARIABLE2 = "Valor 2"
可以在根文件夹或项目中的其他位置创建此文件。
2.导入dotenv模块。
我们将 dotenv 模块,特别是 load_dotenv 函数导入到我们的项目中。加载 .env 的内容后,我们还必须导入 os 模块来导入环境变量:
from dotenv import load_dotenv import os
由于它不是原生 Python 模块,因此需要通过 Pip 安装,使用命令 pip install python-dotenv。
3. 恢复变量。
load_dotenv() 函数将变量作为环境变量加载到程序中。使用该模块,我们可以恢复它们的值并将它们分配给项目内的变量:
# Cargamos las variables del archivo como variables de entorno. load_dotenv() # Se almacena el valor "Valor 1" de la primera variable. VARIABLE1 = os.getenv("VARIABLE1") # Otra forma de recuperar el valor de la variable. VARIABLE2 = os.environ.get("VARIABLE2")
如果 .env 文件与执行代码的路径不同,我们必须定义该文件的位置:
load_dontenv(path="ruta/.env")
以上是Python 中环境变量的 .env 文件的详细内容。更多信息请关注PHP中文网其他相关文章!

pythonuseshybridapprace,ComminingCompilationTobyTecoDeAndInterpretation.1)codeiscompiledtoplatform-Indepententbybytecode.2)bytecodeisisterpretedbybythepbybythepythonvirtualmachine,增强效率和通用性。

theKeyDifferencesBetnewpython's“ for”和“ for”和“ loopsare:1)” for“ loopsareIdealForiteringSequenceSquencesSorkNowniterations,而2)”,而“ loopsareBetterforConterContinuingUntilacTientInditionIntionismetismetistismetistwithOutpredefinedInedIterations.un

在Python中,可以通过多种方法连接列表并管理重复元素:1)使用 运算符或extend()方法可以保留所有重复元素;2)转换为集合再转回列表可以去除所有重复元素,但会丢失原有顺序;3)使用循环或列表推导式结合集合可以去除重复元素并保持原有顺序。

fasteStmethodMethodMethodConcatenationInpythondependersonListsize:1)forsmalllists,operatorseffited.2)forlargerlists,list.extend.extend()orlistComprechensionfaster,withextendEffaster,withExtendEffers,withextend()withextend()是extextend()asmoremory-ememory-emmoremory-emmoremory-emmodifyinginglistsin-place-place-place。

toInSerteLementIntoApythonList,useAppend()toaddtotheend,insert()foreSpificPosition,andextend()formultiplelements.1)useappend()foraddingsingleitemstotheend.2)useAddingsingLeitemStotheend.2)useeapecificindex,toadapecificindex,toadaSpecificIndex,toadaSpecificIndex,blyit'ssssssslorist.3 toaddextext.3

pythonlistsareimplementedasdynamicarrays,notlinkedlists.1)他们areStoredIncoNtiguulMemoryBlocks,mayrequireRealLealLocationWhenAppendingItems,EmpactingPerformance.2)LinkesedlistSwoldOfferefeRefeRefeRefeRefficeInsertions/DeletionsButslowerIndexeDexedAccess,Lestpypytypypytypypytypy

pythonoffersFourmainMethodStoreMoveElement Fromalist:1)删除(值)emovesthefirstoccurrenceofavalue,2)pop(index)emovesanderturnsanelementataSpecifiedIndex,3)delstatementremoveselemsbybybyselementbybyindexorslicebybyindexorslice,and 4)

toresolvea“ dermissionded”错误Whenrunningascript,跟随台词:1)CheckAndAdjustTheScript'Spermissions ofchmod xmyscript.shtomakeitexecutable.2)nesureThEseRethEserethescriptistriptocriptibationalocatiforecationAdirectorywherewhereyOuhaveWritePerMissionsyOuhaveWritePermissionsyYouHaveWritePermissions,susteSyAsyOURHomeRecretectory。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver CS6
视觉化网页开发工具

WebStorm Mac版
好用的JavaScript开发工具