简体   繁体   English

在 python 命令中使用环境变量

[英]Use environment variable in python command

I have a Python script that interrogates a thermocouple HAT connected to a Raspberry Pi.我有一个 Python 脚本,用于询问连接到 Raspberry Pi 的热电偶 HAT。 I am now trying to use .env files so that I can use the same script across multiple systems.我现在正在尝试使用 .env 文件,以便可以跨多个系统使用相同的脚本。

I'm having problem with one section of code where the environment variable is used within a method (sorry, not sure if that is the right term).我对在方法中使用环境变量的一段代码有疑问(抱歉,不确定这是否是正确的术语)。

This is the code before I try to add the environment variable:这是我尝试添加环境变量之前的代码:

from dotenv import load_dotenv #.env files
from pathlib import Path #.env files
import os
env_path = Path(os.path.expanduser('PATH/HERE/.env'))
load_dotenv(dotenv_path=env_path)
for a in range(number_of_tcHATs):
     hat_list.append([])
     hat_list[a]=daqhats.mcc134(a)
     for x in range(int(os.getenv('NUMBER_OF_THERMOCOUPLES'))):
         hat_list[a].tc_type_write(x,daqhats.TcTypes.TYPE_N)

The updated code I'm using is (only the last line changes):我正在使用的更新代码是(仅最后一行更改):

from dotenv import load_dotenv #.env files
from pathlib import Path #.env files
import os
env_path = Path(os.path.expanduser('PATH/HERE/.env'))
load_dotenv(dotenv_path=env_path)
for a in range(number_of_tcHATs):
    hat_list.append([])
    hat_list[a]=daqhats.mcc134(a)
    for x in range(int(os.getenv('NUMBER_OF_THERMOCOUPLES'))):
        print("hat_list[" + str(a) + "].tc_type_write(" + str(x) + ",daqhats.TcTypes."+ os.getenv('THERMOCOUPLE_TYPE') +")")

The pull from the .env and concatenation works as I get the following on screen:当我在屏幕上看到以下内容时,来自 .env 和连接的拉动起作用:

hat_list[0].tc_type_write(0,daqhats.TcTypes.TYPE_N)
hat_list[0].tc_type_write(1,daqhats.TcTypes.TYPE_N)
hat_list[0].tc_type_write(2,daqhats.TcTypes.TYPE_N)
hat_list[0].tc_type_write(3,daqhats.TcTypes.TYPE_N)
hat_list[1].tc_type_write(0,daqhats.TcTypes.TYPE_N)
hat_list[1].tc_type_write(1,daqhats.TcTypes.TYPE_N)
hat_list[1].tc_type_write(2,daqhats.TcTypes.TYPE_N)
hat_list[1].tc_type_write(3,daqhats.TcTypes.TYPE_N)

But it never runs the code itself.但它从不运行代码本身。 How can I achieve this?我怎样才能做到这一点?

Read my comments again, according to getting started you are missing the load_dotenv() call.再次阅读我的评论,根据入门,您缺少load_dotenv()调用。 Just importing it is not enough, you need to call it.仅仅导入它是不够的,你需要调用它。 Without that, os.getenv() will only return variables from OS environment and as such is not aware of any .env files.没有它, os.getenv()只会从操作系统环境返回变量,因此不知道任何 .env 文件。

from dotenv import load_dotenv

load_dotenv()  # take environment variables from .env.

# Code of your application, which uses environment variables (e.g. from `os.environ` or
# `os.getenv`) as if they came from the actual environment.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 子进程导出环境变量(使用其他环境变量) - Python subprocess export environment variables (which use other environment variable) 使用bash命令的输出作为VSCode中环境变量的输入 - Use output of a bash command as an input to environment variable in VSCode Django 通过命令行设置环境变量并在迁移中使用 - Django set environment variable via command line and use in Migrations Python 命令在从环境变量 PATH 中删除后仍然有效 - Python command working even after deleting from environment variable PATH 如何在 Django 管理命令(python 脚本)中设置环境变量 - How to set an environment variable inside a Django management command (python script) 模块导入前需要Python命令行导入环境变量 - Python command line import of environment variable required before module import Python Popen:通过sudo将环境变量传递给以其他用户身份运行的命令 - Python Popen: pass environment variable to command running as another user with sudo 如何在 python 环境中使用 Fonttools 的 pyftsubset,而不是从命令行 - How to use pyftsubset of Fonttools inside of the python environment, not from the command line 如何在sql命令中使用python变量 - How to use python variable in sql command python在command.replace中使用变量 - python use variable inside command.replace
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM