Command prompt returning blank, but python console

2019-07-20 07:48发布

Sorry if the title is a bit confusing; I'm not sure how to phrase it. I'm a beginner learning python for a linguistics course I'm taking, and I'm having a bit of a problem transferring some scripts into the command prompt. I'm using Windows 8.1 and python 3.6.0, if that helps at all.

The problem I'm having is that I can write a script in the python.exe program, and it will work exactly how it should. Then I will run the exact same script using the command prompt, the only thing it returns is a blank line.

for example, I can manually type this into the python console and get the right output:

>>>print("hello")
hello

but if I save that in a Notepad++ file and execute the same script using the command prompt, it'll look like this (please excuse my butchered pathway recreation):

C:\Users\username\Python>hello.py

C:\Users\username\Python>

The "hello.py" is the script I tried to run using the exact same code I manually entered in the Python console, but when I try to open the file, it does nothing.

This doesn't happen with every script I try to run, though, and I can't figure out what triggers this to happen. I'm not getting any kind of error, so I don't know how to go through trial and error. This is making it increasingly difficult to check my work as I add elements, and I'm not sure how to fix this. If anyone has suggestions, I would really appreciate the help.

标签: python cmd
1条回答
闹够了就滚
2楼-- · 2019-07-20 08:33

Run Python scripts from Windows command line with script name only

Configure Windows File Association Pointers

Set the python.exe full explicit path as in the below #3 for the correct value where this is on your system

  1. Open up an elevated [cmd.exe] command prompt as administrator
  2. Type in ASSOC .py=PythonScript and press Enter
  3. Type in FTYPE PythonScript="C:\Program Files\Python\python.exe" "%1" %* and press Enter
  4. Type in SET PATHEXT=.py;%PATHEXT% and press Enter

Execute from Shell with Script Name only

If you really need to run the script from the command line without telling the shell the full explicit path to the python script file, then you need to add the path where this script resides to the %PATH% environmental variable.

  1. Open up an elevated [cmd.exe] command prompt as administrator
  2. Type in SET PATH=%PATH%;C:\Program Files\Python where the C:\Program Files\Python is the value where this exist on your system.

Now you can just type in the script name with or without the file extension without doing a CD to another directory or explicitly specifying the full path to the python script.


Further Resources

  • Windows Environment Variables

  • FTYPE /?

    Displays or modifies file types used in file extension associations
    
    FTYPE [fileType[=[openCommandString]]]
    
      fileType  Specifies the file type to examine or change
      openCommandString Specifies the open command to use when launching files
                        of this type.
    
    Type FTYPE without parameters to display the current file types that
    have open command strings defined.  FTYPE is invoked with just a file
    type, it displays the current open command string for that file type.
    Specify nothing for the open command string and the FTYPE command will
    delete the open command string for the file type.  Within an open
    command string %0 or %1 are substituted with the file name being
    launched through the assocation.  %* gets all the parameters and %2
    gets the 1st parameter, %3 the second, etc.  %~n gets all the remaining
    parameters starting with the nth parameter, where n may be between 2 and 9,
    inclusive.  For example:
    
        ASSOC .pl=PerlScript
        FTYPE PerlScript=perl.exe %1 %*
    
    would allow you to invoke a Perl script as follows:
    
        script.pl 1 2 3
    
    If you want to eliminate the need to type the extensions, then do the
    following:
    
        set PATHEXT=.pl;%PATHEXT%
    
    and the script could be invoked as follows:
    
        script 1 2 3
    
  • ASSOC /?

    Displays or modifies file extension associations
    
    ASSOC [.ext[=[fileType]]]
    
      .ext      Specifies the file extension to associate the file type with
      fileType  Specifies the file type to associate with the file extension
    
    Type ASSOC without parameters to display the current file associations.
    If ASSOC is invoked with just a file extension, it displays the current
    file association for that file extension.  Specify nothing for the file
    type and the command will delete the association for the file extension.
    
查看更多
登录 后发表回答