Ctrl+Z does not exit Python 3.10 in Git Bash - python

I just downloaded the latest Git Bash (2.36.1 64-bit) and installed Python 10. I'm running Windows 11. Among a couple of other unexpected changes from my earlier setup using a previous version of Git Bash and mostly running Python 3.9 (i.e. I now have to run python -i for the interactive python interpreter, for all 2.x and 3.x versions, instead of just running python), the most frustrating is that ctrl+z followed by Enter no longer quits from the interpreter. Instead I must call exit(). Ex:
user#User MINGW64 ~
$ python -i
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> # ctrl+z, Enter pressed here
File "<stdin>", line 1
^
SyntaxError: invalid syntax
>>> exit()
user#User MINGW64 ~
$
The same is true for my other installations of python 3 (3.7, 3.8, 3.9), but python 2.7 still has the expected behavior:
user#User MINGW64 ~
$ $PYTHON\\Python27\\python -i
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> # ctrl+z, Enter pressed here
user#User MINGW64 ~
$
What I've tried
I've done quite a bit of research but can't figure out what may have changed. In the earlier version of Git Bash, I remember that ^Z, ^X, ^C, and ^V characters (and maybe more) would display explicitly in the console when typed. Curiously, pressing ctrl+c followed by Enter comes with the error message below. Did some sort of character encoding change in the newer versions of Git Bash?
user#User MINGW64 ~
$ python
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> # ctrl+c, Enter pressed here
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\encodings\cp1252.py", line 14, in decode
def decode(self,input,errors='strict'):
KeyboardInterrupt
The above exception was the direct cause of the following exception:
KeyboardInterrupt: decoding with 'cp1252' codec failed (KeyboardInterrupt: )
>>>
I am too lazy to constantly type exit(). How can I restore the functionality where pressing ctrl+z, then Enter quits the interpreter for my python 3 versions?

After stumbling upon this answer, I've found a solution that restores the functionality I wanted. Instead of:
alias python3='.../Python310/python
in which case you must get to the interactive interpreter using python3 -i, and ctrl+Z keystrokes are not recognized, use:
alias python3='winpty .../Python310/python
If it's helpful to anyone, here's how I've set up my ~/.bash_aliases:
alias python2='winpty C:\Users\<user>\AppData\Local\Programs\Python/Python27/python'
alias python3.7='winpty C:\Users\<user>\AppData\Local\Programs\Python/Python37/python'
alias python3.8='winpty C:\Users\<user>\AppData\Local\Programs\Python/Python38-32/python'
alias python3.9='winpty C:\Users\<user>\AppData\Local\Programs\Python/Python39/python'
alias python3.10='winpty C:\Users\<user>\AppData\Local\Programs\Python/Python310/python'
alias python3='python3.10'
alias python='python2'
and these lines must be in ~/bashrc:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Now you can open the python interpreter without requiring -i (just run python), and ctrl+Z, Enter exits it.
Edit: After reading this answer, I reinstalled Git Bash with "Enable experimental support for pseudo consoles" checked and functionality is restored without needing to run Python through winpty.

Related

Trying to run simple script at python prompt but getting an error

Trying to run simple python file in python prompt but giving an error:
content of test.py
print("Trying to print this using .py file on anaconda prompt")
Running file from python prompt
(base) C:\Users\acharbha>python
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> python C:\Users\acharbha\Documents\python\test.py
File "<stdin>", line 1
python C:\Users\acharbha\Documents\python\test.py
^
SyntaxError: invalid syntax
>>>
To execute in shell: python test.py
To execute inside the interpreter: exec(open("test.py").read())
This should help:
How to execute a file within the python interpreter?
You are trying to run it within the python interpreter. So just exit that and run the command on the terminal.

Run scons on Windows 10 environment with Python 2.7 and Python 3

How can I run scons on Windows 10 when Python 3.6.1 and Python 2.7.13 is installed? When typing python into my command window I get the following output:
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Since I personally use Python 3.6.x this is fine for me and I do not want to change this.
When typing py -2 into my command window I get the following output:
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
scons does not work with Python 3.x. It needs to be executed with Python 2.7.x. How can I "tell" scons to use Python 2.7.x?
When I just type scons into the command prompt I get the following output:
PS C:\dev\projectX> scons
Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00007050 (most recent call first):
Try Changing line 23 in scons.bat from:
python "%scriptname%" %*
to
py -2 "%scriptname%" %*
Note that work on getting SCons to run under both py 2 and py 3.5+ is mostly done and should be released in the next month (or so)
Before running scons in Powershell, amend the PATH environment variable to put your Python 2.7 installation at the front. Something like:
SET PATH=C:\Python27;%path%

Installing python

