Complete Guide to Running Python Scripts in Ubuntu Terminal

Nov 28, 2025 · Programming · 21 views · 7.8

Keywords: Python | Ubuntu | Terminal | Script Execution | Working Directory

Abstract: This article provides a comprehensive guide to running Python scripts in Ubuntu terminal, covering fundamental concepts like current working directory, two main execution methods (direct interpreter invocation and making scripts executable), Python version compatibility, and practical debugging techniques. With clear step-by-step instructions and code examples, it helps Python beginners master essential skills for script execution in Linux environments.

Understanding File Path Issues in Terminal

When you type python test.py in the terminal and receive the error message python: can't open file 'test.py': [Errno 2] No such file or directory, this indicates that the system cannot find a file named test.py in the current working directory. The core issue stems from the concept of current working directory in terminal sessions, which determines how relative paths are resolved.

Fundamentals of Current Working Directory

In Linux systems, each terminal session has a current working directory, representing the folder location where the shell is currently operating. When you use relative paths (like test.py), the system attempts to locate the file within this directory. To check your current working directory, use the pwd command:

$ pwd
/home/username/projects

To change the current working directory, use the cd command:

$ cd /path/to/your/script/directory
$ python test.py

Two Primary Methods for Python Script Execution

Method 1: Direct Interpreter Invocation

This is the simplest approach, particularly suitable for beginners. Choose the appropriate command based on your Python version:

# For Python 2.x
$ python script.py

# For Python 3.x
$ python3 script.py

If the script is not in the current directory, use absolute paths:

$ python /home/username/projects/script.py

Method 2: Making Scripts Executable

This method allows scripts to be executed directly like system commands. First, add a shebang line at the beginning of your script file:

#!/usr/bin/env python

Then grant execution permissions to the script:

$ chmod +x script.py

Finally, execute using relative or absolute paths:

$ ./script.py

Python Version Compatibility Considerations

Ubuntu systems typically have both Python 2 and Python 3 installed. If your script is written for Python 3 but executed with the python command (which usually points to Python 2), syntax errors may occur. It's recommended to explicitly specify the Python version:

# Explicitly use Python 3
$ python3 script.py

# Or use absolute paths for specific versions
$ /usr/bin/python3 script.py

Practical Debugging Techniques

Using the -i parameter enters interactive mode after script execution, which is valuable for debugging:

$ python -i script.py

To verify file existence and location, use ls and find commands:

$ ls -la *.py
$ find /home -name "*.py"

Best Practice Recommendations

Create dedicated directory structures for Python projects, use virtual environments for dependency management, and develop the habit of explicitly specifying Python versions. These practices will help avoid common path and compatibility issues while improving development efficiency.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.