Home > Environment & Utility > python

python: Execute Python Interpreter

Python is an interpreter for a powerful and versatile programming language. This command allows you to execute Python scripts or enter an interactive shell to run and test code immediately. It is widely used in data analysis, web development, automation, and various other fields.

Overview

The `python` command invokes the interpreter used to execute Python code. You can interact with the Python environment in various ways, such as running script files, entering interactive mode, or executing specific modules.

Key Features

The main tasks you can perform using the `python` command.

  • Execute Python script files
  • Enter interactive (REPL) shell
  • Execute specific Python modules directly
  • Check Python version information

Key Options

The primary options available when running the `python` command.

Execution Modes and Information

Environment Configuration

Generated command:

Try combining the commands.

Description:

`python` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Practical examples of using the `python` command.

Check Python Version

python --version

Checks the version of Python installed on your system.

Start Interactive Shell

python

Starts the Python interactive interpreter, allowing you to run and test code immediately.

Execute Python Script

echo "print('Hello from script!')" > my_script.py
python my_script.py

Executes a specified Python script file (e.g., my_script.py).

Execute Code String

python -c "print('Hello, Python from command line!')"

Executes Python code directly from the command line by passing it as a string.

Execute Built-in Module (HTTP Server)

python -m http.server 8000

Runs Python's built-in HTTP server module to share the current directory over the web. (Port 8000)

Installation

Most modern Linux distributions come with `python3` pre-installed. However, if the `python` command does not point to `python3` or if a specific version is required, manual installation or link configuration might be necessary. Using virtual environments is essential for managing project-specific dependencies.

Check Python Version

python --version
python3 --version

Verify the installed Python version to confirm if the `python` command points to `python3`.

Install Python 3 on Debian/Ubuntu

sudo apt update
sudo apt install python3

Installs Python 3 using the APT package manager.

Install Python 3 on Fedora/RHEL

sudo dnf install python3

Installs Python 3 using the DNF package manager.

Create and Activate Virtual Environment (venv)

python3 -m venv myproject_env
source myproject_env/bin/activate

Sets up an isolated Python environment for each project to prevent dependency conflicts.

Tips & Precautions

Useful tips and precautions to enhance efficiency and prevent potential issues during Python development.

Key Tips

Considerations when using Python.

  • Recommendation to use `python3` instead of `python`: On many systems, `python` may still refer to Python 2, so it's advisable to explicitly use `python3`.
  • Utilize virtual environments (venv): Create isolated virtual environments for each project to prevent package dependency conflicts and facilitate project management.
  • Package management with `pip`: Install and manage Python packages using the `pip` command. (e.g., `pip install requests`)
  • Script execution permissions and Shebang: To make Python scripts directly executable, grant execute permissions using `chmod +x script.py` and add a Shebang line at the beginning of the script, such as `#!/usr/bin/env python3`.

Same category commands