简体   繁体   English

从PowerShell语法错误中运行Python脚本

[英]Running Python Script from PowerShell Syntax Error

Following is a summary of the problem along with relevant info about OS, System Environment Variables. 以下是问题的摘要以及有关操作系统,系统环境变量的相关信息。

Upon starting PowerShell, screen shows 启动PowerShell后,屏幕显示

Windows Powershell  
Copyright © 2009 Microsoft Corporation  
PS C:\Users>

My Input is: 我的输入是:

Python 

Output is: 输出为:

Python 2.7.4 default Apr 8 2013 19:54:46) [MSC v.1500 32 bit (Intel)] on win32"  
Type “help” ……etc.

My next input is: 我的下一个输入是:

Python CleanupStops.py 

Output is: 输出为:

File <stdin>,line 1  
Python CleanupStops.py  
              ^ "  
Syntax Error: invalid syntax"   

The script resides in the directory pyscripts which is set in the System Environment Variables as shown below. 该脚本位于目录pyscripts中,该目录在“系统环境变量”中设置,如下所示。

C:\Python27;C:\Python27\Tools\Scripts\pyscripts;C:\Python27\Lib\site-packages; 

Note that my OS is 64-bit SPI while Python 2.7.4 is 32 bit Intel. 请注意,我的操作系统是64位SPI,而Python 2.7.4是32位Intel。

Operating System: 操作系统:

  • Windows 7 Professional 64-bit SP1 Windows 7专业版64位SP1
    Installation Date: 4/3/2015 10:13:51 PM 安装日期:2015/4/3 10:13:51 PM

.NET Frameworks installed: 安装的.NET Framework:

  • v4.5 Full v4.5完整版
  • v4.5 Client v4.5客户端
  • v3.5 SP1 v3.5 SP1
  • v3.0 SP2 v3.0 SP2
  • v2.0 SP2 v2.0 SP2

Your first Python statement started the Python interpreter in interactive mode. 您的第一条Python语句以交互方式启动Python解释器。 If you want to run a Python script from there you need something like this (assuming that the file is in the current directory): 如果要从那里运行Python脚本,则需要类似以下内容(假设文件位于当前目录中):

>>> import os
>>> os.system('.\\CleanupStops.py')

Type exit() and press Enter to quit the interactive interpreter. 键入exit()并按Enter退出交互式解释器。

A statement python CleanupStops.py is used when you want to run a Python script directly from PowerShell or CMD: 当您要直接从PowerShell或CMD运行Python脚本时,将使用python CleanupStops.py语句:

PS C:\> python C:\Python27\Tools\Scripts\pyscripts\CleanupStops.py

or 要么

PS C:\> Set-Location C:\Python27\Tools\Scripts\pyscripts
PS C:\Python27\Tools\Scripts\pyscripts> python CleanupStops.py

If you associated .py files with the Python interpreter during installation you should even be able to run Python scripts directly (without explicitly specifying the interpreter), as @tdelaney pointed out in the comments to your question: 如果在安装过程中将.py文件与Python解释器相关联,您甚至应该能够直接运行Python脚本(无需显式指定解释器),如@tdelaney在对问题的评论中指出的那样:

PS C:\> CleanupStops.py

Note, however, that the latter (calling the script without interpreter and path) will only work if the directory containing the script is listed in the $env:Path environment variable (and the extension .py is listed in the $env:PATHEXT variable, as @eryksun pointed out in the comments). 但是请注意,只有在$env:Path环境变量中列出了包含该脚本的目录(并且$env:PATHEXT变量中列出了扩展名.py )时,后者(不带解释器和路径的脚本调用)才有效。 ,如@eryksun在评论中指出的)。 If the directory is not listed in $env:Path you need to call the script with its absolute or relative path. 如果该目录未在$env:Path列出,则需要使用其绝对或相对路径来调用该脚本。

PS C:\Python27\Tools\Scripts\pyscripts> .\CleanupStops.py

If the installer did not prepare the environment properly you can easily do it yourself. 如果安装程序未正确准备环境,则您可以自己轻松地进行安装。 Just run the following 4 statements from an elevated PowerShell prompt: 只需在提升的PowerShell提示符下运行以下4条语句:

[environment]::SetEnvironmentVariable('Path', "$env:Path;C:\python27", 'Machine')
[environment]::SetEnvironmentVariable('PATHEXT', "$env:PATHEXT;.py", 'Machine')
cmd /c 'assoc .py=Python.File'
cmd /c 'ftype Python.File="C:\python27\python.exe" "%1" %*'

The new settings become effective the next time you start PowerShell (or CMD). 下次启动PowerShell(或CMD)时,新设置将生效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM