Overview
python3 is the core command used to run programs written in the Python 3 language or to start an interactive Python shell. Its execution behavior can be controlled through various options.
Key Features
- Execute Python script files
- Start an interactive Python shell
- Run Python modules as scripts
- Execute code strings directly
- Check installed Python version
Common Options
These are the options frequently used with the python3 command.
Execution and Information
Generated command:
Try combining the commands.
Description:
`python3` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Here are various examples of how to use the python3 command.
Start Interactive Python Shell
python3
Running python3 without any arguments starts the interactive interpreter.
Execute Python Script
python3 my_script.py
Executes the specified Python script file (e.g., `my_script.py`).
Execute Code String Directly
python3 -c "print('Hello, Python from command line!')"
Useful for running small Python code snippets directly from the command line.
Run Python Module
python3 -m http.server 8000
Runs a specific Python module as a script. For example, you can start a simple HTTP server.
Check Python Version
python3 -V
Checks the version of Python 3 installed on your system.
Installation
Most modern Linux distributions come with Python 3 pre-installed. However, if you need a specific version or it's not installed, you can use the following commands.
Debian/Ubuntu Based Systems
sudo apt update
sudo apt install python3
Install Python 3 using the APT package manager.
CentOS/RHEL/Fedora Based Systems
sudo yum install python3 -y
# or
sudo dnf install python3 -y
Install Python 3 using the YUM or DNF package manager.
Tips & Notes
Useful tips and notes for Python development and usage.
Using Virtual Environments
It is recommended to use virtual environments to create isolated Python environments for each project. You can create one with the command `python3 -m venv myenv`.
- Prevent dependency conflicts between projects
- Avoid polluting the system Python environment
- Facilitate package management
Package Management with pip
Python packages can be installed, updated, and removed using the `pip3` command. (e.g., `pip3 install requests`)
- Install package: `pip3 install <package_name>`
- Update package: `pip3 install --upgrade <package_name>`
- Uninstall package: `pip3 uninstall <package_name>`
Beware of Python 2 vs. Python 3 Confusion
On some older systems, the `python` command might refer to Python 2. To ensure you are using Python 3, it is always safer to explicitly use `python3`.