简体   繁体   中英

How do I set an environment variable in Makefile for Windows?

I have the following Makefile:

PYTHON = python
.DEFAULT_GOAL = help
help:
    @echo ------------------------------Makefile for Flask app------------------------------
    @echo USAGE:
    @echo   make dependencies           Install all project dependencies
    @echo       make docker             Run Docker
    @echo       make env                Set environment variables
    @echo       make run                Run Flask app
    @echo       make test               Run tests for app
    @echo ----------------------------------------------------------------------------------
dependencies:
    @pip install -r requirements.txt
    @pip install -r dev-requirements.txt
docker:
    docker compose up

env:
    @set CS_HOST_PORT=5000
    @set CS_HOST_IP=127.0.0.1
    @set DATABASE_URL=postgresql://lv-python-mc:575@127.0.0.1:5482/Realty_DB
    @set REDIS_IP=127.0.0.1
    @set REDIS_PORT=6379
run:
    ${PYTHON} app.py    test:
    @${PYTHON} -m pytest

The set command doesn't work and the environment variables aren't set, what may be the problem?

You can certainly set environment variables that will be in effect for programs make will invoke . But make cannot set environment variables for shells that invoke make . So if your makefile runs a program then you can set an environment variable in your makefile that will be visible in that program.

This has nothing to do with make, by the way. This is a limitation (or feature, depending on your perspective) of the operating system. Try this experiment:

  • Open a terminal.
  • Run set FOO=bar
  • Run echo %FOO% . See that it prints bar .
  • From that same terminal start a new shell by running cmd.exe
  • Now here run set FOO=nobar
  • Run echo %FOO% . See that it prints nobar .
  • Now exit the new shell by running exit
  • Now run echo %FOO%

You'll see that instead of nobar , it still prints bar . That's because the OS does not allow a child program to modify the environment of its parent program.

So, there's nothing make can do about this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM