.env not being loaded when run from CLI but is when run from VSCode

Question:

I’ve finished up writing an application in Python, which is working fine when running from VSCode, however, when trying to run from the terminal, it doesn’t start.

The error I get is:

    SQLConnString = 'Driver={ODBC Driver 17 for SQL Server};Server=' + SQLServer+ ';Database=' + SQLDatabase + ';UID='+ SQLLogin +';PWD=' + SQLPassword
TypeError: can only concatenate str (not "NoneType") to str

This is obviously quite clear, it’s stating that, at least, one of those variables (SQLServer, SQLDatabase, SQLLogin or SQLPassword) have the value None, and so concatenation fails. That much I understand.

What I don’t understand, however, is that those variables are defined, and loaded prior:

SQLServer = os.getenv('SQL_SERVER')
SQLDatabase = os.getenv('SQL_DATABASE')
SQLLogin = os.getenv('SQL_LOGIN')
SQLPassword = os.getenv('SQL_PASSWORD')

And, like I mentioned, if I’m in VSCode, and run the python file, then the file starts up fine, and works; no errors. So there is something that is different from me debugging it in VSCode, and running python3 ./mypythonfile.py in bash.

What am I missing here, why is the environment file’s value not pulled into the variables when I run it from command line, but are when I run it from VSCode. That doesn’t make sense. Why is the behaviour different; with one non-functional and the other works fine.

Unfortunately searches of things like "python3 not loading .env file in cli" lead me to sources that tell me how to create/use a .env files; I have a file, and it works but seems to just not in CLI and I don’t understand why. Clearly I’m missing something obvious, but when it works in VSCode, I expect that it should work from CLI.

I’ve tried this on 2 different devices as well, same result. Runs fine in VSCode, but can’t be started from Command Line.

Asked By: Mogo

||

Answers:

Found out the answer. I import other files, which also use data from the environment file. In VSCode, when debugging, you don’t need load_dotenv() in the individual files, you do however, when running from python3. I assume that is a bug in the debugger.

Answered By: Mogo

I had a similar issue (ran in VSCode, failed when run from the CLI), but I was already using load_dotenv(). The root cause of my issue was that my python file that ran the load_dotenv() was in a subfolder under the main folder where the .env file was located. So, the solution was to pass in the path to the .env file to load_dotenv, simply load_dotenv(path_to_env_file). This works in both VSCode and from the CLI.

I’m not exactly sure what goes on behind the scenes in VSCode that enables it to avoid this issue.

Answered By: DaveB

I’ve came across this same issue today.
Once I undestood that the problem was related to the root directory the dubugger was set, it was straight forward to fix it:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}", // <- the secret is here
            "console": "integratedTerminal",
            "justMyCode": true
        }
    ]
}

Just add "cwd" as the ${fileDirName} so you set the root directory as the file directory itself, then all the relative paths are going to be fixed as well.

Rederences:

Answered By: guilfer
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.