Have you heard about the Python programming language and do you want to create your first Python script? You will learn how to do it with this guide.
A Python script is a sequence of instructions that can be executed as a program to perform a specific task. The instructions are lines of Python code. The .py file extension is used to identify Python scripts.
In this guide, I will show you how to create and run your first Python script!
Pre-requisite: Make sure you have installed Python on your computer so you can execute the simple program you will create in this tutorial. You can download the Python version for your platform (Windows/Linux/macOS) on the Official Python website and follow the installation instructions.
What is a Python Script: The First Step If You Want to Learn Python
A Python script is a file that contains Python code. Python scripts have the .py extension which tells your computer to handle their content as a Python program. You set the .py extension for a file when you create it.
What are examples of scripts?
An example could be a Python script that generates a report of the monthly sales for a company. Another example is a script that calculates the result of a complex formula given a set of inputs.
Usually, when you get started with Python, you begin by writing simple scripts and then you increase the complexity of your programs the more you improve your skills as a Python developer.
Below you can see an example of a filename for a Python script:
Once Python is installed and added to the PATH on your computer, you will be able to execute the code in this Python script using the following command:
This way, you will ask Python to execute the code in the file your_program.py line by line.
Notice that we have specified python3 in the command above. In this way, we make sure to execute the code in the file using the version of Python 3 installed on your computer.
Note: the previous version of Python was Python 2 and you shouldn’t use it considering that it’s no longer supported.
How Do I Create a Python Script Using a Text Editor?
Now that you know that to create a Python script you have to give the .py extension to it, let’s learn how to create a .py file.
To create a .py file with a text editor follow the steps below:
Open a Text Editor or Code Editor: Technically you can use any text editor for this, like Notepad on Windows or TextEdit on macOS but this is not a good approach. If you really have to use a text editor to start writing your first scripts at least use code editors like Visual Studio Code or Sublime Text. A code editor is a text editor with advanced features to support software development.
Write Some Code: Create a new file in your code editor and then write a simple Python program by adding the following line of code to the file you created:
This is the code of a program that prints the message “Hello, Codefather!” as output.
Note: Python provides a variety of built-in functions that allow you to build programs faster by using functionality already built for you. The print() function is one of Python’s built-in functions.
Save the File: Go to “File > Save As” and save your file with the .py extension. For example, you could name it:
Well done! You have created your first Python program!
How Do I Write a Python Script Using a Python IDE?
Another approach to creating Python scripts is to use a Python IDE (Integrated Development Environment).
To create a script in your IDE go through the following steps:
Open the IDE: Launch your chosen IDE and create a new file.
Write Some Code: Add the following line of code to the file:
Save the File: Save your file using the .py extension.
Et voilà, you have your script!
How to Create a .py File Using the Command Line or the Terminal
Another way to create a .py file for your Python code is via the command-line. This is an approach for those of you who are used to the Windows/Linux/macOS command line.
Open the Command Line: Open the Command Prompt on Windows or the Terminal on macOS / Linux.
Navigate to the Folder: Use the cd command to navigate to the folder where you want to create your Python script.
Create the File:
- macOS / Linux: Type the following command to create an empty file: touch hello_codefather.py
- Windows: Type the following command to create an empty file: echo. > hello_codefather.py
Edit the File:
- macOS / Linux: Open this file using Vim with the following command: vim hello_codefather.py
- Windows: In the Windows cmd type the command copy con hello_codefather.py to start editing the file and add type the Python code in it. To save the file press Ctrl+Z followed by Enter.
The use of the command line can work just for very simple code changes but it’s not the ideal tool for creating scripts or making code changes to existing scripts.
As you can see from the commands mentioned above, the process of editing files in the Windows command-line is quite convoluted. Definitely not ideal for code changes!
If you want to make code changes in a reliable way that you can test, using a strong code editor like Visual Studio Code or an IDE like PyCharm is the best option.
That’s because with Visual Studio Code or PyCharm you have additional features like syntax highlighting, code auto-completion, and debugging. These three features will make your Python coding experience a lot smoother!
Where Should You Save Your Python Script?
You might be tempted to save your Python files in the first random directory that comes to your mind. For example, the desktop!
Please don’t do that as over time you will end up with a huge number of files that are impossible to find and maintain.
It’s a good idea to have a specific directory on your computer dedicated to your Python programming. You can then create sub-directories for individual Python projects.
This will make it easier for you to quickly open the code related to a specific project without having to remember where you saved the files for that project when you created it in the first place!
How Do You Add Python Code to a .py File
Now that you have created a .py file for your Python program, it’s time to add some code to it.
In one of the previous sections, we have written the following line of code in our file:
What if you want to add multiple lines to a .py file?
To add multiple lines of code to a Python script, open your code editor or IDE and write the lines of code one under the other. Python will execute each line of code in the script sequentially.
Depending on the programming constructs (or keywords) you will add to your code you will have to provide specific levels of indentation to your script. If you don’t do that, Python will flag those lines with syntax errors when you run your code.
This is also the reason why writing your code in a good code editor or IDE is important. That’s because they understand and verify Python syntax so they can immediately flag errors in your Python code while you are writing it (this is a way to fail fast and to get working code written faster).
How Do You Run a Python Script?
After creating a Python .py script, you can execute the code in that script using the “python” command.
In the examples below we will assume that the script we want to execute is called hello_codefather.py.
To execute the code in a script from the directory where the file is located (using a relative path), type the following commands:
cd <directory_where_the_python_file_is_located> python3 hello_codefather.py
You can also run the script using its absolute path. In other words, you won’t have to enter the directory where the script is located before executing it.
When you execute your Python program you want to make sure you are using the latest version of Python on your system which should be a version of Python 3 (for example Python 3.11.5).
Running Python code from the command line is not the only option you have. You can also execute your program directly from your Visual Studio Code or PyCharm IDE. They both provide a “Run” button that allows you to execute your code and see its output in the IDE.
This is quite handy because this means that you can develop Python code in your code editor / IDE and execute it from the same window without having to switch to a different window.
This could save you lots of time in the long term! That’s because while building a Python program you will have to run it multiple times to verify that it’s working fine at every stage of the development process.
Would You Use a Single File for a Python Project?
A single file can work well if you create a simple Python script that you want to execute.
The more the length of your Python application code increases the more you will realize that is not ideal to have all your Python code in a single .py file.
This means that for a Python project, you will need multiple .py files organized in a specific directory structure that makes your project easy to maintain and enhance over time.
What are Common Mistakes When Creating and Executing a Python Script?
Below you can see three common beginner mistakes to avoid when you write and run Python scripts for the first time.
Forgetting the .py Extension
It can happen to forget the .py extension when saving your Python script.
Always remember to include the .py extension. This way you and your computer will be able to identify that file as a Python script.
Make sure the syntax of the code you write in your Python script is correct. Even small errors like incorrect characters specified in the wrong place or missing/incorrect indentation can cause errors when executing your Python code.
Also, when you are getting started with Python it’s harder to understand the root cause of the errors. But don’t worry, it becomes easier when you get used to Python’s syntax.
That’s why it’s important to use an IDE or code editor that notifies you about syntax errors as early as possible in the development process.
Imagine how bad it would be if you wrote a 100-line script and found several syntax errors only after finishing writing the code.
You would have to go back and review your script from scratch to fix all the errors instead of dealing with them one at a time while they occur when writing your code.
Executing the Python script from the wrong directory
Another common error is accessing an incorrect directory when you try to execute your Python script. The result is that you will see an error that tells you that your Python script cannot be found.
Example of Windows/Linux/macOS error
python: can't open file 'C:\\dev\\test_file.py': [Errno 2] No such file or directory
This is a common error especially if you are running your Python code via the command line.
You have taken the first step to becoming a Python developer and started understanding how Python works.
Now you know what you need to write your first Python scripts!
In this tutorial, we have also covered the commands you need to run your Python program.
As explained, the use of a simple text editor can work when creating a single Python file that contains very simple code. This would be just a test for you to start learning how to create your first Python script.
However, the right approach to create and modify files containing Python code is to use a robust code editor like Visual Studio Code or IDE like PyCharm.
Related article: Continue your journey to learn Python by going through the beginner Codefather tutorial to print “Hello, World” in multiple ways!
Claudio Sabato is an IT expert with over 15 years of professional experience in Python programming, Linux Systems Administration, Bash programming, and IT Systems Design.
With a Master’s degree in Computer Science, he has a strong foundation in Software Engineering and a passion for Robotics with projects that include Raspberry Pi and Arduino platforms.