0

Having this syntax error running python in windows command prompt.

syntaxError

Even if i type nothing and just hit enter it will still have syntax error. Only thing i can do is Ctrl+Z to exit. ( exit() doesnt work )

The command prompt is launched from a bat script that first sets a bunch of env vars then launches cmd. This bat file is called from a python gui via subprocess or os.system (both have the issue).

bat file set up in following way:

set PYTHONPATH=\some\path

...lots more env var setting (which is the purpose of this "custom" cmd

start /D "some/location" "cmd"

If i run the bat file directly by double clicking its fine.

running echo %PYTHONPATH% shows all the correct paths in both instances of launching (same for all other env vars)

I am wanting to launch it from a python gui for convenience with all environment paths set.

Any ideas whats going on?

7
  • Did you do anything weird with character encodings or terminal behavior? Commented Mar 2, 2017 at 0:44
  • not that i know of. Launch process is 1 line: subprocess.Popen('path\to\bat.bat', shell=True) Bat is as explained above.THe paths being set are: PYTHONPATH=myCustomPaths PATH=%PATH%;gitPathForCallingAGit.exe Thats it for environment. Havent touched anything else. Commented Mar 2, 2017 at 1:11
  • 1
    What command do you use to start Python? This behavior looks like you've used the -u command-line option, which is incompatible with the standard REPL on Windows. It puts stdin in binary mode. Thus CRLF from the console input doesn't get translated to the Unix-style LF line-endings that the REPL requires. Commented Mar 2, 2017 at 1:38
  • BTW, depending on the speed and available resources on the system, what you're doing can cause an annoying flash of a temporary console window (conhost.exe). Using shell=True runs the command with cmd.exe /c. That creates a console (conhost.exe process). You run the batch in that cmd instance to set up the environment, but then spawn a new shell in a second console window. That's not necessary. If you use the default shell=False and run the batch via cmd.exe /k, then you won't need to end with a start command. You can do everything in one shell and one console. Commented Mar 2, 2017 at 1:45
  • which python? lol the first python is run via something like: python pythonFile.py. This launches the GUI. On the GUI is a button that runs the bat file with subprocess which opens a separate command prompt window. Once in this command prompt i run python by typing python. Never known about or used a -u, is it an option that is on by default? It sounds to me like its a 'line-endings' issue as you mention. Havent noticed any temporary console window flash. The weird thing is it works perfectly fine as long as i launch the bat directly with double click. Commented Mar 2, 2017 at 1:56

1 Answer 1

2

"Solved" it, but doesnt quite explain it for me though. @eryksun pointed me in the right direction, thanks!

removing var PYTHONUNBUFFERED did it for me.

set PYTHONUNBUFFERED=

Kinda get it but not fully...hope removing it doesnt cause more issues! :P It's really just an environment for testing stuff so the actual product isnt run in this way.

Python Interactive Interpreter always returns "Invalid syntax" on Windows

Sign up to request clarification or add additional context in comments.

5 Comments

Setting that environment variable is the same as passing the -u command-line option. You can temporarily enable binary mode to keep from corrupting a binary stream that's piped to stdin, but setting it permanently breaks the REPL on Windows.
right, in this case its temporary within the session. So far haven't run into any hiccups. What i'd like to know is how was the -u being passed. I wasnt setting it..
Do you mean how the PYTHONUNBUFFERED environment variable is being set? Open a new a command prompt and check set PYTHON to see a listing of all environment variables that start with "PYTHON". If it's set, then use the system environment variable editor to remove it.
Yeah i know how to remove it as my solution above. This line happens during run time straight after cmd is launched (in line actually). However i was wondering more why it was set in the first place and more so why only in this method of launching (i.e. from another python interpreter). If launched directly (via double clicking the bat) there's no issues.
If you don't see PYTHONBUFFERED defined in the system environment variables editor, then possibly your pythonFile script is setting it. You can pass a custom environment to the Popen call. For example, the original environment is available as nt.environ. You can copy it, e.g. environ = nt.environ.copy(), and then modify the copy, e.g. if 'PYTHONBUFFERED' in environ: del environ['PYTHONBUFFERED']. Then run subprocess.Popen(r'cmd.exe /k "path\to\bat.bat"', env=environ).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.