Running Python in PowerShell?

I am attempting to learn the very basics of Python using the guide "Learn Python the hard way" by Zed A. Shaw. The problem that I am having is that I can run Python scripts, but only when using .\ in front of the name. This opens up CMD for a split second and then closes.

If I attempt to run the file it returns that the file is not an operable program file, script, etc..

I've found multiple questions on Stack Overflow that relate to this question, but none of the solutions have worked for me.

Two things I've tried:

[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")

and

$env:PATH =$env:PATH+";."

Source: (How do you remove the PowerShell requirement that scripts and executables be preceded by ".\"?)

When I check the environment variable PATH it has the correct path within it, so what other things could be causing this?


Solution 1:

Since, you are able to run Python in PowerShell. You can just do python <scriptName>.py to run the script. So, for a script named test.py containing

name = raw_input("Enter your name: ")
print "Hello, " + name

The PowerShell session would be

PS C:\Python27> python test.py
Enter your name: Monty Python
Hello, Monty Python
PS C:\Python27>

Solution 2:

As far as I have understood your question, you have listed two issues.

PROBLEM 1:

You are not able to execute the Python scripts by double clicking the Python file in Windows.

REASON:

The script runs too fast to be seen by the human eye.

SOLUTION:

Add input() in the bottom of your script and then try executing it with double click. Now the cmd will be open until you close it.

EXAMPLE:

print("Hello World")
input()

PROBLEM 2:

./ issue

SOLUTION:

Use Tab to autocomplete the filenames rather than manually typing the filename with ./ autocomplete automatically fills all this for you.

USAGE:

CD into the directory in which .py files are present and then assume the filename is test.py then type python te and then press Tab, it will be automatically converted to python ./test.py.

Solution 3:

Go to Control PanelSystem and SecuritySystem, and then click Advanced system settings on the left hand side menu.

On the Advanced tab, click Environment Variables.

Under 'User variables' append the PATH variable with path to your Python install directory:

C:\Python27;