On Windows, I am getting a `SyntaxError` when I try to input commands after having launched a Python 2.7.x interpreter with `subprocess.call`. This is a minimal example: import os import subprocess def python_env_path(python_path): env = os.environ.copy() python_scripts = os.path.join(python_path, "Scripts") python_bin = os.path.join(python_path, "Library", "bin") path_env = "%s;%s;%s;" % (python_path, python_scripts, python_bin) env['PATH'] = path_env.encode() return env def open_python_prompt(python_path): env = python_env_path(python_path) prc = subprocess.call(["start", "python"], shell=True, cwd=python_path, env=env) if prc != 0: print("Unable to open a Python prompt") return False return True open_python_prompt("C:\Py27x64") When I try to write whatever simple command for the interpreter I get: >>> a = 0 File "", line 1 a = 0 ^ SyntaxError: invalid syntax I did not find SO questions that solve my issue. The same code works fine with Python 3.x. The same Python installation works fine if I open a shell and call the interpreter using a batch file.
The error you're getting indicates that stdin is set to binary mode for some reason. You can see this using the -u command line option: C:\>py -2 -u Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> a = 0 File "", line 1 a = 0 ^ SyntaxError: invalid syntax By any chance do you have PYTHONUNBUFFERED set in the environment?
Your example worked fine for me with the official 64-bit distribution of 2.7.10 installed in "C:\Program Files\Python27" on Windows 10. I created a junction from "C:\Py27x64" to the 2.7 installation directory in order to run the example as provided. This issue is filed under "Extension Modules". Is this a personal build of Python? What's in "Library\bin" (python_bin)? That's not a standard Windows Python directory.
It worked also for me with the official 64-bit distribution! I get that buggy behavior with the Anaconda distribution (this was the "Library\bin"). I should have checked it as first step! My apologies and thank you so much for your kind help.
History
Date
User
Action
Args
2022-04-11 14:58:27
admin
set
github: 70588
2016-02-22 01:26:48
giumas
set
status: open -> closedresolution: third partymessages: +