Bash freezes when calling Ipython [duplicate] - python

Python will not run in git bash (Windows). When I type python in the command line, it takes me to a blank line without saying that it has entered python 2.7.10 like its does in Powershell. It doesn't give me an error message, but python just doesn't run.
I have already made sure the environmental variables in PATH included c:\python27. What else can I check?
A session wherein this issue occurs looks like the following:
user#hostname MINGW64 ~
$ type python
python is /c/Python27/python
user#hostname MINGW64 ~
$ python
...sitting there without returning to the prompt.

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.
>>>

This is a known bug in MSys2, which provides the terminal used by Git Bash. You can work around it by running a Python build without ncurses support, or by using WinPTY, used as follows:
To run a Windows console program in mintty or Cygwin sshd, prepend console.exe to the command-line:
$ build/console.exe c:/Python27/python.exe
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 10 + 20
30
>>> exit()
The prebuilt binaries for msys are likely to work with Git Bash. (Do check whether there's a newer version if significant time has passed since this answer was posted!).
As of Git for Windows 2.7.1, also try using winpty c:Python27/python.exe; WinPTY may be included out-of-the-box.

I am windows 10 user and I have installed GIT in my system by just accepting the defaults.
After reading the above answers, I got 2 solutions for my own and these 2 solutions perfectly works on GIT bash and facilitates me to execute Python statements on GIT bash.
I am attaching 3 images of my GIT bash terminal. 1st with problem and the latter 2 as solutions.
PROBLEM - Cursor is just waiting after hitting python command
SOLUTION 1
Execute winpty <path-to-python-installation-dir>/python.exe on GIT bash terminal.
Note: Do not use C:\Users\Admin like path style in GIT bash, instead use /C/Users/Admin.
In my case, I executed winpty /C/Users/SJV/Anaconda2/python.exe command on GIT bash
Or if you do not know your username then execute winpty /C/Users/$USERNAME/Anaconda2/python.exe
SOLUTION 2
Just type python -i and that is it.
Thanks.

Try python -i instead of python, it's a cursor thing.

Git Bash Workaround- Launch Python 2 & Python 3 with aliases
HI. This is (for me) the best solution to run both Python (Python 2.7 and Python 3.x) directly from Git Bash on Win 10 => adding aliases into the aliases file that Git Bash uses for.
Git Bash aliases file is aliases.sh. It is located in:
C:\path where you installed Git\etc\profile.d\aliases.sh
1) Open (with a text editor like Atom or other) the aliases.sh
for ex: in my case the file is in C:\Software\Develop\Git\etc\profile.d\aliases.sh
2) Add your alias for Python
In my case the python.exe are installed in:
C:\Networking\Network Automation\Python 2.7\python.exe
C:\Networking\Network Automation\Python 3.7\python.exe
So you must create 2 aliases, one for Python 2 (I named python2) and the other for Python 3 (I named just python)
Git Bash uses linux file structure so you need to change the "\" for "/"
and if you have a path like my example Network Automation you put it with " "
"Network Automation", for ex.
winpty is the magic command that will call the executable.
So add these lines at the beginning of aliases.sh
alias python2='winpty C/Networking/"Network Automation"/"Python 2.7"/python.exe'
alias python='winpty C/Networking/"Network Automation"/"Python 3.7"/python.exe'
3) Add or Modify other aliases (if you want)
I modified also the ll alias to show all the files and in a human readable list:
alias ll='ls -lah'
4) Save the aliases.sh file
5) OK!!! close and relaunch your Git Bash
Now, permanently you could launch both Python directly from Git shell just writting
$ python -> launch Python 3
$ python2 -> launch Python 2
$ ll -> enters a ls -lah to quickly show your detailed file list
Cheers, Harry

2 workarounds, rather than a solution: In my Git Bash, following command hangs and I don't get the prompt back:
% python
So I just use:
% winpty python
As some people have noted above, you can also use:
% python -i
2020-07-14: Git 2.27.0 has added optional experimental support for pseudo consoles, which allow running Python from the command line:
See attached session.

In addition to the answer of #Charles-Duffy, you can use winpty directly without installing/downloading anything extra. Just run winpty c:/Python27/python.exe. The utility winpty.exe can be found at Git\usr\bin. I'm using Git for Windows v2.7.1
The prebuilt binaries from #Charles-Duffy is version 0.1.1(according to the file name), while the included one is 0.2.2

type: 'winpty python' and it will work
gitbash has some issues when running any command that starts with python. this goes for any python manage.py commands as well. Always start with 'winpty python manage.py' At least this is what works for me. Running Windows 10.

You can change target for Git Bash shortcut from:
"C:\Program Files\Git\git-bash.exe" --cd-to-home
to
"C:\Program Files\Git\git-cmd.exe" --no-cd --command=usr/bin/bash.exe -l -i
This is the way ConEmu used to start git bash (version 16). Recent version starts it normally and it's how I got there...

In addition to #Vitaliy Terziev answer
try touch .bash_profile and then add alias into the file.

I am using MINGW64 via Visual Studio Code on Windows 10 and trying to install node-sass (which requires python2). I followed felixrieseberg/windows-build-tools #56 on Github which solved my issue.
This is a special case, but I'm posting in case someone has the same problem:
npm --add-python-to-path='true' --debug install --global windows-build-tools
This installs python and other required build tools to %USERPROFILE%\.windows-build-tools\python27.

