-1

I have a Python script that interrogates a thermocouple HAT connected to a Raspberry Pi. I am now trying to use .env files so that I can use the same script across multiple systems.

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:

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?

  • Python will not automatically read .env files for you. You have three options: (a) read the file yourself, (b) set an environment variable yourself (c) use python-dotenv package. – jurez Jul 11 '22 at 15:31
  • @jurez thanks for posting, but I am already using the python-dotenv package as you can see from above. – olliecampbell Jul 11 '22 at 16:04
  • No, there is nothing in the code that would look like you are. At the very least, you need to do `load_dotenv()`. – jurez Jul 11 '22 at 16:45
  • @jurez there's no issue with the imports. It works. it was roughly covered by "IMPORTS HERE" and the fact that I'm using "os.getenv('THERMOCOUPLE_TYPE')" to read in fields from the .env. Plus the fact that the print line and pulls in the variables that are set in this file as shown by the output above. – olliecampbell Jul 11 '22 at 16:47
  • Post your imports and see the manual https://pypi.org/project/python-dotenv/. – jurez Jul 11 '22 at 16:52
  • The ones relating to the .env files are: "from dotenv import load_dotenv" / "from pathlib import Path" / "import os" Exactly as per the instructions. Either way, this isn't the help I requested but thank you. – olliecampbell Jul 12 '22 at 08:18

1 Answers1

1

Read my comments again, according to getting started you are missing the load_dotenv() call. 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.

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.
jurez
  • 4,436
  • 2
  • 12
  • 20
  • I totally, 100% understand what you are saying. I have that line elsewhere in my code, but didn't include it here, and it does what it's meant to. If it didn't, the output that I exampled wouldn't have worked. I've updated the original question, although it isn't about loading the environmental values but thank you. – olliecampbell Jul 12 '22 at 15:21
  • You can do a `print(repr(os.environ))` before and after `load_dotenv()` and you will see what it picks up, if anything. – jurez Jul 12 '22 at 16:31