I am using a .bat
file to:
- Set environment variables
- Kick off a Python program (that uses these environment variables)
@ECHO OFF
REM TODO: figure out why Python can't see this
SET MY_ENV_VAR="foo"
CALL C:\code\repo\venv\Scripts\activate
CD /d C:\code\repo
@ECHO ON
REM kick off Python, where os.getenv can't see MY_ENV_VAR
CMD /k run_python_console_script
If you're curious, the run_python_console_script
kicks off a command made via an Entry Point.
When I run os.getenv("MY_ENV_VAR")
it doesn't return "foo"
, it returns None
. Why is this?
I don't want to use SETX
(docs), I want the environment variable scoped to just this batch script.
Other Answers
How to access Batch Script variables in python? and Python Subprocess Does not Get Environment Variable from Bat File want to invoke a .bat
script as a subprocess. I don't want to do this, the .bat
script is my parent process.
virtualenvwrapper
supports a postactivate
script (see
How can I use a postactivate script using Python 3 venv? and Set specific environment variables activating a Python virtual environment) but I am not using virtualenvwrapper
.