Q&A for How to Use Windows Command Prompt to Run a Python File

Return to Full Article

Search
Add New Question
  • Question
    I want to create a shortcut that executes the utility "ptpython," running in the cmd prompt. The shortcut I have points to the directory containing "ptpython.exe" file but it does not execute it.
    Community Answer
    It sounds like ptpython.exe is a command-line utility, meaning it will only start if you execute it from a DOS window - you can't create a shortcut for it directly. You can probably create a shortcut to cmd.exe, though (the DOS window) and pass it the ptpython.exe file as a parameter. Something like "cmd.exe /c ptpython.exe" should work, or if this disappears in the end, try with /k (instead of /c).
  • Question
    Does this work on Windows 7?
    Arrogance
    Top Answerer
    Yes. The directions to access the environment variables would be slightly different, as there is no "Power User" menu in Windows 7. Instead: 1. Press the Windows key and R to open the Run dialog. 2. Enter "sysdm.cpl". 3. Click the "Advanced" tab of the System Properties Window. 4. Click the "Environmental variables". Most everything else would work as described even on Windows 95 (if there's a version of Python for Windows 95).
  • Question
    After opening the Command Prompt and navigation to the directory in which the py file exists and opening Python, not able to run the file using python file_name.py. It says that the syntax is wrong.
    Arrogance
    Top Answerer
    That sounds like a problem with the file you're trying to run. Make sure you are using the right version of Python for it (version 2 or 3, usually).
  • Question
    If in "PATH" variable, path of Java is already set, and we try to set path of Python there, will it affect the working of Java?
    Arrogance
    Top Answerer
    No. PATH is just a list of directories for the shell to look in for programs and libraries. Adding the Python directory to the end cannot affect Java at all, as Java would come before it. Even if you added Python before Java, it could only conflict if there were program names common to both.
  • Question
    Whenever after writing the address I try to run the program but it always says that Python is not recognized as an internal or external command, operable program or batch file. What should I do?
    StickBoiMan
    Community Answer
    This means that you cannot run files with the 'Python' command in Command Prompt or Windows Powershell IDE. Run it in the Python Shell. Usually, you can use the command 'import'. So if my file's name is hello.py, I would type 'import hello.py'. (Without the single-quotation marks.)
  • Question
    I'm getting a syntax error on the 8th line of this Python script. 1import requests 2import json 3def test(): 4567 f = open('greenbook_1956.json') 8print("Got Here"data) 9data= json.load(f). How can I fix it?
    Tinkerer02
    Top Answerer
    Python parameters are separated by commas. Add a comma after the last quote like this: print("Got Here", data). It is also good practice to put a space after the comma. However, on line 8, 'data' does not exist yet and you will get another runtime error. Since line 9 assigns the result of 'json.load(f)' to 'data', you should move your 'print' statement to after line 9. It should then show the contents of 'data'.
Ask a Question

      Return to Full Article