本文介绍了命令提示符返回空白,但是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文件关联指针




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

  2. 键入 ASSOC .py = PythonScript 并按

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

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






仅使用脚本名称从Shell执行



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


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

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

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






其他资源





  • FTYPE /?


  • 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

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


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

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

08-04 14:05
查看更多