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/projectsTo change the current working directory, use the cd command:
$ cd /path/to/your/script/directory
$ python test.pyTwo 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.pyIf the script is not in the current directory, use absolute paths:
$ python /home/username/projects/script.pyMethod 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 pythonThen grant execution permissions to the script:
$ chmod +x script.pyFinally, execute using relative or absolute paths:
$ ./script.pyPython 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.pyPractical Debugging Techniques
Using the -i parameter enters interactive mode after script execution, which is valuable for debugging:
$ python -i script.pyTo 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.