Python is a popular programming language known for being simultaneously powerful and easy to learn and use. Several projects on this site use Python. This page will show how to set up and use a Python environment on your computer, not how to use the language itself. If you don’t know how to code in Python and want to learn, take a look at any of the many Python tutorials available for free online, like this one.
Set Up Python
Windows
- Download the installer from the official Python website. The exact version number may vary from what is shown here, but it should look like
3.X.X
- Run the downloaded file, following the instructions onscreen. Make sure that you check the box to add it to PATH. This is essential to running Python easily from a command prompt.
- Open a command prompt. This can be done by searching “cmd” in the Start menu, or pressing Windows+R and typing “cmd”.
- Type
python --version
at the command prompt to verify that Python was installed. You should get a version number3.X.X
. If you instead get an error, either Python was not installed correctly or it was not added to PATH.
Install Modules
Modules are sets of code pre-written for doing common tasks, like creating graphs, loading images, or doing certain kinds of math. Some modules come pre-installed with Python, but some Python scripts require you to install certain modules from the internet before they can run. Luckily, installing modules is very easy.
- Make sure your computer is connected to the internet.
- Open a command prompt and type
python -m pip install NameOfTheModule
. Wait until the installation process returns you to the command prompt. - You’re done! You only have to do this once for any module required by a script.
Create, Edit, and Run Python Scripts
IDLE
The Python installer, in addition to installing Python, installed a program called IDLE. IDLE is an Integrated Development Environment, meaning that it gives you a set of tools to create, edit and run Python scripts.
- Open IDLE.
- This window is called the “shell.” You can execute single lines of Python code at a time by typing them into the prompt and hitting Enter/Return. Start out by trying simple commands like
print("hello")
orsum([1, 4, 6, 7])
to get the hang of it. - While typing directly into the shell is good for doing relatively simple tasks, you will quickly find that doing anything very complicated or repeating previous commands takes a lot of retyping and/or copy-pasting. Luckily for you, there is a way to save many lines of code at once and have them executed in order on command without having to retype them.
- In the shell menu, go to
File -> New File
to create a new Python code (.py
) file. A blank editor window should come up. Notice that your shell window is still open – don’t close it; you’ll need it! - Now, type in some Python commands, one on each line. You can use as many lines as you want; the commands will be executed in order, starting with the first line and ending with the last.
- To run your code, go the the menu option
Run -> Run Module
. Name and save your file when it prompts you to do so. Notice that the file extension it is saved as is.py
, signifying that it is a Python program. You can open, edit and run any.py
file with IDLE - When you run your file, your shell window will be brought to the front, a
== RESTART ==
line will appear, and the output of your code will be displayed in blue. When your program finishes, the prompt>>>
will appear again and you can type in the shell again like normal. - Tip: if you ever want to stop your running program and get back to the shell prompt immediately, hit
Ctrl-C
,Command-C
, orShift-F6
.
Command Prompt
While IDLE will run any Python file, sometimes you may want to run a .py
file from the command line. Perhaps you are working on someone else’s computer and IDLE isn’t installed, or you already have a command prompt open, or you just like working from a command prompt better. In any case, it’s a good skill to know. Assuming that you have already installed Python on your computer:
- Open a command prompt.
- Find the file path to the
.py
file you want to run. It should look something likeC:/This/Is/A/Windows/File/Path/
or/This/Is/An/Apple/File/Path/
- In the command prompt, type
cd "/Your/File/Path/"
, including the quotes. Thiscd
command tells the prompt where to look for your Python file. - Finally, type
python YourFileName.py
. The output of that file will show in the command prompt, like it did in the IDLE shell. - Alternately, instead of using the
cd
command and then thepython -m
command in steps (3) and (4), you could typepython "/Your/File/Path/YourFileName.py"
. Either way works.
1 pings
Maze Traversal – The Code Fair
September 19, 2018 at 12:09 am (UTC -4) Link to this comment
[…] How to Use Python » […]