0

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

cd C:\repo\demo
venv\Scripts\activate
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:\repo\demo
"C:\Users\jw\AppData\Local\Programs\Python\Python310\python.exe" "venv\Scripts\activate"
"C:\Users\jw\AppData\Local\Programs\Python\Python310\python.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:\repo\demo
venv\Scripts\activate
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:\repo\demo
call "venv\Scripts\activate.bat"
python test.py
pause

but seems it didn't finish call "venv\Scripts\activate.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:\repo\demo && call "venv\Scripts\activate.bat" && python test.py && pause
J.W
  • 671
  • 7
  • 23
  • your script on the top does `venv\Scripts\activate` but in your bat you are calling python and passing the activate as a parameter. As far as I know activate is an executable, you don;t have to pass it as a python param – Sembei Norimaki Nov 10 '22 at 09:52
  • 1. ```@CD /D "C:\repo\demo" 2>NUL || Exit /B``` 2. ```@Call "venv\Scripts\activate.bat"``` 3. ```@If Not ErrorLevel 1 "%LocalAppData%\Programs\Python\Python310\python.exe" "python heatmap.py"``` 4. ```@Pause``` – Compo Nov 10 '22 at 12:20
  • @Compo the `call "venv\Scripts\activate.bat"` seems not working, please see my new test result in the edited question body. – J.W Nov 10 '22 at 22:30

2 Answers2

0

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

cd C:\repo\demo
"C:\Users\jw\AppData\Local\Programs\Python\Python310\python.exe" "venv\Scripts\activate"
python heatmap.py  <-- like this
pause

or remove python

cd C:\repo\demo
"C:\Users\jw\AppData\Local\Programs\Python\Python310\python.exe" "venv\Scripts\activate"
"C:\Users\jw\AppData\Local\Programs\Python\Python310\python.exe" "heatmap.py"
pause
Prango
  • 71
  • 5
0

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
Dharman
  • 30,962
  • 25
  • 85
  • 135
Danut Popa
  • 11
  • 5