I have tried to install Python 3.4 on Windows 7 using the following guide
I have added the following to my Path in system environmental variables
C:\Python34\;C:\Python34\Scripts;
When I open up the Python command line and type the following (as instructed in the tutorial) I receive an error message printed below
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
python --version <- what i type
Traceback (most recent call last):
File "", line 1, in
NameError: name 'python' is not defined
Can anyone help me out?
You need to type python --version from the command line. Not when you are already running python.
As has been mentioned, you need to run your command from the command prompt:
python --version
However, if you are in the python interpreter, you can find the current version with this code:
import sys
print (sys.version)

setting up django with virtualenv on windows7

Im unable to start new project in a virtualenv. This is what i did so far: (fresh windows installation)
1) installed python 2.7 from http://python.org/download/ (not the 64 one)
2) using "set path=%path%;C:\python27" only seems to work for one cmd session, so i added C:\Python27; in my environment variables under advanced system settings, typing python in cmd returns
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
3) downloaded virtualenv.py from http://pypi.python.org/pypi/virtualenv/ ran it with:
C:\Users\Maciej\Dropbox\VIRTUALENV_ENVS>python virtualenv.py testenv
New python executable in testenv\Scripts\python.exe
Installing setuptools....................................done.
Installing pip.........................done.
4) activated it and installed some modules
C:\Users\Maciej\Dropbox\VIRTUALENV_ENVS\testenv\Scripts>activate
(testenv) C:\Users\Maciej\Dropbox\VIRTUALENV_ENVS\testenv\Scripts>
...
(testenv) C:\Users\Maciej\Dropbox\VIRTUALENV_ENVS>pip install -r requirements.txt
...
Successfully installed...
5) tested if it works:
testenv) C:\Users\Maciej\Dropbox\VIRTUALENV_ENVS\testenv>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print(django.get_version())
1.4
>>>
6) Tried to setup a project:
(testenv) C:\Users\Maciej\Dropbox\VIRTUALENV_ENVS\testenv>django-admin.py startproject testproject
Traceback (most recent call last):
File "C:\Users\Maciej\Dropbox\VIRTUALENV_ENVS\testenv\Scripts\django-admin.py"
, line 2, in <module>
from django.core import management
ImportError: No module named django.core
Path from virtualenv:
(testenv) C:\Users\Maciej\Dropbox\VIRTUALENV_ENVS\testenv>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path
['','C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg',
'C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv\\lib\\site-packages\\pip-1.1-py2.7.egg',
'C:\\Windows\\system32\\python27.zip',
'C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv\\DLLs',
'C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv\\lib',
'C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv\\lib\\plat-win',
'C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv\\lib\\lib-tk',
'C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv\\Scripts',
'C:\\Python27\\Lib',
'C:\\Python27\\DLLs',
'C:\\Python27\\Lib\\lib-tk',
'C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv',
'C:\\Users\\Maciej\\Dropbox\\VIRTUALENV_ENVS\\testenv\\lib\\site-packages']
I have no idea whats the problem, and how to fix it, also got another question, are those files from c:\python27 supposed to be in my virtualenv path?
Should i attach any other logs? Poke me if so.
Step 6 is where things start to go wrong
Your windows has bound .py(w) files to use Python from c:\python27 directory, not from your virtualenv.
I've written blog entry about virtualenv and win7:
http://djangonautlostinspace.wordpress.com/2012/04/16/django-and-windows/
I agree with jtiai, things go wrong in step 6 because Windows has bound a specific Python. If you did python path/to/django-admin.py startproject it should work.
Please go through the below given tutorial link..
http://ayarshabeer.com/post/50973941605/install-multiple-django-version-using-virtualenvwrapper
Instead of using virtualenv, on Windows I prefer to use Portable Python : http://www.portablepython.com/. You can have several installations on the same machine and switch between them just by setting the path:
set path=d:\python\app\scripts;d:\python\app;%path%
Moreover, it already contains Django. Once you have set up your python environment, you can copy your python directory over to your production server.

os.exec on Windows

I have a script that calls os.execvp into another Python instance. After doing this, I appear to be attached to a cmd.exe instance, not the Python instance I just created. The Python instance responds to Ctrl+C however.
H:\bin>Python 3.2.1 (default, Jul 10 2011, 21:51:15) [MSC v.1500
32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hi')
Can't find file ('hi')
H:\bin>
H:\bin>
KeyboardInterrupt
>>> echo hi
hi
The call to exec:
from sys import argv
os.execvp('python', argv)
How do I replace the original Python instance with the new one, as per the behaviour one might see on Linux?
On Unix executing binaries is split into two stages - fork(3) to clone current process and exec(3) to load executable into address space. On windows there is only CreateProcess which does the same thing as fork+exec.
For portability your best bet is to use subprocess.Popen (which also does proper filename quoting on Windows unlike os.* counterparts) as in http://docs.python.org/library/subprocess.html#replacing-the-os-spawn-family

Categories

Resources