32

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Scherf
  • 1,527
  • 4
  • 16
  • 22
  • What happens when you type `python` into Powershell? – Sukrit Kalra Oct 30 '13 at 07:37
  • It seems to run properly (as far as I know) it displays the version of python and I can close it using Quit(). – Scherf Oct 30 '13 at 07:39
  • When I run the script using .\, and it opens in CMD, the function it is supposed to perform (printing a line of text) works just fine. – Scherf Oct 30 '13 at 07:42
  • Have tried to Run the script using `python `? – salezica Oct 30 '13 at 07:42
  • 1
    Does this answer your question? [I'm trying to use python in powershell](https://stackoverflow.com/questions/11813435/im-trying-to-use-python-in-powershell) – halt9k Jan 18 '23 at 08:48

7 Answers7

37

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
  • Oh, well, that makes sense. I was assuming that since powershell runs things just by referincing filename (not including a 'run' function) that it would be able to run the file with just 'test.py'. I see now that everything was in proper order and it was just my failure to read properly that got the best of me! Thank you for the swift response – Scherf Oct 30 '13 at 07:47
9

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ajknzhol
  • 6,322
  • 13
  • 45
  • 72
5

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;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David Douglas
  • 10,377
  • 2
  • 55
  • 53
2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    About 70% of this answer has been ***[plagiarised](https://en.wiktionary.org/wiki/plagiarism#Noun)*** (wholesale copied) from *[about_Scripts](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scripts)* (Microsoft documentation). For instance, the exact phase *"The default execution policy, "Restricted", prevents all scripts from running, including scripts that you write on the local computer."* – Peter Mortensen Aug 07 '23 at 13:54
  • cont' - And the exact phrase *"The execution policy is saved in the registry, so you need to change it only once on each computer."*. (Note: The first may not match due to the formatting, but use the beginning or the end of the phrase) – Peter Mortensen Aug 07 '23 at 13:54
2

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
Lucas Coelho
  • 1,492
  • 1
  • 9
  • 14
1

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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
-2
  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/

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 6
    Did you read and understand the simplicity of the already accepted answer? How do these garbled instructions help? – chb Aug 11 '18 at 19:58
  • Was it wholesale copied from the indicated source? Or from some other web page? Related: *[What do we do with answers that are entirely copied and improperly attributed (only a "reference" link or similar is included)?](https://meta.stackoverflow.com/questions/321299/)* – Peter Mortensen Aug 07 '23 at 13:12
  • The link is broken: *"Hmm. We’re having trouble finding that site. We can’t connect to the server at thedishbunnybitch.com"*. Was it a bogus link? – Peter Mortensen Aug 07 '23 at 13:14
  • @chb: Single newlines don't render here and it was probably copied directly from a web page (perhaps the indicated one) – Peter Mortensen Aug 07 '23 at 13:15