Ryan Holmes
Ryan Holmes

Reputation: 121

Running python scripts without the word "python" - command prompt

I need to be able to run python scripts by simple typing helloworld.py into command prompt rather than python helloworld.py. At the moment it opens a code editor rather than executing the script.

I found a stack overflow answer from 2012 (Set up Python on Windows to not type python in cmd) that said you can do this:

C:\> assoc .py=Python C:\> ftype Python="C:\python26\python.exe %1 %*"

But it didn't work for me. I'm on a windows 10 computer but have also tried to get it working on a windows 7 computer. I tried using python27 as well.

Upvotes: 1

Views: 3687

Answers (3)

Ramy Helmy
Ramy Helmy

Reputation: 11

Open command prompt with Administrative privileges

  1. assoc .py=Python Command
  2. ftype Python="~path/python.exe" "%1" %*
    after executing the above commands, you can run a python program simply by typing a program name like hello.py

Upvotes: 1

Edgard Knive
Edgard Knive

Reputation: 835

It seems like you have some misconfiguration (did you install from python.org or through Windows Store?). As previously stated by @Professor_Joykill the documentation explains;

On Windows, the standard Python installer already associates the .py extension with a file type (Python.File) and gives that file type an open command that runs the interpreter (D:\Program Files\Python\python.exe "%1" %*). This is enough to make scripts executable from the command prompt as ‘foo.py’. If you’d rather be able to execute the script by simple typing ‘foo’ with no extension you need to add .py to the PATHEXT environment variable.

Which means that after you have installed (or tried to restart) you should be able to run a script from the current folder by simply using foo.py. If you want to run a script by simply typing foo you need to add the following to PATHEXT:

Press WIN button, then type Edit the system environment variables and click on Environment Variables in the dialog box that shows up. From there scroll to PATHEXT in the lower list named System variables, click on it and verify that you see something similar to what is show in the image below.

pathext

It could be that you also need to reboot or at least restart the command prompt or powershell instance to see the effects. (Also do this if you've reinstalled Python)

If this doesn't work, you need to make sure that py files have actually been association to python through the default application interface in windows:

File association

Microsoft, with it's anti-customer approach has of course made it hard to change these to software installed by means other than through Microsoft Store. So to make changes here, you will either have to go the Registry route, or use a program like Default Programs Editor. Alternatives (and this program) can be seen here.

Upvotes: 0

Professor_Joykill
Professor_Joykill

Reputation: 989

Here is a description of how to run python files on windows terminal from the python documentation. It looks like what you have in that second line, but also offers some explanation.

Upvotes: 0

Related Questions