You can access Python in the Command Line by just typing python , python3 , or python3. 7 , python3. 8 , or python3. 9 , depending on which version you installed.
The Windows 7 Command Prompt Environment To open a Command Prompt window in which you can type commands and review output, as shown in Figure 29.1, click Start, All Programs, Accessories, Command Prompt. Alternately, click Start and type cmd in the Search box. Then, when cmd.exe has been located, press Enter.
The most basic and easy way to run a Python script is by using the python command. You need to open a command line and type the word python followed by the path to your script file, like this: python first_script.py Hello World! Then you hit the ENTER button from the keyboard and that's it.
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+:
Environment variables...
button (in the Advanced tab);C:\Python27
to the end (substitute your Python version)Assuming you have Python2.7 installed
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"
It should work.
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 ]
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.
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.
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.
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.
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.
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.
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With