For python version 3.7.3 in vscode with gitbash as the default terminal I was dealing with this for a while and then followed #Vitaliy Terziev advice of adding the alias to .bashrc but with the following specification:
alias python=’“/c/Users/my user name/AppData/Local/Programs/Python/Python37/python.exe”’
Notice the combination of single and double quotes because of “my user name” spaces.
For me, "winpty" couldn't resolve python path in vscode.

Type the command PY instead of Python. Invoking the Interpreter
(python.org).

I know this is an old post, but I just came across this problem on Windows 10 running Python 3.8.5 and Git 2.28.0.windows.1
Somehow I had several different 2.7x versions of Python installed as well. I removed every version of Python (3x and 2x), downloaded the official installer here, installed 3.8.5 fresh (just used the defaults) which installed Python 3.8.5 at this location:
C:\Users\(my username)\AppData\Local\Programs\Python\Python38
Then to get the command python to work in my git bash shell, I had to manually add the path to Python38 to my path variable following the instructions listed here. This is important to note because on the python installer at the bottom of the first modal that comes up it asks if you want to add the python path to your PATH environment variable. I clicked the checkbox next to this but it didn't actually add the path, hence the need to manually add the path to my PATH environment variable.
Now using my gitbash shell I can browse to a directory with a python script in it and just type python theScriptName.py and it runs no problem.
I wanted to post this because this is all I had to do to get my gitbash shell to allow me to run python scripts. I think there might have been some updates so I didn't need to do any of the other solutions listed here. At any rate, this is another thing to try if you are having issues running python scripts in your gitbash shell on a Windows 10 machine.
Enjoy.

Another example of this issue is using the AWS Elastic Beanstalk command line interface (awsebcli, eb cli) from the git bash (MINGW64, Mintty) in windows (using git version 2.19.0.windows.1).
I'm just posting this because it took me a while to end up here, searching for eb-cli specific issues.
Commands such as eb init or eb config save, which require user input, appear to cause a freeze/hang. In reality I guess the console is not updated with the text requesting user input. Moreover, eb deploy only updates the console text after the command has finished, so I don't get to see progress updates until finished.
As mentioned in the git for windows release notes (for v2.19.0) and e.g. in Xun Yang's answer, a workaround is to run
winpty eb <command> (instead of just eb <command>)
A alternative, as suggested in this git for windows issue, could be to use the windows native console instead of mintty (option during git installation).

