How To Run Python Code In Ubuntu Command Line


Running Python code in the Ubuntu command line is a fundamental skill that every Python programmer should possess. Not only does it provide a quick and efficient way to execute Python scripts, but it also allows for easy debugging and testing. In this article, I will guide you through the process of running Python code in the Ubuntu command line, based on my personal experiences and insights.

Step 1: Install Python

Before we can run any Python code, we need to ensure that Python is installed on our Ubuntu system. By default, Ubuntu comes with Python pre-installed, but it may not be the latest version. To install the latest version, open the command line and run the following command:

sudo apt-get install python

Enter your password when prompted, and the installation process will begin. Once the installation is complete, you can verify that Python is installed by running:

python --version

If the command displays the Python version, then Python is successfully installed on your Ubuntu system.

Step 2: Create a Python Script

Now that we have Python installed, let’s create a Python script to run in the command line. Open your favorite text editor and create a new file with the extension .py (e.g., In this file, you can write your Python code.

For example, let’s create a simple script that prints “Hello, World!”:

print("Hello, World!")

Save the file and remember its location, as we will need it in the next step.

Step 3: Navigate to the Script’s Directory

In order to run our Python script, we need to navigate to its directory in the command line. Open the terminal and use the cd command to change the directory to where your Python script is located.

For example, if your script is located in the “Documents” folder, you can navigate to it by running:

cd Documents

Once you are in the correct directory, you can verify it by running ls to list the files and folders in the current directory. Your Python script file should be listed.

Step 4: Run the Python Script

Now that we are in the correct directory, we can finally run our Python script. In the command line, type:


Replace “” with the name of your Python script file.

Hit enter, and if everything is set up correctly, you should see the output of your Python script displayed in the command line!


If you want to run a specific version of Python on your Ubuntu system (e.g., Python 3), you can use the command python3 instead of python. This allows you to have multiple versions of Python installed concurrently and choose the one you want to use.


Running Python code in the Ubuntu command line is a straightforward process once you have Python installed and your script ready. By following the steps outlined in this article, you can quickly execute your Python scripts in the command line and gain more control over your code.

Remember to experiment and explore different features and functionalities offered by the command line. It is a powerful tool that can greatly enhance your Python development experience.