Scherf
Scherf

Reputation: 1567

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?

Upvotes: 33

Views: 419630

Answers (7)

Lucas Coelho
Lucas Coelho

Reputation: 1602

Using CMD you can run your python scripts as long as the installed python is added to the path with the following line:

C:\Python27;

The (27) is example referring to version 2.7, add as per your version.

Path to system path:

Control Panel => System and Security => System => Advanced Settings => Advanced => Environment Variables.

Under "User Variables," append the PATH variable to the path of the Python installation directory (As above).

Once this is done, you can open a CMD where your scripts are saved, or manually navigate through the CMD.

To run the script enter:

C:\User\X\MyScripts>python ScriptName.py

Upvotes: 2

ajknzhol
ajknzhol

Reputation: 6460

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.

Upvotes: 10

Sukrit Kalra
Sukrit Kalra

Reputation: 34543

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:

Start:

cd C:\Python27
python test.py

Session transcript:

Enter your name: Monty Python
Hello, Monty Python

Upvotes: 39

Whitney Kretz
Whitney Kretz

Reputation: 1

  1. Go to the Python Website/downloads/Windows. Download the Windows x86-64 embeddable ZIP file.

  2. Open Windows Explorer

  • Open zipped folder python-3.7.0.

  • In the Windows toolbar, with the Red flair saying “Compressed Folder Tool”, press the “Extract” button on the tool bar with “File” “Home “Share” “View”

  • Select Extract all

  • The Extraction process is not covered yet

  • Once extracted, save it onto an SSD or fastest memory device. Not USB. HDD is fine.

SDD
Users/butte/ProgramFiles blah blah ooooor
D:\Python

Or hook up to your cloud.

  1. Click your User Icon in the Windows tool bar.
  • Search environment variable

  • Proceed with progressing with “Environment Variables” button press

  • Under the “user variables” table select “New..”

  • After the Canvas of Information, add Python.

  • In Variable Name, select the “D:\Python\python-3.7.0-embed-amd64\python.exe;”

  • click OK

  • Under the “System Variables” label and in the Canvas the first row has a value marked “Path”

  • Select “Edit” when “Path” is highlighted.

  • Select “New”

  • Enter D:\Python\python-3.7.0-embed-amd

  • Click OK

  • Ok.

  • Save and double check

  • Open PowerShell

     python --help
    
     python --version
    

Source to tutorial:

https://thedishbunnybitch.com/2018/08/11/installing-python-on-windows-10-for-powershell/

Upvotes: -2

Mustafa jahjoum
Mustafa jahjoum

Reputation: 15

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

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

To change the execution policy, use the following procedure:

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

  2. At the command prompt, type:

    Set-ExecutionPolicy AllSigned

    -or-

    Set-ExecutionPolicy RemoteSigned

The change is effective immediately.

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

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

C:\Scripts\Get-ServiceLog.ps1

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

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

Upvotes: -1

user2145645
user2145645

Reputation:

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

You are getting this error as you are inside the Python interpreter which was triggered by the command python you have entered in the terminal (Windows PowerShell).

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

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

Upvotes: 1

David Douglas
David Douglas

Reputation: 10503

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;

Upvotes: 5

Related Questions