Python Script : One of the most important skills you need to build as a Python developer is to be able to run Python scripts and code. This is going to be the only way for you to know if your code works as you planned. It’s even the only way of knowing if your code works at all!
How do I write a Python script?
- Python commands can be written and stored in a plain text file.
- This file can then be run by using the python <scriptname>
- Python scripts generally use the .py extension.
- You should try to use a syntax-highlighting text editor to edit your Python scripts.
This step-by-step tutorial will guide you through a series of ways to run Python scripts, depending on your environment, platform, needs, and skills as a programmer.
Generally programmers write stand alone scripts, that are independent to live environments. Then they save it with a “.py” extension, which indicates to the operating system and programmer that the file is actually a Python program. After the interpreter is invoked, it reads and interprets the file. The way Python scripts are run on Windows versus Unix based operating systems is very different. We’ll show you the difference, and how to run a Python script on Windows and Unix platforms.
Run the Python command-line interpreter, under your OS of choice,
Open Command line: Finder -> Go menu -> Applications -> Terminal
Type: python
Run any plain text editor
Start TextEdit: Finder -> Go menu -> Applications -> TextEdit
Alternative: Start TextWrangler: Finder -> Go menu -> Applications -> TextWrangler
Run a Python script under Windows with the Command Prompt
Windows users must pass the path of the program as an argument to the Python interpreter. Such as follows:
C:\Python27\python.exe C:\Users\Username\Desktop\my_python_script.py
Note that you must use the full path of the Python interpreter. If you want to simply type python.exe C:\Users\Username\Desktop\my_python_script.py you must add python.exe to your PATH environmental variable. To do this, checkout the adding Python to the PATH environment article..
Window’s python.exe vs pythonw.exe
Note that Windows comes with two Python executables – python.exe and pythonw.exe. If you want a terminal to pop-up when you run your script, use python.exe However if you don’t want any terminal pop-up, use pythonw.exe. pythonw.exe is typically used for GUI programs, where you only want to display your program, not the terminal.
Run a Python Script Under Mac, Linux, BSD, Unix, etc
On platforms like Mac, BSD or Linux (Unix) you can put a “shebang” line as first line of the program which indicates the location of the Python interpreter on the hard drive. It’s in the following format:
Note: Shebang Means
#!/usr/bin/env python “”” The first line in this file is the “shebang” line. When you execute a file from the shell, the shell tries to run the file using the command specified on the shebang line. The ! is called the “bang”. … The shebang line specifies exactly how to run a script.
#!/path/to/interpreter
A common shebang line used for the Python interpreter is as follows:
#!/usr/bin/env python
You must then make the script executable, using the following command:
chmod +x my_python_script.py
Unlike Windows, the Python interpreter is typically already in the $PATH environmental variable, so adding it is un-necessary.
You can then run a program by invoking the Python interpreter manually as follows:
python firstprogram.py
Python Execution with the Shell (Live Interpreter)
Assuming that you already have Python installed and running well (if you’re getting an error, see this post), open the terminal or console and type ‘python’ and hit the ‘Enter’ key. You will then be directed immediately to the Python live interpreter. Your screen will display a message something like:
user@hostname:~ python
Python 3.3.0 (default, Nov 23 2012, 10:26:01)
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Discover more from CODE t!ps
Subscribe to get the latest posts sent to your email.