Running Python in PowerShell?

I am trying to learn the very basics of Python using Zed A. Shaw using the Learn Python the hard way tutorial. The problem I am facing is that I can run Python scripts, but only when using .\ front of the name. This opens the CMD for a second and then closes.

If I try to run the file, it will return that the file is not an executable program file, script, etc.

I found a few stack overflow questions that relate to this question, but none of the solutions worked for me.

Two things I've tried:

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

and

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

Source: ( How do I remove a PowerShell request preceded by scripts and ". \" Executables? )

When I check the PATH environment variable, it has the correct path inside it, so what other things can cause this?

+7
python powershell
source share
5 answers

Since you can run Python in PowerShell. You can simply execute python <scriptName>.py to run the script. So for a script called test.py containing

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

PowerShell session will be

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

As far as I understand your question, you have indicated two questions.

PROBLEM 1:

You cannot execute Python scripts by double-clicking the Python file on Windows.

CAUSE:

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

DECISION:

Add input() at the bottom of your script, and then try to double-click it. Now cmd will be open until you close it.

Example:

 print("Hello World") input() 

PROBLEM 2:

./ issue

DECISION:

Use the tab to autocomplete file names rather than manually entering a file name using ./ autocomplete will automatically fill it all out for you.

APPLICATION:

CD into the directory in which the .py files are present, and then suppose the file name is test.py , then type python te and then press Tab , this will be automatically converted to python ./test.py .

+5
source share

Go to Control Panel β†’ System and Security β†’ System, and then click β€œAdvanced System Settings” on the left side menu.

On the Advanced tab, click Environment Variables.

In the Custom Variables section, add the PATH variable with the path to your Python installation directory:

 C:\Python27; 
+1
source share

The command [Environment] :: SetEnvironmentVariable ("Path", "$ env: Path; C: \ Python27", "User") is not a Python command. Instead, it is the operating system command to set the PATH variable.

You get this error because you are inside the Python interpreter that was invoked by the python command that you entered into the terminal (Windows PowerShell).

Note >>> on the left side of the line. It states that you are inside the Python interpreter.

Type quit () to exit the Python interpreter and enter the command. It should work!

0
source share

The default execution policy, Limited, prevents all scripts from running, including scripts that you write on the local computer.

The execution policy is stored in the registry, so you only need to change it once on each computer.

To change the execution policy, complete the following procedure:

  • Start Windows PowerShell with the option "Run as administrator".

  • At the command prompt, type:

    Set-ExecutionPolicy AllSigned

    -or -

    Set-ExecutionPolicy RemoteSigned

This change is effective immediately.

To run the script, enter the full name and full path to the script file.

For example, to run the Get-ServiceLog.ps1 script in the C:\Scripts , type:

 C:\Scripts\Get-ServiceLog.ps1 

And for the Python file, you have two points. Try adding your Python folder to your PATH and the .py extension.

In PATHEXT from the go properties of a computer. Then click on advanced system protection. Then the environment variable. Here you will find two points.

0
source share

All Articles