简体   繁体   English

生成文件中意外标记附近的语法错误

[英]syntax error near unexpected token in makefile

I'm trying to create a template in Makefile to reuse Python virtualenv.我正在尝试在 Makefile 中创建一个模板来重用 Python virtualenv。 in Makefile I define:在 Makefile 中我定义:

ENV_CREATE ?= $(shell python3 -m virtualenv venv) and in target: ENV_CREATE ?= $(shell python3 -m virtualenv venv)和目标:

set_up:
    $(ENV_CREATE) ; \
    . venv/bin/activate

As a result of Makefiule target execution I'm getting作为 Makefile 目标执行的结果,我得到了

/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).ENV_CREATE ?= $(shell...)右侧似乎是非递归地(即立即)评估的。 So the ENV_CREATE variable is assigned the result of this shell script: [created ... .所以ENV_CREATE变量被分配了这个 shell 脚本的结果: [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 .在您的配方中,您使用此 make 变量( $(ENV_CREATE) )的扩展作为 shell 语法,虽然它不是 shell 语法,但它是python3 -m virtualenv venv的输出消息。

There is absolutely no point in using the shell make function in a recipe which is already... a shell script.在已经是……shell 脚本的配方中使用shell make 函数绝对没有意义。 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. Make 将在将配方传递给 shell 之前对其进行扩展。 So what will be passed to the shell is:那么将传递给shell的是:

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.但请注意,将采购 ( . venv/bin/activate ) 作为配方的最后一个命令可能不会做任何有用的事情。

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.似乎我不需要$(shell python 3...)构造。 I have defined variable with Make commands(no shell) and injected it as one-liner:我已经使用 Make 命令(无外壳)定义了变量并将其作为单行注入:

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.)现在我可以以相同的方式在其他目标中重用$(ENV_CREATE) (集成测试、单元测试等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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