简体   繁体   中英

Powershell prompt returning prompt but not Python output when trying to run a python program from Powershell

I am trying to run python from the powershell, I go to the python directory using

cd f:\python27

If i type python i get the following error (despite it being a exe file in the folder):

python : The term 'python' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:1
+ python
+ ~~~~~~
+ CategoryInfo          : ObjectNotFound: (python:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Suggestion [3,General]: The command python was not found, but does exist in the 
current location. Windows PowerShell does not load commands from the current 
location by default. If you trust this command, instead type: ".\python". See "get-
help about_Command_Precedence" for more details.

If I type .\\python it just does nothing and returns the prompt again. I created a simple file called helloworld.py in a text editor and saved it into the python27 directory, which contains one line:

print "Hello World!"

and I tried typing both

python helloworld.py
.\python helloworld.py

The first returns the original error and the second returns the prompt again without apparently doing anything, I cant get the python program to run. I am new to Powershell and Pyton and I am trying to work my way through learn python the hard way, can anyone help or point where I am going wrong and explain what it is I am doing wrong and what the solution is? thanks

You should be able to get it working by telling Powershell where to find Python. This can be done by setting an Environment Variable.

Open Powershell as an admin. Run this code:

[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Program Files\Python35-32")

Note : Here, in Path, add the path where you've installed Python in your system, here in my case it is “C:\\Program Files\\Python35-32”.

This should help with the issue.
To thest, try running:

python --version

It should display the version number of Python installed in the system, this is what it showed in my system:

“Python 3.5.1″

I've not figured this out myself but as a workaround, either run

cmd /C python helloworld.py

or

python helloworld.py COMMAND_LINE_ARGUMENTS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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