Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 555
Scherf Avatar asked Oct 30 '13 07:10

Scherf


People also ask

How do I run Python on Windows shell?

To run the Python Shell, open the command prompt or power shell on Windows and terminal window on mac, write python and press enter. A Python Prompt comprising of three greater-than symbols >>> appears, as shown below. Now, you can enter a single statement and get the result.

Can I run Python in PowerShell?

With your PowerShell command line open, enter python to run the Python 3 interpreter. (Some instructions prefer to use the command py or python3 , these should also work).


1 Answers

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> 
like image 185
Sukrit Kalra Avatar answered Sep 23 '22 03:09

Sukrit Kalra