I'm trying to create a template in Makefile to reuse Python virtualenv. in Makefile I define:
ENV_CREATE ?= $(shell python3 -m virtualenv venv)
and in target:
set_up:
$(ENV_CREATE) ; \
. venv/bin/activate
As a result of Makefiule target execution I'm getting
/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `echo created virtual environment CPython3.7.9.final.0-64 in 333ms creator CPython3Posix(dest=/Users/marian/Work/git/sigma/sphere/sphere-data-platform/venv, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/marian/Library/Application Support/virtualenv) added seed packages: pip==21.1.2, setuptools==57.0.0, wheel==0.36.2 activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator ; . venv/bin/activate python3 -m pip install -r requirements.txt -r requirements-dev.txt ; '
make: *** [setup] Error 2
What am I doing wrong?
In ENV_CREATE ?= $(shell...)
the right hand side seems to be evaluated non-recursively (that is, immediately). So the ENV_CREATE
variable is assigned the result of this shell script: [created ...
.
In your recipe you use the expansion of this make variable ( $(ENV_CREATE)
) as shell syntax, while it is not shell syntax, it is the output message of python3 -m virtualenv venv
.
There is absolutely no point in using the shell
make function in a recipe which is already... a shell script. Try:
ENV_CREATE ?= python3 -m virtualenv venv
set_up:
$(ENV_CREATE) ; \
. venv/bin/activate
Make will expand the recipe before passing it to the shell. So what will be passed to the shell is:
python3 -m virtualenv venv ; . venv/bin/activate
But note that sourcing ( . venv/bin/activate
) as the last command of a recipe will probably not do anything useful.
Why not just use another target?
venv:
python3 -m virtualenv venv
set_up: venv
. venv/bin/activate
This way, you only create the virtual environment if it doesn't already exist.
Managed to find the best way. Seems like i don't need a $(shell python 3...)
construction. I have defined variable with Make commands(no shell) and injected it as one-liner:
ENV_CREATE := python3 -m virtualenv venv ; . venv/bin/activate ;
setup:
$(ENV_CREATE) python3 -m pip install -r requirements.txt
now I can re-use $(ENV_CREATE)
in other targets the same way(integration tests, unit tests, etc.)
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.