Shell script syntax error when trying to activate a virtualenv and execute a python script in it

StackOverflow https://stackoverflow.com/questions/22664027

  •  21-06-2023
  •  | 
  •  

Question

I want a script that will activate a virtual environment (in both a shell and through cron) and then run the passed script with all arguments IN that virtualenv.

Here's what I have now for prod.sh:

#!/bin/bash

. $VE_DIR/Production_VE/bin/activate

python $0 "$@"

Then I try this:

hostname:~$prod.sh myscript.py -c arg1 -fu

I get this error:

  File "/home/username/prod.sh", line 3
    . $VE_DIR/Production_VE/bin/activate
    ^
SyntaxError: invalid syntax

I try switching to source:

#!/bin/bash

source $VE_DIR/Production_VE/bin/activate

python $0 "$@"

And the error changes up, but now:

  File "/home/username/prod.sh", line 3
    source $VE_DIR/Production_VE/bin/activate
           ^
SyntaxError: invalid syntax

For additional context, I want to be able to run this script on both a CentOS 6.5 VPS and Mac OS.

What's wrong with this picture?

Thank you!

Était-ce utile?

La solution

I just created an script similar to you and it run well. I thought it was really strange, you add an issue with it. But when i look at your error :

  File "/home/username/prod.sh", line 3
    source $VE_DIR/Production_VE/bin/activate
           ^
SyntaxError: invalid syntax

It really look like a Python error and not a bash error. You error is that when you do :

python $0 "$@"

You should remove the $0 because it contain the path of your bash script.

python "$@"

Autres conseils

in your bash script, $0 is "prod.sh", which is not a python script. Change

python $0 "$@"

to

python "$@"
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top