1

Following is a summary of the problem along with relevant info about OS, System Environment Variables.

Upon starting PowerShell, screen shows

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.

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.

Operating System:

  • Windows 7 Professional 64-bit SP1
    Installation Date: 4/3/2015 10:13:51 PM

.NET Frameworks installed:

  • v4.5 Full
  • v4.5 Client
  • v3.5 SP1
  • v3.0 SP2
  • v2.0 SP2
2
  • 3
    You are already in python so trying to run python again doesn't work. I don't know powershell and microsurf likely made it completely incomprehensible but how about instead of Python as the first powershell command, try python CleanupStops.py or even just CleanupStops.py as your Windows will likely be configured to associate .py files with python. Commented Dec 13, 2015 at 18:32
  • Just directly call python CleanupStops.py as advised above. microsurf likely made it completely incomprehensible ? PowerShell is quite comprehensible, don't say things like this. Commented Dec 13, 2015 at 18:59

1 Answer 1

4

Your first Python statement started the Python interpreter in interactive mode. If you want to run a Python script from there you need something like this (assuming that the file is in the current directory):

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

Type exit() and press Enter to quit the interactive interpreter.

A statement python CleanupStops.py is used when you want to run a Python script directly from PowerShell or CMD:

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:

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). If the directory is not listed in $env:Path you need to call the script with its absolute or relative 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:

[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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.