简体   繁体   English

Bash脚本在virtualenv中运行python脚本

[英]Bash script to run python script in virtualenv

I'm creating PHP application which need to run python script. 我正在创建需要运行python脚本的PHP应用程序。 I must use virtualenv. 我必须使用virtualenv。 In project catalog i've created env directory and inside created virtual env named python . 在项目目录中,我创建了env目录,并在内部创建了名为python虚拟环境。 In another catalog i've put script i'd like to run and this bash script: 在另一个目录中,我放置了我想运行的脚本和以下bash脚本:

#!/bin/bash

app="$(pwd)/../"
pythonEnv="${app}env/python/"
source ${pythonEnv}"bin/activate"
"${pythonEnv}bin/python" "${app}scripts/script.py arg1 arg2"

But when i run this script (using terminal so far) i got this error: 但是当我运行此脚本(到目前为止使用终端)时,出现此错误:

/var/www/project/scripts/../env/python/bin/python: can't open file '/var/www/project/scripts/../scripts/script.py arg1 arg2': [Errno 2] No such file or directory

Of course there is script.py in scripts directory and has right access permissions: 当然,在脚本目录中有script.py并具有正确的访问权限:

-rwxrwxr-x

What am I doing wrong? 我究竟做错了什么?

Reading your error message, it appears that the interpreter thinks that the file is /var/www/project/scripts/../scripts/script.py arg1 arg2 and not simply /var/www/project/scripts/../scripts/script.py . 阅读您的错误消息后,似乎解释器认为该文件是/var/www/project/scripts/../scripts/script.py arg1 arg2而不是/var/www/project/scripts/../scripts/script.py You should separate arg1 and arg2 from the file name. 您应该将arg1arg2与文件名分开。

Also, I think you can just write python instead of "${pythonEnv}bin/python" once you activated your virtual environment. 另外,我认为一旦激活虚拟环境,您就可以编写python而不是"${pythonEnv}bin/python"

Due to your quoting, you are trying to call "${app}scripts/script.py arg1 arg2" as the script name - which does not exist. 由于引用原因,您试图将"${app}scripts/script.py arg1 arg2"作为脚本名称-不存在。 You can change your script to fix it: 您可以更改脚本来修复它:

${pythonEnv}bin/python ${app}scripts/script.py arg1 arg2

Adjust your quoting as needed, but keep it specific. 根据需要调整报价,但要保持具体。 For example, if you need to quote your pythonEnv variable do "${pythonEnv}" . 例如,如果需要引用pythonEnv变量,请执行"${pythonEnv}"

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

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