This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: SyntaxError when running Python 2.7 interpreter with subprocess.call
Type: behavior Stage:
Components: Extension Modules, Windows Versions: Python 2.7
process
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, giumas, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2016-02-21 14:39 by giumas, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg260612 - (view) Author: giumas (giumas) Date: 2016-02-21 14:39
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 "<stdin>", 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.
msg260633 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2016-02-21 19:41
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 "<stdin>", line 1
        a = 0
             ^
    SyntaxError: invalid syntax

By any chance do you have PYTHONUNBUFFERED set in the environment?
msg260651 - (view) Author: giumas (giumas) Date: 2016-02-22 00:32
No, I don't. I have just double-checked it. 

Did you try to run my minimal example on your machine? If yes, are you getting the same behavior?
msg260652 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2016-02-22 00:45
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.
msg260653 - (view) Author: giumas (giumas) Date: 2016-02-22 01:26
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:27adminsetgithub: 70588
2016-02-22 01:26:48giumassetstatus: open -> closed
resolution: third party
messages: + msg260653
2016-02-22 00:45:39eryksunsetmessages: + msg260652
2016-02-22 00:32:11giumassetmessages: + msg260651
2016-02-21 19:41:24eryksunsettype: crash -> behavior

messages: + msg260633
nosy: + eryksun
2016-02-21 14:39:43giumascreate