命令提示符返回空白,但是python控制台没有 [英] Command prompt returning blank, but python console doesn't

查看:440
本文介绍了命令提示符返回空白,但是python控制台没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,标题有点混乱;我不确定该如何表达。我是我正在学习的语言学课程的初学者,但是将某些脚本转移到命令提示符中时遇到了一些问题。我正在使用Windows 8.1和python 3.6.0,如果有帮助的话。



我遇到的问题是我可以在python中编写脚本。 exe程序,它将完全按照应有的方式工作。然后,我将使用命令提示符运行完全相同的脚本,它返回的唯一内容是空行。



例如,我可以手动将其键入python控制台并获得正确的输出:

 >> print( hello)
你好

但是如果我将其保存在Notepad ++文件中并使用命令提示符执行相同的脚本,它将看起来像这(请原谅我的屠杀道路休闲活动):

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

C:\Users\用户名\Python>

hello.py是我尝试使用与手动输入完全相同的代码运行的脚本在Python控制台中,但是当我尝试打开文件时,它什么也没做。



虽然我尝试运行的每个脚本都不会发生这种情况,但是我无法弄清楚是什么触发了这种情况。我没有遇到任何错误,所以我不知道如何进行反复试验。这使得添加元素时检查工作变得越来越困难,而且我不确定如何解决此问题。

解决方案

仅使用脚本名称从Windows命令行运行Python脚本,

h2>

配置Windows文件关联指针


按照下面的#3设置python.exe的完整显式路径,以获取
正确值,该值在您的系统上





  1. 以管理员身份打开提升的[cmd.exe]命令提示符

  2. 键入 ASSOC .py = PythonScript 并按 Enter

  3. 键入 FTYPE PythonScript = C:\程序文件\Python\python.exe%1%* 并按 Enter

  4. 键入 SET PATHEXT = .py;%PATHEXT%并按 Enter






仅使用脚本名称从Shell执行



如果你是真的ly需要从命令行运行脚本,而无需告知shell python脚本文件的完整显式路径,然后需要将此脚本所在的路径添加到%PATH%环境变量。


  1. 以管理员身份打开提升的[cmd.exe]命令提示符

  2. 键入 SET PATH =%PATH%; C:\Program Files\Python ,其中 C: \程序文件\Python 是系统上存在的值。

现在您可以只需键入脚本名称(带或不带文件扩展名),而无需对另一个目录进行 CD 或显式指定python脚本的完整路径。






其他资源




  • Windows环境变量


  • FTYPE /?


     显示或修改文件扩展名关联中使用的文件类型

    FTYPE [fileType [= [openCommandString]]]

    fileType指定要检查或更改的文件类型
    openCommandString指定启动此类文件
    时要使用的打开命令。

    键入FTYPE,不带参数显示
    定义了打开命令字符串的当前文件类型。仅使用
    文件类型调用FTYPE,它将显示该文件类型的当前打开命令字符串。
    不为打开命令字符串指定任何内容,FTYPE命令将使
    删除文件类型的打开命令字符串。在打开的
    命令字符串中,%0或%1替换为通过关联启动的文件名
    。 %*获取所有参数,%2
    获取第一个参数,%3第二个,依此类推。%〜n获取所有剩余的
    参数,从第n个参数开始,其中n可以在2到2之间。 9,包括
    美元。例如:

    ASSOC .pl = PerlScript
    FTYPE PerlScript = perl.exe%1%*

    将允许您按以下方式调用Perl脚本:

    script.pl 1 2 3

    如果要消除键入扩展名的必要,请执行以下


    设置PATHEXT = .pl;%PATHEXT%

    并可以按以下方式调用脚本:

    脚本1 2 3



  • ASSOC /?


     显示或修改文件扩展名关联

    ASSOC [.ext [= [fileType]]]

    .ext指定将文件类型与
    关联的文件扩展名fileType指定与文件扩展名

    关联的文件类型键入不带参数的ASSOC以显示当前文件关联。
    如果仅使用文件扩展名调用ASSOC,它将显示该文件扩展名的当前
    文件关联。不为文件
    类型指定任何内容,该命令将删除文件扩展名的关联。




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.

解决方案

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.
    

这篇关于命令提示符返回空白,但是python控制台没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