1

I am running a python script from the IDE and it works well, however when calling it from the git-bash terminal the following errors showed.

./main.py: line 2: import: command not found
./main.py: line 3: import: command not found
./main.py: line 5: syntax error near unexpected token `'I am in main 1''
./main.py: line 5: `print('I am in main 1')'

In git-bash I run the chmod u+x main.py and chmod +x main.py lines, but it still does not work.

I am using VScode as my IDE with python3.7.9 as the interpreter, I am calling the script from the direction where it is placed in the terminal, and the script that I am testing is the following:

#!/bin/bash
import os
import sys

print('I am in main 1')

I have changed the first line for #!/bin/bash/python and #!/bin/bash/env python3... didn't work. I would appreciate it if someone could tell me what I miss or what I am doing wrong. I am new in git or using the terminal, any tip would be appreciated.

Thanks in advance.

1 Answers1

3

Not sure if you made the error in typing this question or if it's also like this in your script, but you should have #!/usr/bin/python3 or #!/bin/python3 for your system to know to execute it as a python script.

As it stands, your system is trying to execute it as a shell script with bash, which would explain why the import statements aren't being recognized.

Dharman
  • 30,962
  • 25
  • 85
  • 135
cak
  • 31
  • 4
  • It returns this: 'bad interpreter: No such file or directory'. The exact path of the interpreter is: 'C:/Users/andre/AppData/Local/Programs/Python/Python37/python.exe' – Andrés Tello Urrea Feb 13 '22 at 23:52
  • 2
    I'm sorry, I took for granted that you were on a unix-style system because of the filepaths you were trying. For a Windows system you can refer to [this question](https://stackoverflow.com/questions/7574453/shebang-notation-python-scripts-on-windows-and-linux). – cak Feb 13 '22 at 23:57
  • No worries, thanks a lot – Andrés Tello Urrea Feb 13 '22 at 23:59