Python provides a convenient way to execute code interactively or from scripts. While you can use Integrated Development Environments (IDEs) like PyCharm or Jupyter Notebook, the command line offers a lightweight and efficient environment for running Python code in Ubuntu.
Table of Contents
Setting up Python in Ubuntu
Before you can run Python code, you need to ensure that Python is installed on your Ubuntu system. Most versions of Ubuntu come with Python pre-installed, but it’s a good practice to check the installed version. Open a terminal and enter the following command:
If Python is not installed, you can install it using the package manager, apt, with the following command:
sudo apt update sudo apt install python3
This will install Python 3, which is the recommended version for new projects. If you specifically need Python 2, you can install it using the
python package instead.
Running Python code using the command line
Using the Python interpreter
The simplest way to run Python code in the command line is by using the Python interpreter. Open a terminal and type
python3 to start the interpreter. You can then enter Python code directly and execute it by pressing Enter. For example:
This will output “Hello, world!” to the terminal.
Running a Python script
Another common approach is to save your Python code in a file with a
.py extension and execute it as a script. Create a new file, such as
script.py, and write your Python code inside it. To run the script, use the following command:
This will execute the code in
script.py and display the output, if any.
Passing arguments to the script
You can also pass arguments to a Python script from the command line. These arguments can be accessed within the script using the
sys.argv list. For example, if you have a script called
my_script.py that takes two arguments, you can run it as follows:
python3 my_script.py arg1 arg2
Within the script, you can access
Advanced options and tips
When working on multiple Python projects, it’s recommended to use virtual environments. Virtual environments allow you to isolate project dependencies and avoid conflicts between different projects. You can create a virtual environment using the
venv module in Python:
python3 -m venv myenv source myenv/bin/activate
This will create a new virtual environment called
myenv and activate it. Any packages installed within this environment will be isolated from the global Python installation.
To install third-party packages, you can use the
pip package manager, which is the standard tool for Python package installation. For example, installing the
requests package, use the following command:
pip install requests
If you want to run a Python script directly without explicitly invoking the interpreter, you can use a shebang line at the beginning of the script. This line specifies the path to the Python interpreter to use. For example, if you want to use Python 3, your shebang line should look like this:
Make sure the script is executable (
chmod +x script.py), and you can then run it directly:
Redirecting input and output
You can redirect input and output streams when running Python code in the command line. For example, to read input from a file and write output to another file, use the following commands:
python3 script.py < input.txt > output.txt
This will execute
script.py and use
input.txt as the input source and
output.txt as the output destination.
Running Python in the background
If you want to run a Python script in the background, you can use the
nohup command. For example:
nohup python3 script.py &
This will execute
script.py in the background, and any output will be redirected to a file named
Troubleshooting common issues
Sometimes, you may encounter issues when running Python code in Ubuntu. Here are some common problems and their solutions:
- Python command not found: Ensure that Python is installed correctly and that the executable is in your system’s
- Module not found: If your code depends on external packages, make sure they are installed using
- Syntax error: Check your code for any syntax errors or typos. Python is sensitive to indentation, so ensure that your code is properly indented.
- File permissions: If you’re having trouble executing a script, make sure it has the proper permissions. You can use the
chmodcommand to make the script executable.
Running Python code in the Ubuntu command line is a fundamental skill for any Python developer. In this article, we explored different methods to run Python code, including using the Python interpreter and executing scripts. We also discussed advanced options such as virtual environments, package installation, shebangs, input/output redirection, and running Python in the background.
By mastering these techniques, you can leverage the power of the command line to streamline your Python development workflow and efficiently execute code in Ubuntu.