How do I run a Python program in the Command Prompt in Windows 7?
Asked Answered
A

24

168

I'm trying to figure out how to run Python programs with the Command Prompt on Windows 7. (I should have figured this out by now...)

When I typed "python" into the command prompt, I got the following error:

'python' is not recognized as an internal or external command, operable program or batch file.

The first place I found when looking for help was this site: http://docs.python.org/faq/windows.html#how-do-i-run-a-python-program-under-windows.

It was somewhat helpful, but the tutorial was written for Windows 2000 and older, so it was minimally helpful for my Windows 7 machine. I attempted the following:

For older versions of Windows the easiest way to do this is to edit the C:\AUTOEXEC.BAT >file. You would want to add a line like the following to AUTOEXEC.BAT:

This file did not exist on my machine (unless I'm mistaken).

Next, I tried this: (here: How do I run a Python program?)

Putting Python In Your Path

Windows

In order to run programs, your operating system looks in various places, and tries to match the name of the program / command you typed with some programs along the way.

In windows:

control panel > system > advanced > |Environmental Variables| > system variables -> Path

this needs to include: C:\Python26; (or equivalent). If you put it at the front, it will be the first place looked. You can also add it at the end, which is possibly saner.

Then restart your prompt, and try typing 'python'. If it all worked, you should get a ">>>" prompt.

This was relevant enough for Windows 7, and I made my way to the System Variables. I added a variable "python" with the value "C:\Python27"

I continued to get the error, even after restarting my computer.

Anyone know how to fix this?

Abject answered 6/1, 2011 at 23:51 Comment(0)
B
233

You need to add C:\Python27 to your system PATH variable, not a new variable named "python".

Find the system PATH environment variable, and append to it a ; (which is the delimiter) and the path to the directory containing python.exe (e.g. C:\Python27). See below for exact steps.

The PATH environment variable lists all the locations that Windows (and cmd.exe) will check when given the name of a command, e.g. "python" (it also uses the PATHEXT variable for a list of executable file extensions to try). The first executable file it finds on the PATH with that name is the one it starts.

Note that after changing this variable, there is no need to restart Windows, but only new instances of cmd.exe will have the updated PATH. You can type set PATH at the command prompt to see what the current value is.


Exact steps for adding Python to the path on Windows 7+:

  1. Computer -> System Properties (or Win+Break) -> Advanced System Settings
  2. Click the Environment variables... button (in the Advanced tab)
  3. Edit PATH and append ;C:\Python27 to the end (substitute your Python version)
  4. Click OK. Note that changes to the PATH are only reflected in command prompts opened after the change took place.
Boarfish answered 6/1, 2011 at 23:55 Comment(6)
Thanks for that, was about to throw in my Python chops before I even earned them. I'm sure people can figure this out but if not remember if you download Python 3.4 your path variable will be C:\Python34 instead of 27!Pervious
Another potential issue to check for those who follow the above, and still have an issue, I have seen a mysterious double quote in the path (after C:\Program Files (x86)\QuickTime\QTSystem";) that would not let the python command work. removing it did the trick.Keyboard
You mentioned "Do not forget the trailing \". May I ask why it is important?Debonair
@MBA: I had a slash in the answer and somebody else added the note later on. It was just for clarity, it's not necessary at all. I've cleaned up the answer somewhat. Thanks for pointing this out!Boarfish
For windows 10 it was needed for me to restart windows.Hello
then you just need to write in the cmd: python36 "C:\Users\John\Desktop\my script.py"Sailplane
N
66

Assuming you have Python2.7 installed

  1. Goto the Start Menu

  2. Right Click "Computer"

  3. Select "Properties"

  4. A dialog should pop up with a link on the left called "Advanced system settings". Click it.

  5. In the System Properties dialog, click the button called "Environment Variables".

  6. In the Environment Variables dialog look for "Path" under the System Variables window.

  7. Add ";C:\Python27" to the end of it. The semicolon is the path separator on windows.

  8. Click Ok and close the dialogs.

  9. Now open up a new command prompt and type "python"

It should work.

Nautch answered 7/1, 2011 at 0:2 Comment(1)
I vote you because I had a problem. I added correctly C:\Python27. With an ; after the previouse variable. But I didn't notice that I wrote an space after the ; . Then the command prompt didn't work. Thank you I dind't notice that. CORRECT: ;C:\Python27 || WRONG: ; C:\Python27Meteoric
A
38

It has taken me some effort looking for answers here, on the web, and and in the Python documentation, and testing on my own, to finally get my Python scripts working smoothly on my Windows machines (WinXP and Win7). So, I just blogged about it and am pasting that below in case it's useful to others. Sorry it's long, and feel free to improve it; I'm no expert.

[UPDATE: Python 3.3 now includes the Python Launcher for Windows, which allows you to type py (rather than python) to invoke the default interpreter, or py -2, py -3, py -2.7, etc. It also supports shebang lines, allowing the script itself to specify. For versions prior to 3.3, the launcher is available as a separate download. http://docs.python.org/3/whatsnew/3.3.html ]

Running Python scripts conveniently under Windows

Maybe you're creating your own Python scripts, or maybe someone has given you one for doing something with your data files. Say you've acquired a Python script and have saved it to "D:\my scripts\ApplyRE.py". You want to run it conveniently by either double-clicking it or typing it into the command line from any location, with the option of passing parameters to it like this (-o means "overwrite the output file if it already exists"):

ApplyRE infile.txt outfile.txt -o

Say you also have a data file, "C:\some files\some lexicon.txt". The simplest option is to move the file or the script so they're in the same location, but that can get messy, so let's assume that they'll stay separate.

Making sure Windows can find the Python interpreter

After installing Python, verify that typing python into a command prompt works (and then type exit() to get back out of the Python interpreter).

C:\>python
Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
C:\>

If this doesn't work, you'll need to append something like ";C:\Python32" (without quotes) to the PATH environment variable. See PATHEXT below for instructions.

Associating Python with .py and .pyc

Verify that double-clicking on ApplyRE.py runs it. (It should also have a Python logo as its icon and be labeled "Python File", by the way.) If this isn't already done, right-click on a .py file, choose Open With, Choose Program, and check "Always use..." This association improves convenience but isn't strictly necessary--you can specify "python" every time you want to run a script, like this:

D:\my scripts>python ApplyRE.py lexicon-sample.txt -o
Running... Done.

Here's a very specific variation, which is optional unless you need to specify a different version of the interpreter.

D:\my scripts>c:\python32\python ApplyRE.py lexicon-sample.txt -o
Running... Done.

But that's a pain. Fortunately, once Python is installed, in the PATH, and associated with .py, then double-clicking a .py file or directly typing it as a command should work fine. Here, we seem to be running the script directly--it's nice and simple to run it on a sample file that's located in the "my scripts" folder along with the script.

D:\my scripts>ApplyRE.py lexicon-sample.txt -o
Running... Done.

Omitting the .py extension (editing PATHEXT)

To further reduce typing, you can tell Windows that .py (and perhaps .pyc files) are executable. To do this, right-click Computer and choose Properties, Advanced, Environment Variables, System Variables. Append ";.PY;.PYC" (without quotes) to the existing PATHEXT variable, or else create it if you're certan it doesn't exist yet. Close and reopen the command prompt. You should now be able to omit the .py (FYI, doing so would cause ApplyRE.exe or ApplyRE.bat to run instead, if one existed).

D:\my scripts>ApplyRE lexicon-sample.txt -o
Running... Done.

Adding scripts to the system PATH

If you're going to use your scripts often from the command prompt (it's less important if doing so via using BAT files), then you'll want to add your scripts' folder to the system PATH. (Next to PATHEXT you should see a PATH variable; append ";D:\my scripts" to it, without quotes.) This way you can run a script from some other location against the files in current location, like this:

C:\some files>ApplyRE "some lexicon.txt" "some lexicon OUT.txt" -o
Running... Done.

Success! That's pretty much all you need to do to streamline the command-line.

Running directly without tweaking the PATH

If you're a fast typist or don't mind creating a batch file for each situation, you can specify full paths (for the script, or for the parameters) instead of tweaking PATH.

C:\some files>"d:\my scripts\ApplyRE.py" "some lexicon.txt" "some lexicon OUT.txt" -o
Running... Done.
C:\some files>d:
D:\>cd "my scripts"
D:\my scripts>ApplyRE.py "c:\some files\some lexicon.txt" "c:\some files\some lexicon OUT.txt" -o
Running... Done.

Creating shortcuts or batch files

If .py is associated with an installed Python, you can just double-click ApplyRE.py to run it, but the console may appear and disappear too quickly to read its output (or failure!). And to pass parameters, you'd need to first do one of the following. (a) Right-click and create a shortcut. Right-click the shortcut to edit properties and append parameters to Target. (b) Create a batch file--a plain text file with a distinct name such as ApplyRErun.bat. This option is probably better because you can ask it to pause so you can see the output. Here is a sample BAT file's contents, written to be located and run from c:\some files .

python "d:\my scripts\ApplyRE.py" "some lexicon.txt" "some lexicon OUT.txt" -o
pause

Advanced: appending to PYTHONPATH

This usually isn't necessary, but one other environment variable that may be relevant is PYTHONPATH. If we were to append d:\my scripts to that variable, then other Python scripts in other locations could make use of those via import statements.

Abomb answered 23/11, 2012 at 18:5 Comment(1)
Your answer explains why .py files run magically on Windows 7 without a python prefix and without a shebang line. Thanks.Conduct
N
9

Python comes with a script that takes care of setting up the windows path file for you.

After installation, open command prompt

cmd

Go to the directory you installed Python in

cd C:\Python27

Run python and the win_add2path.py script in Tools\Scripts

python.exe Tools\Scripts\win_add2path.py

Now you can use python as a command anywhere.

Nett answered 7/8, 2014 at 2:8 Comment(3)
On Windows 10 cmd would still throw the same error after running this script ('python' is not recognized ...). After inspecting the PATH I noticed that the relevant lines had indeed been added, but only to the "user variables", not to "system variables". I copied them manually to "system variables" and then it worked.Serdab
@Serdab did you close and open a fresh terminal after running the add script before testing python? Usually adding this to the PATH under user variables is fine but requires a fresh command prompt to load that updated PATHNett
Good point to restart. I honestly don't remember if I tried that before copying manually.Serdab
E
7

You have to put the python path in the PATH variable.

In the System Variables section, you should have User Variables and System Variables. Search for the PATH variable and edit its value, adding at the end ;C:\python27.

The ; is to tell the variable to add a new path to this value, and the rest, is just to tell which path that is.

On the other hand, you can use ;%python% to add the variable you created.

Eldwon answered 6/1, 2011 at 23:56 Comment(0)
K
4

Even after going through many posts, it took several hours to figure out the problem. Here is the detailed approach written in simple language to run python via command line in windows.

1. Download executable file from python.org
Choose the latest version and download Windows-executable installer. Execute the downloaded file and let installation complete.

2. Ensure the file is downloaded in some administrator folder

  1. Search file location of Python application.
  2. Right click on the .exe file and navigate to its properties. Check if it is of the form, "C:\Users....". If NO, you are good to go and jump to step 3. Otherwise, clone the Python37 or whatever version you downloaded to one of these locations, "C:\", "C:\Program Files", "C:\Program Files (x86)".

3. Update the system PATH variable This is the most crucial step and there are two ways to do this:- (Follow the second one preferably)

1. MANUALLY
- Search for 'Edit the system Environment Variables' in the search bar.(WINDOWS 10)
- In the System Properties dialog, navigate to "Environment Variables".
- In the Environment Variables dialog look for "Path" under the System Variables window. (# Ensure to click on Path under bottom window named System Variables and not under user variables)
- Edit the Path Variable by adding location of Python37/ PythonXX folder. I added following line:-
" ;C:\Program Files (x86)\Python37;C:\Program Files (x86)\Python37\Scripts "
- Click Ok and close the dialogs.

2. SCRIPTED
- Open the command prompt and navigate to Python37/XX folder using cd command.
- Write the following statement:-
"python.exe Tools\Scripts\win_add2path.py"

You can now use python in the command prompt:)
1. Using Shell
Type python in cmd and use it.
2. Executing a .py file
Type python filename.py to execute it.

Kreisler answered 5/10, 2018 at 13:24 Comment(0)
S
3

You don't add any variables to the System Variables. You take the existing 'Path' system variable, and modify it by adding a semi-colon after, then c:\Python27

Sixteenth answered 6/1, 2011 at 23:55 Comment(0)
H
3

So after 30 min of R&D i realized that after setup the PATH at environment variable

i.e.

" C:\Python/27; "

just restart

now open cmd :

C:> cd Python27 C:\ Python27> python.exe

USE python.exe with extension

alternative option is :

if the software is installed properly directly run Python program, your command line screen will automatically appear without cmd.

Thanks.

Hurwitz answered 8/9, 2014 at 14:18 Comment(0)
D
3
  • Go to the Start Menu

  • Right Click "Computer"

  • Select "Properties"

  • A dialog should pop up with a link on the left called "Advanced system settings". Click it.

  • In the System Properties dialog, click the button called "Environment Variables".

  • In the Environment Variables dialog look for "Path" under the System Variables window.

  • Add ";C:\Python27" to the end of it. The semicolon is the path separator on windows.

  • Click Ok and close the dialogs.

  • Now open up a new command prompt and type "python" or if it says error type "py" instead of "python"

Dashed answered 9/12, 2016 at 14:30 Comment(1)
Perhaps you could add some explanation as to why OP might have experienced this problem/ why your answer fixes it?Furthermore
D
2

First make sure you enter the path environment variable

C:\ path %path%;C:\Python27    press Enter 

C:\Python27>python file_name   press Enter
Dimmick answered 21/6, 2013 at 15:46 Comment(0)
G
2

Just want to mention, when you do:

cd C:\Python27
python Tools\Scripts\win_add2path.py

The PATH variable in "user variables for administrator" is changed.

But you can also follow the others' answer to open:

System -> advanced system settings -> advanced -> Environment Variables,

and modify/add the variable Path in "System Variables", add ;C:\Python27 at the end of it.

Geldens answered 20/11, 2015 at 16:27 Comment(0)
B
1

in powershell enter the following:

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

close and open the powershell and try again. this should solve your problem.

Bronchus answered 3/12, 2013 at 11:25 Comment(1)
This is the only working solution. All other modified some other user's env variable Path. Thanks, man.Sanbo
G
0

Modify the PATH variable too and append ;%python% otherwise the executable can not be found.

Genovera answered 6/1, 2011 at 23:54 Comment(0)
R
0

You need to edit the environment variable named PATH, and add ;c:\python27 to the end of that. The semicolon separates one pathname from another (you will already have several things in your PATH).

Alternately, you can just type

c:\python27\python

at the command prompt without having to modify any environment variables at all.

Radiancy answered 6/1, 2011 at 23:54 Comment(0)
D
0

On Windows you use C:\Python27\python.exe instead of python.

If you add C:\Python27 to your path, you can shorten it to just python.exe, but you do not need to do this.

Diann answered 6/1, 2011 at 23:55 Comment(0)
A
0

First install the Python into your windows by using this url and then add path variable as

c:\python27
Algae answered 12/1, 2012 at 14:23 Comment(0)
M
0

Use this PATH in Windows 7:

C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts\;
Mutate answered 23/2, 2013 at 0:31 Comment(0)
L
0

I also found the same problem even though i've added the path in the environment variable. Finally, I put my "C:\Python27" in the FRONT part of the "PATH" in environment variable and after restarting the cmd, it works!!! I hope this can help.

Lenwood answered 22/7, 2013 at 2:11 Comment(0)
I
0

press start button then type cmd. - Note you will need to run the command prompt as 'Adminstrator'.

write setx -m path "%path%;C:\Python27" then press enter.

[here -m for giving accessing permission to all users and in Python27 27 is version 2.7]

%path%; will prevent the original value from destroying. C:\Python27 will be appended to the current Path value.

that's it,you are done.

Iliad answered 13/2, 2014 at 11:40 Comment(1)
This deleted all my previous path variables... now even i am not able run anything on my computerPolytechnic
L
0

For Windows 10 & Python 3.5.1 users:

While installing Python on Windows 10, please don't forget to check the "Add to cmd prompt" option before hitting the "Install". This would help in easily access python from cmd.

If the option was not checked, then please use Set Path in cmd to see if it is available as executables or not. If not, Navigate to Start >> Control Panel >> System and Security >> System >> Advanced System Settings >> Advanced >> Environment Variables.. >> Select PATH from System Variables and Edit it. Then copy "C:\Python35\cmd" in the new line. After this please add .PY to PATHEXT in the same procedure.

Also please check if Start >> Control Panel >> System and Security >> System >> Advanced System Settings >> Advanced >> Environment Variables.. >> User variables from Username >> PATH is containing these two lines - "C:\Users\Username\AppData\Local\Programs\Python\Python35-32\Scripts\" & "C:\Users\Username\AppData\Local\Programs\Python\Python35-32\". Else please add them manually.

Ref: https://docs.python.org/3/using/windows.html

Libya answered 22/5, 2016 at 7:59 Comment(0)
C
0

Goto the Start Menu Right Click "Computer" Select "Properties" A dialog should pop up with a link on the left called "Advanced system settings". Click it. In the System Properties dialog, click the button called "Environment Variables". In the Environment Variables dialog look for "Path" under the System Variables window. Add ";C:\Python27" to the end of it. The semicolon is the path separator on windows. Click Ok and close the dialogs. Now open up a new command prompt and type "python"

If still the problem persists then type "py" instead of "python" in command prompt. might help!!!!

Coquette answered 6/6, 2017 at 11:38 Comment(0)
L
0

All the steps you have performed are correct, except one step, instead creating one separate variable, try below steps.

  1. Search for python.exe file, find the parent folder.
  2. Copy the folder path like on which python installation files resides
  3. Now go to the control panel-system-advanced settings-environment variables
  4. Find Path variable paste the copied folder path here and add ;
  5. Now all set for the execution goto cmd type python you must see the version details
Lighterage answered 21/5, 2020 at 23:46 Comment(0)
F
0

Go to the path where python is installed (type in python to start menu and open file location). On the address toolbar, type in the version of the Python - for me it's Python 3.10 (64-bit) and press enter. The command prompt will open up with the exact path where python is saved, so that the code will work. Same principle if you have installed Jupyter Notebook on your drive. Go to the folder where Jupyter is saved and type in Jupyter Notebook on the address bar and press enter. Easier than changing system paths..

Foal answered 1/6, 2022 at 22:13 Comment(0)
T
-2

For windows 8, just type "py".

Trouvaille answered 31/12, 2016 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.