Can't Run a Python File in Windows Command Prompt [Duplicate]

I'm running Windows 7 64-bit with Python 3.4.1, 64-bit cmd window I've appended the PATH in system Environment Variable with ;C:\Python34 and saved, then gone into cmd and done the following, yet I keep getting an 'INVALID SYNTAX' message. Yes, I've searched the Q&A here and seen several threads with same problem, but haven't reached success trying their suggestions. As can be seen, I've tried 3 variations of the basic command "python ." Please help.

1

I think you're misunderstanding the idea.

If you want to execute a script "RUN_ME.py", run python RUN_ME.py directly in the command line. (type cmd into start and then python RUN_ME.py in the window you get)

If you want to type some lines of python and interpret them directly, type python without arguments. (or use the program shortcut you used here)

2

The mistake made here is the difference between the command line and the python REPL.

When you opened the terminal the way you did, probably by clicking on python terminal or whatever, you opened the python REPL (also available by executing python without arguments in command line), this is a place where you can execute code directly (no scripts). it used to be able to run python scripts as well but the command was phased out in python 3.x. Typically the REPL starts lines with >>>

To run a python script, you have to be in the command line and give the script as parameter to python, aka python <script_name>. Getting the command line on windows can be done directly by typing cmd, or if you want to open it at a certain path, browse to the path you want in the file explorer and type cmd into the place where the path is displayed.

Okay, I understand now not to use 'python' plus the .py filename when already in the Python '>>>' prompt. Here's a simple try within Windows command prompt at navigating to the directory where the 'RUN_ME.py' file is located and attempting to run:

C:\>cd program files\trader workstation
C:\Program Files\Trader Workstation>cd ibridgepy
C:\Program Files\Trader Workstation\IBridgePy>python

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AM D64)] on win32 Type "help", "copyright", "credits" or "license" for more information.

>>> import os
>>> os.getcwd()
'C:\\Program Files\\Trader Workstation\\IBridgePy'
>>> RUN_ME.py
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'RUN_ME' is not defined
Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest