You can create a batch file (.bat) with the following content:
"C:\Program Files\Git\git-bash.exe" -c "python myScript.py"
Yu can also make this available for all .py file by editing your regedit as explained here: Adding a context menu item in Windows for a specific file extension with the following command:
"C:\Program Files\Git\git-bash.exe" -c "python \"%1\""
windows - Python not working in the command line of git bash - Stack Overflow
Python 3 won't run from the Git Bash command line - Stack Overflow
git - Run bash command line script in python - Stack Overflow
why does the python command not work in gitbash app windows??
Temporary solution
Just enter this in your git shell on windows - > alias python='winpty python.exe', that is all and you are going to have alias to the python executable. This alias will be valid for the duration of the shell session.
winpty is a Windows software package providing an interface similar to a Unix pty-master for communicating with Windows console programs.
Permanent solution
Add the command to your .bashrc in the users home directory. You can use the CLI or a text editor:
Using CLI
This can be accomplished from git bash like so:
echo "alias python='winpty python.exe'" >> ~/.bashrc
which will create .bashrc in the current users home directory if the file doesn't exist or append the alias to the end of .bashrc if it does.
Using a text editor
Alternatively, you could first create a .bashrc. Depending on your file manager, this may be easier to accomplish in git bash like so:
cd ~
touch .bashrc
At which point you can open .bashrc in your prefered text editor and add it there.
To apply the change, either use the command source .bashrc or restart the shell.
Update
Newer versions of Git no longer use .bashrc but instead use .bash_profile. Conda also uses this profile when initializing, so be sure not to overwrite or delete the initialization block. See more here: Git for Windows doesn't execute my .bashrc file.
I don't see next option in a list of answers, but I can get interactive prompt with "-i" key:
$ python -i
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55)
Type "help", "copyright", "credits" or "license" for more information.
>>>
"One caveat if using Git Bash with MinTTY: python doesn't go into interactive mode so a solution is to force it to start that way: echo alias python=\"python -i\" >> ~/.bash_profile"]
For more details
I had the same issue and using "python -i" solved it.
The comment of adding it to the .bashrc is likely the best answer.
Type:
- open terminal
- cd ~ (hit enter)
- vim .bashrc (hit enter)
- Hit I to insert on one of the lines and type:
- alias python3='c:/Python34/python.exe'
- hit esc (enters vim command mode)
- type :wq (write and quit)
- hit enter
Now type python3 in gitbash hit enter and there you go!
when i type in python hello.py, it shows Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Apps > Advanced app settings > App execution aliases.
Getting python run in git bash you simply can type winpty python or you can use python -i
Note: if you use winpty python you can exit python with CTRL + Z or exit(), but if you used python -i, you only can exit with exit().
Actually I was having the same problem. The only thing is that when you enter the Python command prompt with python -i, you can only exit it with quit()
Adapting the PATH should work. Just tried on my Git bash:
$ python --version
sh.exe": python: command not found
$ PATH=$PATH:/c/Python27/
$ python --version
Python 2.7.6
In particular, only provide the directory; don't specify the .exe on the PATH ; and use slashes.
That command did not work for me, I used:
$ export PATH="$PATH:/c/Python27"
Then to make sure that git remembers the python path every time you open git type the following.
echo 'export PATH="$PATH:/c/Python27"' > .profile
Reason Popen accepts list of arguments so it should look like as follows
import subprocess
p = subprocess.Popen(["C:\Program Files\Git\git-bash.exe","C:/Users/userName/Documents/dev/basicSc.sh"],
bufsize=-1,
executable=None,
stdin=None,
stdout=None,
stderr=None,
preexec_fn=None,
close_fds=True,
shell=False,
cwd="C:/Users/userName/Documents/dev",
)
use p.wait() to ensure that your script ran successfully.....However other way can be to capture all the progress of script to any logger file then check whether it is doing what it is supposed to do or not....... You can use CHECK_OUTPUT as well because it returns return code which can be used to assert whether your script ran successfully or not....Keep this in Try-Except catch to capture exception
I managed to create a folder in Windows in which I store my Python scripts. I can then simply start these files using the command prompt:
Example:
d:\scripts\hello.py
Command Prompt:
C:\>hello.py
hello
I also installed "Windows Terminal" and Git. During the Git installation I clicked "Add a Git Bash Profile to Windows Terminal".
When I start Terminal and enter hello.py in the Git Bash as above, I get the following error message:
$ hello.py
/d/scripts/hello.py: line 1: syntax error near unexpected token `"hello"'
/d/scripts/hello.py: line 1: `print("hello")'
$ hello
bash: hello: command not found