-1

I would like to create a Makefile for my python flask application in order to make it easier to use.

So I have this directory named myproject which contains:

> 07/28/2021  02:30 PM    <DIR>          . 
> 07/28/2021  02:30 PM    <DIR>          .. 
> 07/27/2021  01:30 PM             3,275 hello.py 
> 07/28/2021  02:37 PM               311 Makefile 
> 07/27/2021  11:13 AM    <DIR>         static 
> 07/22/2021  04:24 PM    <DIR>          Templates 
> 07/22/2021  02:23 PM    <DIR>          venv 
> 07/27/2021  01:31 PM    <DIR>         __pycache__

I would like to execute my python flask project easily with a Makefile.

When I want to start my project I have to set these command lines in the folder myproject:

for window

venv\Scripts\activate
set FLASK_APP=hello
flask run

for linux/mac OS

. venv/bin/activate
export FLASK_APP=hello
flask run 

I would like to put them in a Makefile and give the possibility to make it work for windows AND linus/mac os when running make.

Here is the Makefile I have:

PY = python3
VENV = venv
BIN=$(VENV)/bin/activate

#make it work on windows too
ifeq ($(OS), Windows_NT)
    BIN=$(VENV)/Scripts/activate
    PY=python
endif

all: 
    make env && \
    make debug && \
    flask run

env: 
    set FLASK_APP=hello.py

debug:
    set FLASK_ENV=development

(my python file is named hello.py)

but when I run make when I am in the folder myproject i have the following error:

'make' is not recognized as an internal or external command, operable program or batch file.

Camille BOUQUET
  • 445
  • 1
  • 6
  • 18
  • possible related ? https://stackoverflow.com/questions/23723364/windows-7-make-is-not-recognized-as-an-internal-or-external-command-operabl – cavalcantelucas Jul 28 '21 at 13:50
  • 1
    Beyond not being able to find make, this makefile cannot work as you hope. Every recipe in a makefile is run in a separate shell, and environment variable assignments are only in effect for the current process (and children of that process if they're exported). – MadScientist Jul 28 '21 at 14:00
  • thank you for your answer! what solution do you propose? – Camille BOUQUET Jul 28 '21 at 14:16
  • 1
    Not related to python, you need to install make. – Danny Varod Jul 28 '21 at 14:40

1 Answers1

0

I'm not 100% clear on exactly what you want. Also, you haven't specified whether your version of make that you are running on Windows is built to use a POSIX shell, or whether it's built to use Windows command as its shell.

But this seems to be closer:

export FLASK_APP = hello
export FLASK_ENV = development

VENV = venv

ACTIVATE = . $(VENV)/bin/activate

# make it work on windows too
ifeq ($(OS),Windows_NT)
    ACTIVATE = $(VENV)/Scripts/activate
endif

all: 
        $(ACTIVATE) && flask run

I don't know why you had $(PY) set when it was never used so I removed it.

MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • Thank you. But I have an issue `Makefile:14: *** missing separator. Stop.` I tried to check the tabs and spaces (because I know Makefile only allows tabs) and everything is fine so I don't understand why I keep having this issue. – Camille BOUQUET Jul 29 '21 at 11:30
  • Well, what's at line 14 in your makefile? – MadScientist Jul 29 '21 at 12:53
  • ` $(ACTIVATE) && flask run` this is my last line – Camille BOUQUET Jul 29 '21 at 13:49
  • Well, you don't have a TAB as the first character of that line. Maybe you seem to have a TAB there when you look at it in your editor, and your editor is converting TAB to space when it saves the file so that on disk there's no TAB. You should consider using a text editor which understands the syntax of Makefiles (or else, doesn't try to be "helpful" like this). Just to be very clear it's not true that "Makefiles only allow TABs". What is true is that every command line in a recipe must start with a TAB character as the first character on that line. – MadScientist Jul 29 '21 at 13:55
  • If you have access to a POSIX environment you can run `cat -et Makefile` and it will show the ends of lines as `$` and TAB characters as `^I` so you can see exactly what the file on disk looks like. – MadScientist Jul 29 '21 at 13:57