#๐Ÿ”’ Best practices of .env using

27 messages ยท Page 1 of 1 (latest)

silver sequoia
#

Hello ! I'm discovering the use of environment variables through an .env file (!= of a venv) with Python and I'd like to have a critique for two distinct situations for its use.

โžก๏ธ Context: I have a project with the following file structure:

โ”œโ”€โ”€ annots1
โ”‚ย ย  โ””โ”€โ”€ annot.csv
โ”œโ”€โ”€ annots2
โ”‚ย ย  โ””โ”€โ”€ annot.csv
โ”œโ”€โ”€ folder2
โ”‚ย ย  โ”œโ”€โ”€ deep
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ deeper
โ”‚ย ย  โ”‚ย ย      โ””โ”€โ”€ deepest
โ”‚ย ย  โ”‚ย ย          โ””โ”€โ”€ deep_script.py
โ”‚ย ย  โ”œโ”€โ”€ question1.py
โ”‚ย ย  โ””โ”€โ”€ question2.py
โ””โ”€โ”€ utils
    โ””โ”€โ”€ upper_script.py

โžก๏ธ I've installed the python-dotenv module, which allows me to load an .env into the project (it looks for an .env file in the folders above it).

โžก๏ธ Here are the contents of my .env file:
PROJECT_PATH=/TEST
ANNOT2_PATH=/TEST/annots2/annot.csv
UPPER_SCRIPT_PATH=/TEST/utils/upper_script.py
UPPER_SCRIPT_NAME=upper_script

๐ŸŒŸ First situation: Loading a file as a resource:

import os
from pathlib import Path
from dotenv import load_dotenv
import pandas as pd

def get_annots(file_path_name:str):
    return pd.read_csv(file_path_name)

def main():
    annot_file_path_name = Path(os.getenv('PROJECT_PATH')) / "annots1" / "annot.csv"
    get_annots(annot_file_path_name)

    annot2_file_path_name = Path(os.getenv('ANNOT2_PATH'))
    get_annots(annot2_file_path_name)

if __name__ == "__main__":
    load_dotenv()
    main()

The advantage is that if I use this same code from another python file, for example the deep_script.py file, it still works!

๐ŸŒŸ Second situation:
I want to use a function from a utility python file in a folder parallel to my launch script. I don't think there's an easy way to retrieve this function. The same problem exists with a python file higher in the file hierarchy. It seems to me that I should be able to use sys.path(...).

In the question2.py script I have the following code: (not enough chars...)

toxic radishBOT
#

@silver sequoia

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

silver sequoia
#
import os
from pathlib import Path
import importlib.util
import sys
from dotenv import load_dotenv

def upper_script_load():
    module_path = Path(os.getenv('UPPER_SCRIPT_PATH'))
    module_name = os.getenv('UPPER_SCRIPT_PATH')

    path = Path(module_path)
    spec = importlib.util.spec_from_file_location(module_name, path)
    if spec is None:
        raise ImportError(f"Error while loading {module_path=}")
    module = importlib.util.module_from_spec(spec)
    sys.modules[module_name] = module
    spec.loader.exec_module(module)
    return module

def main():
    upper_script = upper_script_load()
    upper_script.hi()

if __name__ == "__main__":
    load_dotenv()

    main()
uncut hollow
uncut hollow
#

for the second, do relative imports not work for you?

#

i dont think you need importlib trickery to achieve that

#
from ..utils import upper_script```
silver sequoia
silver sequoia
uncut hollow
#

each dot goes up a directory, starting with zero

silver sequoia
uncut hollow
silver sequoia
#

oh yes

#

sorrry

uncut hollow
silver sequoia
#

it's more something like

from ...consts import WHATEVER

no ?

uncut hollow
#

nope

#

well that could work

#

thats the relative version

#

the absolute version would be from consts import WHATEVER

silver sequoia
#

It depends on the depth where I execute my script that need consts.py, doesn't it?

uncut hollow
#

not the absolute version, no

toxic radishBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.