Current Path variable in .env file
Asked Answered
S

2

6

Is there a way to refer to the current directory in a .env file?

Trying to point the location of a config file from userprofile, to the current path. For example,

CONFIG_FILE=${USERPROFILE}\.configs\config.cfg

I would like to set it to,

CONFIG_FILE=${CURRENTPATH}\.configs\config.cfg
Summerly answered 26/3, 2019 at 13:33 Comment(14)
maybe using pwdCycloid
I'll give that one a shot!, I've tried, currentpath, currentdir, currentdirectory, even . (dot)Summerly
pwd didn't workSummerly
why dont you put the whole code.Survey
Which operating system are you using?Twelfth
os.path.abspath(file) gives the location of current file from where the code is running. os.path.dirname( os.path.abspath(file )) would give the directory where you have placed your .py file . DO not forget to import osSurvey
I am on windows 10Summerly
@Summerly I hope this worksSurvey
@Summerly my code will work irrespective of platformSurvey
yeah close the questionSurvey
@RoushanSingh I was trying to have it load when I run pipenv shell|run, if I can't get that to work I will have to create a load script in my root directory.Summerly
use subprocesses moduleSurvey
@Summerly by the way i am still not getting what you have asked . seriouslySurvey
@RoushanSingh when run your pipenv shell is sets environment variables from a .env file, I wanted to point a config from from one location to another while I have an active pipenv shellSummerly
I
2

Running my program w/ pipenv on Ubuntu, I'm able to do e.g.:

SETUP_CFG=${PWD}/setup.cfg

And in Python I can do:

import os
from pathlib import Path

assert os.environ.get("SETUP_CFG") == (Path.cwd() / "setup.cfg").as_posix()
Incompletion answered 10/1, 2021 at 2:44 Comment(0)
M
0

Yes, 'pwd' like the below in .env file for your example should work ( I tested it on Mac)

CONFIG_FILE=${PWD}/.configs/config.cfg
Morgenthaler answered 19/8, 2024 at 23:39 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.