skip to Main Content

Try to create a Windows .bat file to achieve the below function:

cd C:repodemo
venvScriptsactivate
python test.py

In Visual Studio Code terminal window, I can run the above lines without issue.

Created a .bat file as below:

cd C:repodemo
"C:UsersjwAppDataLocalProgramsPythonPython310python.exe" "venvScriptsactivate"
"C:UsersjwAppDataLocalProgramsPythonPython310python.exe" "python test.py"
pause

When double click the above .bat file to run it, end with error:

if [ "${BASH_SOURCE-}" = "$0" ]; then

SyntaxError: cannot assign to literal here. Maybe you meant ‘==’ instead of ‘=’?

Also tried the below .bat code, not working either:

cd C:repodemo
venvScriptsactivate
python test.py
pause

How to correct the .bat file to make it work?

======================================

Based on @Compo’s comment, tried the below version and it successfully executed python test.py:

cd C:repodemo
call "venvScriptsactivate.bat"
python test.py
pause

but seems it didn’t finish call "venvScriptsactivate.bat", the command line window shows as below:

enter image description here

When manually run the code, it will prefix the path with (venv) as below which shows the proper result:

enter image description here

============================================

UPDATE:

The below .bat version works now, an answer from this question

cd C:repodemo && call "venvScriptsactivate.bat" && python test.py && pause

2

Answers


  1. You should either remove "C:…python.exe" from the second line:

    cd C:repodemo
    "C:UsersjwAppDataLocalProgramsPythonPython310python.exe" "venvScriptsactivate"
    python heatmap.py  <-- like this
    pause
    

    or remove python

    cd C:repodemo
    "C:UsersjwAppDataLocalProgramsPythonPython310python.exe" "venvScriptsactivate"
    "C:UsersjwAppDataLocalProgramsPythonPython310python.exe" "heatmap.py"
    pause
    
    Login or Signup to reply.
  2. Try this:

    cd c:python
    python scripts/test.py <-- here, you can replace "test" with the name of the Python program(script) you want to execute you
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search