The one worked for me is as mentioned earlier in these great answers above is the alias as follows:
(I'm using anaconda, so first find where is the python path, then add it into the alias on git bash).
1. on anaconda terminal I run: where python
2. on git bash I run: alias python='winpty "C:\ProgramData\Anaconda3\envs\your_env_name\python.exe"'
3. Done. Python is defined inside the git Bash using the alias.
Thanks to (Vitaliy Terziev & hygull) for their very helpful answers.

python.exe -i works but got issues in exiting from the interactive mode by sending "^Z" (CTRL+Z). So, seem better to use winpty python.exe in Git Bash for Windows.
Use ~/bin directory to make a wrap/reference file (like ~/bin/python) which will be accessible everywhere (you may use different version reference like ~/bin/python37).
Code inside the file:
#!/usr/bin/env bash
# maybe declare env vars here like
# export PYTHONHOME=/c/Users/%USERNAME%/.python/Python36
# export PATH="${PATH}:/c/Users/%USERNAME%/.python/Python36"
# replace %USERNAME%,
# or use "~" instead of "/c/Users/%USERNAME%" if it works
winpty /c/Users/%USERNAME%/.python/Python36/python.exe ${#}
I just don't like these "magic" aliases which you're always forgetting where it's coming from, and sometimes leads to issues in some cases.
Use ~/bin/python file and -i parameter:
#!/usr/bin/env bash
if [ -z "${#}" ]; then
# empty args, use interactive mode
/c/Users/%USERNAME%/.python/Python36/python.exe -i
else
/c/Users/%USERNAME%/.python/Python36/python.exe ${#}
fi

if you run a Windows PowerShell command and an error occurs, the error record will be appended to the “automatic variable” named $error.
You can use the $error variable to find the errors, in the same PowerShell session.
The $Error variable holds a collection of information, and that’s why using $Error[0] can get to your error message objects. Also the $Error[0] variable will hold the last error message encountered until the PowerShell session ends.

Have a look at this answer:
Git Bash won't run my python files?
the path in Git Bash should be set like this:
PATH=$PATH:/c/Python27/

Related

MacOs Monterey Python3 as default

I am currently trying to change my default Python version to Python3. This proves to be harder than expected.
I have already tried the following things:
I have tried to change the alias by doing alias python python3 but this seems to just change it for the running Terminal session
I have installed Python3 again with Homebrew and tried to change the directory within the zshellruncommand by defining it myself and editing it in vim
to be honest I don't really understand the process and just followed along with a tutorial but it didn't work.
I tried the command ln -s -f /opt/homebrew/bin/python3 /usr/bin/python to change the directories but it returned ln: /usr/bin/python: Operation not permitted
to deal with this problem I have already given the Terminal full access to the local harddrive but it is still not working properly
Hoping for some advise,
thanks in advance
First make sure the installed version is linked:
brew link python
then on your .bash_profile or .bashrc (.zshrc if you're using zsh) write this:
export PATH="/usr/local/opt/python#X.Y/libexec/bin:$PATH"
change X and Y to your python version
I think it's easier to just change your user shell environment instead of changing the system level environment.
vi ~/.bash
Then type
alias python=python3
Save your file by pressing Esc -> type :wq
Update your shell environment
source ~/.bash
Check your Python version.
python -V
Final result
Python 3.9.10
This works in my past few versions MacOS.

How to set python 3.x priority? [duplicate]

I installed Python 2.6 and Python 3.1 on Windows 7 and set environment variable: path = d:\python2.6.
When I run python in cmd, it displays the python version 2.6, which is what I want!
But, when I wrote a script in a bat file and ran it, the displayed python version was 3.1.
import sys
print (sys.version)
What's going on here?
This is if you have both the versions installed.
Go to This PC → Right-click → Click on Properties → Advanced System Settings.
You will see the System Properties. From here navigate to the Advanced Tab -> Click on Environment Variables.
You will see a top half for the user variables and the bottom half for System variables.
Check the System Variables and double-click on the Path (to edit the Path).
Check for the path of Python(which you wish to run i.e. Python 2.x or 3.x) and move it to the top of the Path list.
Restart the Command Prompt, and now when you check the version of Python, it should correctly display the required version.
The Python installer installs Python Launcher for Windows. This program (py.exe) is associated with the Python file extensions and looks for a "shebang" comment to specify the python version to run. This allows many versions of Python to co-exist and allows Python scripts to explicitly specify which version to use, if desired. If it is not specified, the default is to use the latest Python version for the current architecture (x86 or x64). This default can be customized through a py.ini file or PY_PYTHON environment variable. See the docs for more details.
Newer versions of Python update the launcher. The latest version has a py -0 option to list the installed Pythons and indicate the current default.
Here's how to check if the launcher is registered correctly from the console:
C:\>assoc .py
.py=Python.File
C:\>ftype Python.File
Python.File="C:\Windows\py.exe" "%1" %*
Above, .py files are associated with the Python.File type. The command line for Python.File is the Python Launcher, which is installed in the Windows directory since it is always in the PATH.
For the association to work, run scripts from the command line with script.py, not "python script.py", otherwise python will be run instead of py. If fact it's best to remove Python directories from the PATH, so "python" won't run anything and enforce using py.
py.exe can also be run with switches to force a Python version:
py -3 script.py # select latest Python 3.X version to be used.
py -3.6 script.py # select version 3.6 specifically.
py -3.9-32 script.py # select version 3.9 32-bit specifically.
py -0 # list installed Python versions (latest PyLauncher).
Additionally, add .py;.pyw;.pyc;.pyo to the PATHEXT environment variable and then the command line can just be script with no extension.
Running 'py' command will tell you what version you have running. If you currently running 3.x and you need to switch to 2.x, you will need to use switch '-2'
py -2
If you need to switch from python 2.x to python 3.x you will have to use '-3' switch
py -3
If you would like to have Python 3.x as a default version, then you will need to create environment variable 'PY_PYTHON' and set it's value to 3.
If you know about Environment variables and the system variable called path, consider that any version of any binary which comes sooner, will be used as default.
Look at the image below, I have 3 different python versions but python 3.8 will be used as default since it came sooner than the other two. (In case of mentioned image, sooner means higher!)
If you are a Windows user and you have a version of Python 3.3 or greater, you should have the Python Launcher for Windows installed on your machine, which is the recommended way to use for launching all python scripts (regardless of python version the script requires).
As a user
Always type py instead of python when running a script from the command line.
Setup your "Open with..." explorer default program association with C:\Windows\py.exe
Set the command line file extension association to use the Python Launcher for Windows (this will make typing py optional). In an Admin cmd terminal, run:
ftype Python.File="C:\Windows\py.exe" "%L" %*
ftype Python.NoConFile="C:\Windows\pyw.exe" "%L" %*
Set your preferred default version by setting the PY_PYTHON environment variable (e.g. PY_PYTHON=3.11). You can see what version of python is your default by typing py. You can also set PY_PYTHON3 or PY_PYTHON2 to specify default python 3 and python 2 versions (if you have multiple).
If you need to run a specific version of python, you can use py -M.m (where M is the major version and m is the minor version). For example, py -3 will run any installed version of python 3.
List the installed versions of python with py -0.
As a script writer
Include a shebang line at the top of your script that indicates the major version number of python required. If the script is not compatible with any other minor version, include the minor version number as well. For example:
#!/usr/bin/env python3
Note: (see this question) If python3 does not work for you, ensure that you've installed python from the Windows Store (e.g. via winget install --id 9NRWMJP3717K, as the winget package Python.Python.3.11 does not appear to include a python3.exe).
You can use the shebang line to indicate a virtual environment as well (see PEP 486 below).
See also
PEP 397 -- Python launcher for Windows
PEP 486 -- Make the Python Launcher aware of virtual environments
Python Launcher for Windows - User Guide
See here for original post
;
; This is an example of how a Python Launcher .ini file is structured.
; If you want to use it, copy it to py.ini and make your changes there,
; after removing this header comment.
; This file will be removed on launcher uninstallation and overwritten
; when the launcher is installed or upgraded, so don't edit this file
; as your changes will be lost.
;
[defaults]
; Uncomment out the following line to have Python 3 be the default.
;python=3
[commands]
; Put in any customised commands you want here, in the format
; that's shown in the example line. You only need quotes around the
; executable if the path has spaces in it.
;
; You can then use e.g. #!myprog as your shebang line in scripts, and
; the launcher would invoke e.g.
;
; "c:\Program Files\MyCustom.exe" -a -b -c myscript.py
;
;myprog="c:\Program Files\MyCustom.exe" -a -b -c
Thus, on my system I made a py.ini file under c:\windows\ where py.exe exists, with the following contents:
[defaults]
python=3
Now when you Double-click on a .py file, it will be run by the new default version. Now I'm only using the Shebang #! python2 on my old scripts.
Edit registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\python.exe\default
Set default program to open .py files to python.exe
This work for me.
If you want to use the python 3.6 you must move the python3.6 on the top of the list.
The same applies to the python2.7
If you want to have the 2.7 as default then make sure you move the python2.7 on the very top on the list.
step 1
step 2
step 3
then close any cmd command prompt and opened again, it should work as expected.
python --version
>>> Python 3.6
This worked for me:
Go to
Control Panel\System and Security\System
select
Advanced system settings from the left panel
from Advanced tab click on Environment Variables
In the System variables section search for (create if doesn't exist)
PYTHONPATH
and set
C:\Python27\;C:\Python27\Scripts;
or your desired version
You need to restart CMD.
In case it still doesn't work you might want to leave in the PATH variable only your desired version.
With Python version 2.7, 3.7, 3.9 and 3.11 installed on my Windows 11 OS, the above solutions didn't works for me.
The command py --help give some hints to set python version, example:
usage:
[...]
If an exact version is not given, using the latest
version can be overridden by any of the following, (in priority
order):
👉 From [defaults] in py.ini in your %LOCALAPPDATA%\py.ini
The easiest way I found to set a specific default Python version is to create/edit a py.ini file under %LOCALAPPDATA%\py.ini.
Content of py.ini 👇
[defaults]
python=3.7
Console output with Administrator and User right 💡:
C:\Users\bob>py --version
Python 3.7.9
If you are on Windows, use the ASSOC command to change the default python version for python programs.
assoc .py=<Python 3.1 directory>
Now that Python 3.3 is released it is easiest to use the py.exe utility described here:
http://www.python.org/dev/peps/pep-0397/
It allows you to specify a Python version in your script file using a UNIX style directive. There are also command line and environment variable options for controlling which version of Python is run.
The easiest way to get this utility is to install Python 3.3 or later.
Nothing above worked, this is what worked for me:
ftype Python.File=C:\Path\to\python.exe "%1" %*
This command should be run in Command prompt launched as administrator
Warning: even if the path in this command is set to python35, if you have python36 installed it's going to set the default to python36. To prevent this, you can temporarily change the folder name from Python36 to xxPython36, run the command and then remove the change to the Python 36 folder.
Edit: This is what I ended up doing: I use Python Launcher.
https://stackoverflow.com/a/68139696/3154274
Check which one the system is currently using:
python --version
Add the main folder location (e.g. C/ProgramFiles) and Scripts location (C/ProgramFiles/Scripts) to Environment Variables of the system. Add both 3.x version and 2.x version
Path location is ranked inside environment variable. If you want to use Python 2.x simply put path of python 2.x first, if you want for Python 3.x simply put 3.x first
This uses python 2
Since my problem was slightly different and none of the above worked for me, I'll add what worked for me. I had installed the new python launcher for python 3.10 today, installed the version through it, but the command window did not recognise the version. Instead, it listed older python3 versions I had on my computer.
Finally, in the windows programs list, I saw that I had two versions of the python launcher. I uninstalled the old one, and now python 3.10 shows up correctly when running py -0 and is the chosen version when running py.
Apologies if this is a noob answer, I am new to all this.
I had same problem and solve it by executing the installation file again. when you do that python automatically knows you have installed it before so it recommends you 3 options! select modify and select all packages you want to modify then in the next page you can check if new version of python is added to your environment variables or not. check it and then execute modification. I did and it solved.
Use SET command in Windows CMD to temporarily set the default python for the current session.
SET PATH=C:\Program Files\Python 3.5
Try modifying the path in the windows registry (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment).
Caveat: Don't break the registry :)

Adding into Path var while silent installation of Python - possible bug?

I need to passively install Python in my applications package installation so i use the following:
python-3.5.4-amd64.exe /passive PrependPath=1
according this: 3.1.4. Installing Without UI I use the PrependPath parameter which should add paths into Path in Windows environment variables.
But it seems not to work. The variables does not take any changes.
If i start installation manually and select or deselect checkbox with add into Paths then everything works.
Works same with clear installation also on modify current installation. Unfortunately i do not have other PC with Win 10 Pro to test it.
I have also tried it with Python 3.6.3 with same results.
EDIT:
Also tried with PowerShell Start-Process python-3.5.4-amd64.exe -ArgumentList /passive , PretendPath=1 with same results.
Also tested on several PCs with Windows 10, same results, so the problem is not just on single PC
EDIT:
Of cource all attempts were run as administrator.
Ok, from my point of view it seems to be bug in Python Installer and I can not find any way how to make it works.
I have founds the following workaround:
Use py.exe which is wrapper for all version of Python on local machine located in C:\Windows so you can run it directly from CMD anywhere thanks to C:\Windows is standard content of Path variable.
py -3.5 -c "import sys; print(sys.executable[:-10])"
This gives me directory of python 3.5 installation.
And then i set it into Path manually by:
setx Path %UserProfile%";PythonLocFromPreviousCommand
try powershell to do that
Start-Process -NoNewWindow .\python.exe /passive
Make sure you are using an elevated command prompt (ie: run as administrator).
Have you tried to use the InstallAllUsers argument. By default it is set >to 0 so try to use it like this (which is the same example from [here][1]):
python-3.6.0.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
it migth make a difference to use the /quiet over /passive
[1]: https://docs.python.org
/3.6/using/windows.html#installing-without-ui "the link you supplied"
To answer Erik Šťastný comment i believe that a good solution to your problem is to package python with your program to make sure that all the required libaries is preinstalled.
I also tried the command line options for the python installer and noticed the same issue as you, and here's the solution I found:
Download the 64-bit installer from here: https://www.python.org/downloads/windows/
(the link is titled "Windows x86-64 executable installer")
Uninstall any current python installation.
You can use this command: START python-3.8.3-amd64.exe /uninstall
(replace python-3.8.3-amd64.exe with the name of the file you downloaded).
(run cmd or your batch file as administrator, by right-clicking, then Run As Administrator).
Install (as admin) python 64-bit for all users, with the START command:
START python-3.8.3-amd64.exe /passive PrependPath=1 Include_pip=1 InstallAllUsers=1
(replace python-3.8.3-amd64.exe with the name of the file you downloaded).
(run cmd or your batch file as administrator, by right-clicking, then Run As Administrator).
(More info on python installer command line options: https://docs.python.org/3/using/windows.html#installing-without-ui).
(Optional) Open a new cmd window to verify that python works from any location:
You can run this command:python --version
(If you don't see output like "Python 3.8.3", then Python has not been added to your PATH).
(Note: That command didn't work until I opened a new command prompt window).
For me, all of the details were important, so don't skip any.

Python not working in the command line of git bash

Python will not run in git bash (Windows). When I type python in the command line, it takes me to a blank line without saying that it has entered python 2.7.10 like its does in Powershell. It doesn't give me an error message, but python just doesn't run.
I have already made sure the environmental variables in PATH included c:\python27. What else can I check?
A session wherein this issue occurs looks like the following:
user#hostname MINGW64 ~
$ type python
python is /c/Python27/python
user#hostname MINGW64 ~
$ python
...sitting there without returning to the prompt.
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.
>>>
This is a known bug in MSys2, which provides the terminal used by Git Bash. You can work around it by running a Python build without ncurses support, or by using WinPTY, used as follows:
To run a Windows console program in mintty or Cygwin sshd, prepend console.exe to the command-line:
$ build/console.exe c:/Python27/python.exe
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 10 + 20
30
>>> exit()
The prebuilt binaries for msys are likely to work with Git Bash. (Do check whether there's a newer version if significant time has passed since this answer was posted!).
As of Git for Windows 2.7.1, also try using winpty c:Python27/python.exe; WinPTY may be included out-of-the-box.
I am windows 10 user and I have installed GIT in my system by just accepting the defaults.
After reading the above answers, I got 2 solutions for my own and these 2 solutions perfectly works on GIT bash and facilitates me to execute Python statements on GIT bash.
I am attaching 3 images of my GIT bash terminal. 1st with problem and the latter 2 as solutions.
PROBLEM - Cursor is just waiting after hitting python command
SOLUTION 1
Execute winpty <path-to-python-installation-dir>/python.exe on GIT bash terminal.
Note: Do not use C:\Users\Admin like path style in GIT bash, instead use /C/Users/Admin.
In my case, I executed winpty /C/Users/SJV/Anaconda2/python.exe command on GIT bash
Or if you do not know your username then execute winpty /C/Users/$USERNAME/Anaconda2/python.exe
SOLUTION 2
Just type python -i and that is it.
Thanks.
Try python -i instead of python, it's a cursor thing.
Git Bash Workaround- Launch Python 2 & Python 3 with aliases
HI. This is (for me) the best solution to run both Python (Python 2.7 and Python 3.x) directly from Git Bash on Win 10 => adding aliases into the aliases file that Git Bash uses for.
Git Bash aliases file is aliases.sh. It is located in:
C:\path where you installed Git\etc\profile.d\aliases.sh
1) Open (with a text editor like Atom or other) the aliases.sh
for ex: in my case the file is in C:\Software\Develop\Git\etc\profile.d\aliases.sh
2) Add your alias for Python
In my case the python.exe are installed in:
C:\Networking\Network Automation\Python 2.7\python.exe
C:\Networking\Network Automation\Python 3.7\python.exe
So you must create 2 aliases, one for Python 2 (I named python2) and the other for Python 3 (I named just python)
Git Bash uses linux file structure so you need to change the "\" for "/"
and if you have a path like my example Network Automation you put it with " "
"Network Automation", for ex.
winpty is the magic command that will call the executable.
So add these lines at the beginning of aliases.sh
alias python2='winpty C/Networking/"Network Automation"/"Python 2.7"/python.exe'
alias python='winpty C/Networking/"Network Automation"/"Python 3.7"/python.exe'
3) Add or Modify other aliases (if you want)
I modified also the ll alias to show all the files and in a human readable list:
alias ll='ls -lah'
4) Save the aliases.sh file
5) OK!!! close and relaunch your Git Bash
Now, permanently you could launch both Python directly from Git shell just writting
$ python -> launch Python 3
$ python2 -> launch Python 2
$ ll -> enters a ls -lah to quickly show your detailed file list
Cheers, Harry
2 workarounds, rather than a solution: In my Git Bash, following command hangs and I don't get the prompt back:
% python
So I just use:
% winpty python
As some people have noted above, you can also use:
% python -i
2020-07-14: Git 2.27.0 has added optional experimental support for pseudo consoles, which allow running Python from the command line:
See attached session.
In addition to the answer of #Charles-Duffy, you can use winpty directly without installing/downloading anything extra. Just run winpty c:/Python27/python.exe. The utility winpty.exe can be found at Git\usr\bin. I'm using Git for Windows v2.7.1
The prebuilt binaries from #Charles-Duffy is version 0.1.1(according to the file name), while the included one is 0.2.2
type: 'winpty python' and it will work
gitbash has some issues when running any command that starts with python. this goes for any python manage.py commands as well. Always start with 'winpty python manage.py' At least this is what works for me. Running Windows 10.
You can change target for Git Bash shortcut from:
"C:\Program Files\Git\git-bash.exe" --cd-to-home
to
"C:\Program Files\Git\git-cmd.exe" --no-cd --command=usr/bin/bash.exe -l -i
This is the way ConEmu used to start git bash (version 16). Recent version starts it normally and it's how I got there...
In addition to #Vitaliy Terziev answer
try touch .bash_profile and then add alias into the file.
I am using MINGW64 via Visual Studio Code on Windows 10 and trying to install node-sass (which requires python2). I followed felixrieseberg/windows-build-tools #56 on Github which solved my issue.
This is a special case, but I'm posting in case someone has the same problem:
npm --add-python-to-path='true' --debug install --global windows-build-tools
This installs python and other required build tools to %USERPROFILE%\.windows-build-tools\python27.
For python version 3.7.3 in vscode with gitbash as the default terminal I was dealing with this for a while and then followed #Vitaliy Terziev advice of adding the alias to .bashrc but with the following specification:
alias python=’“/c/Users/my user name/AppData/Local/Programs/Python/Python37/python.exe”’
Notice the combination of single and double quotes because of “my user name” spaces.
For me, "winpty" couldn't resolve python path in vscode.
Type the command PY instead of Python. Invoking the Interpreter
(python.org).
I know this is an old post, but I just came across this problem on Windows 10 running Python 3.8.5 and Git 2.28.0.windows.1
Somehow I had several different 2.7x versions of Python installed as well. I removed every version of Python (3x and 2x), downloaded the official installer here, installed 3.8.5 fresh (just used the defaults) which installed Python 3.8.5 at this location:
C:\Users\(my username)\AppData\Local\Programs\Python\Python38
Then to get the command python to work in my git bash shell, I had to manually add the path to Python38 to my path variable following the instructions listed here. This is important to note because on the python installer at the bottom of the first modal that comes up it asks if you want to add the python path to your PATH environment variable. I clicked the checkbox next to this but it didn't actually add the path, hence the need to manually add the path to my PATH environment variable.
Now using my gitbash shell I can browse to a directory with a python script in it and just type python theScriptName.py and it runs no problem.
I wanted to post this because this is all I had to do to get my gitbash shell to allow me to run python scripts. I think there might have been some updates so I didn't need to do any of the other solutions listed here. At any rate, this is another thing to try if you are having issues running python scripts in your gitbash shell on a Windows 10 machine.
Enjoy.
Another example of this issue is using the AWS Elastic Beanstalk command line interface (awsebcli, eb cli) from the git bash (MINGW64, Mintty) in windows (using git version 2.19.0.windows.1).
I'm just posting this because it took me a while to end up here, searching for eb-cli specific issues.
Commands such as eb init or eb config save, which require user input, appear to cause a freeze/hang. In reality I guess the console is not updated with the text requesting user input. Moreover, eb deploy only updates the console text after the command has finished, so I don't get to see progress updates until finished.
As mentioned in the git for windows release notes (for v2.19.0) and e.g. in Xun Yang's answer, a workaround is to run
winpty eb <command> (instead of just eb <command>)
A alternative, as suggested in this git for windows issue, could be to use the windows native console instead of mintty (option during git installation).
The one worked for me is as mentioned earlier in these great answers above is the alias as follows:
(I'm using anaconda, so first find where is the python path, then add it into the alias on git bash).
1. on anaconda terminal I run: where python
2. on git bash I run: alias python='winpty "C:\ProgramData\Anaconda3\envs\your_env_name\python.exe"'
3. Done. Python is defined inside the git Bash using the alias.
Thanks to (Vitaliy Terziev & hygull) for their very helpful answers.
python.exe -i works but got issues in exiting from the interactive mode by sending "^Z" (CTRL+Z). So, seem better to use winpty python.exe in Git Bash for Windows.
Use ~/bin directory to make a wrap/reference file (like ~/bin/python) which will be accessible everywhere (you may use different version reference like ~/bin/python37).
Code inside the file:
#!/usr/bin/env bash
# maybe declare env vars here like
# export PYTHONHOME=/c/Users/%USERNAME%/.python/Python36
# export PATH="${PATH}:/c/Users/%USERNAME%/.python/Python36"
# replace %USERNAME%,
# or use "~" instead of "/c/Users/%USERNAME%" if it works
winpty /c/Users/%USERNAME%/.python/Python36/python.exe ${#}
I just don't like these "magic" aliases which you're always forgetting where it's coming from, and sometimes leads to issues in some cases.
Use ~/bin/python file and -i parameter:
#!/usr/bin/env bash
if [ -z "${#}" ]; then
# empty args, use interactive mode
/c/Users/%USERNAME%/.python/Python36/python.exe -i
else
/c/Users/%USERNAME%/.python/Python36/python.exe ${#}
fi
if you run a Windows PowerShell command and an error occurs, the error record will be appended to the “automatic variable” named $error.
You can use the $error variable to find the errors, in the same PowerShell session.
The $Error variable holds a collection of information, and that’s why using $Error[0] can get to your error message objects. Also the $Error[0] variable will hold the last error message encountered until the PowerShell session ends.
Have a look at this answer:
Git Bash won't run my python files?
the path in Git Bash should be set like this:
PATH=$PATH:/c/Python27/

How Should I Set Default Python Version In Windows?

I installed Python 2.6 and Python 3.1 on Windows 7 and set environment variable: path = d:\python2.6.
When I run python in cmd, it displays the python version 2.6, which is what I want!
But, when I wrote a script in a bat file and ran it, the displayed python version was 3.1.
import sys
print (sys.version)
What's going on here?
This is if you have both the versions installed.
Go to This PC → Right-click → Click on Properties → Advanced System Settings.
You will see the System Properties. From here navigate to the Advanced Tab -> Click on Environment Variables.
You will see a top half for the user variables and the bottom half for System variables.
Check the System Variables and double-click on the Path (to edit the Path).
Check for the path of Python(which you wish to run i.e. Python 2.x or 3.x) and move it to the top of the Path list.
Restart the Command Prompt, and now when you check the version of Python, it should correctly display the required version.
The Python installer installs Python Launcher for Windows. This program (py.exe) is associated with the Python file extensions and looks for a "shebang" comment to specify the python version to run. This allows many versions of Python to co-exist and allows Python scripts to explicitly specify which version to use, if desired. If it is not specified, the default is to use the latest Python version for the current architecture (x86 or x64). This default can be customized through a py.ini file or PY_PYTHON environment variable. See the docs for more details.
Newer versions of Python update the launcher. The latest version has a py -0 option to list the installed Pythons and indicate the current default.
Here's how to check if the launcher is registered correctly from the console:
C:\>assoc .py
.py=Python.File
C:\>ftype Python.File
Python.File="C:\Windows\py.exe" "%1" %*
Above, .py files are associated with the Python.File type. The command line for Python.File is the Python Launcher, which is installed in the Windows directory since it is always in the PATH.
For the association to work, run scripts from the command line with script.py, not "python script.py", otherwise python will be run instead of py. If fact it's best to remove Python directories from the PATH, so "python" won't run anything and enforce using py.
py.exe can also be run with switches to force a Python version:
py -3 script.py # select latest Python 3.X version to be used.
py -3.6 script.py # select version 3.6 specifically.
py -3.9-32 script.py # select version 3.9 32-bit specifically.
py -0 # list installed Python versions (latest PyLauncher).
Additionally, add .py;.pyw;.pyc;.pyo to the PATHEXT environment variable and then the command line can just be script with no extension.
Running 'py' command will tell you what version you have running. If you currently running 3.x and you need to switch to 2.x, you will need to use switch '-2'
py -2
If you need to switch from python 2.x to python 3.x you will have to use '-3' switch
py -3
If you would like to have Python 3.x as a default version, then you will need to create environment variable 'PY_PYTHON' and set it's value to 3.
If you know about Environment variables and the system variable called path, consider that any version of any binary which comes sooner, will be used as default.
Look at the image below, I have 3 different python versions but python 3.8 will be used as default since it came sooner than the other two. (In case of mentioned image, sooner means higher!)
If you are a Windows user and you have a version of Python 3.3 or greater, you should have the Python Launcher for Windows installed on your machine, which is the recommended way to use for launching all python scripts (regardless of python version the script requires).
As a user
Always type py instead of python when running a script from the command line.
Setup your "Open with..." explorer default program association with C:\Windows\py.exe
Set the command line file extension association to use the Python Launcher for Windows (this will make typing py optional). In an Admin cmd terminal, run:
ftype Python.File="C:\Windows\py.exe" "%L" %*
ftype Python.NoConFile="C:\Windows\pyw.exe" "%L" %*
Set your preferred default version by setting the PY_PYTHON environment variable (e.g. PY_PYTHON=3.11). You can see what version of python is your default by typing py. You can also set PY_PYTHON3 or PY_PYTHON2 to specify default python 3 and python 2 versions (if you have multiple).
If you need to run a specific version of python, you can use py -M.m (where M is the major version and m is the minor version). For example, py -3 will run any installed version of python 3.
List the installed versions of python with py -0.
As a script writer
Include a shebang line at the top of your script that indicates the major version number of python required. If the script is not compatible with any other minor version, include the minor version number as well. For example:
#!/usr/bin/env python3
Note: (see this question) If python3 does not work for you, ensure that you've installed python from the Windows Store (e.g. via winget install --id 9NRWMJP3717K, as the winget package Python.Python.3.11 does not appear to include a python3.exe).
You can use the shebang line to indicate a virtual environment as well (see PEP 486 below).
See also
PEP 397 -- Python launcher for Windows
PEP 486 -- Make the Python Launcher aware of virtual environments
Python Launcher for Windows - User Guide
See here for original post
;
; This is an example of how a Python Launcher .ini file is structured.
; If you want to use it, copy it to py.ini and make your changes there,
; after removing this header comment.
; This file will be removed on launcher uninstallation and overwritten
; when the launcher is installed or upgraded, so don't edit this file
; as your changes will be lost.
;
[defaults]
; Uncomment out the following line to have Python 3 be the default.
;python=3
[commands]
; Put in any customised commands you want here, in the format
; that's shown in the example line. You only need quotes around the
; executable if the path has spaces in it.
;
; You can then use e.g. #!myprog as your shebang line in scripts, and
; the launcher would invoke e.g.
;
; "c:\Program Files\MyCustom.exe" -a -b -c myscript.py
;
;myprog="c:\Program Files\MyCustom.exe" -a -b -c
Thus, on my system I made a py.ini file under c:\windows\ where py.exe exists, with the following contents:
[defaults]
python=3
Now when you Double-click on a .py file, it will be run by the new default version. Now I'm only using the Shebang #! python2 on my old scripts.
Edit registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\python.exe\default
Set default program to open .py files to python.exe
This work for me.
If you want to use the python 3.6 you must move the python3.6 on the top of the list.
The same applies to the python2.7
If you want to have the 2.7 as default then make sure you move the python2.7 on the very top on the list.
step 1
step 2
step 3
then close any cmd command prompt and opened again, it should work as expected.
python --version
>>> Python 3.6
This worked for me:
Go to
Control Panel\System and Security\System
select
Advanced system settings from the left panel
from Advanced tab click on Environment Variables
In the System variables section search for (create if doesn't exist)
PYTHONPATH
and set
C:\Python27\;C:\Python27\Scripts;
or your desired version
You need to restart CMD.
In case it still doesn't work you might want to leave in the PATH variable only your desired version.
With Python version 2.7, 3.7, 3.9 and 3.11 installed on my Windows 11 OS, the above solutions didn't works for me.
The command py --help give some hints to set python version, example:
usage:
[...]
If an exact version is not given, using the latest
version can be overridden by any of the following, (in priority
order):
👉 From [defaults] in py.ini in your %LOCALAPPDATA%\py.ini
The easiest way I found to set a specific default Python version is to create/edit a py.ini file under %LOCALAPPDATA%\py.ini.
Content of py.ini 👇
[defaults]
python=3.7
Console output with Administrator and User right 💡:
C:\Users\bob>py --version
Python 3.7.9
If you are on Windows, use the ASSOC command to change the default python version for python programs.
assoc .py=<Python 3.1 directory>
Now that Python 3.3 is released it is easiest to use the py.exe utility described here:
http://www.python.org/dev/peps/pep-0397/
It allows you to specify a Python version in your script file using a UNIX style directive. There are also command line and environment variable options for controlling which version of Python is run.
The easiest way to get this utility is to install Python 3.3 or later.
Nothing above worked, this is what worked for me:
ftype Python.File=C:\Path\to\python.exe "%1" %*
This command should be run in Command prompt launched as administrator
Warning: even if the path in this command is set to python35, if you have python36 installed it's going to set the default to python36. To prevent this, you can temporarily change the folder name from Python36 to xxPython36, run the command and then remove the change to the Python 36 folder.
Edit: This is what I ended up doing: I use Python Launcher.
https://stackoverflow.com/a/68139696/3154274
Check which one the system is currently using:
python --version
Add the main folder location (e.g. C/ProgramFiles) and Scripts location (C/ProgramFiles/Scripts) to Environment Variables of the system. Add both 3.x version and 2.x version
Path location is ranked inside environment variable. If you want to use Python 2.x simply put path of python 2.x first, if you want for Python 3.x simply put 3.x first
This uses python 2
Since my problem was slightly different and none of the above worked for me, I'll add what worked for me. I had installed the new python launcher for python 3.10 today, installed the version through it, but the command window did not recognise the version. Instead, it listed older python3 versions I had on my computer.
Finally, in the windows programs list, I saw that I had two versions of the python launcher. I uninstalled the old one, and now python 3.10 shows up correctly when running py -0 and is the chosen version when running py.
Apologies if this is a noob answer, I am new to all this.
I had same problem and solve it by executing the installation file again. when you do that python automatically knows you have installed it before so it recommends you 3 options! select modify and select all packages you want to modify then in the next page you can check if new version of python is added to your environment variables or not. check it and then execute modification. I did and it solved.
Use SET command in Windows CMD to temporarily set the default python for the current session.
SET PATH=C:\Program Files\Python 3.5
Try modifying the path in the windows registry (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment).
Caveat: Don't break the registry :)

Categories

Resources