Running a Python script on Ubuntu is a fundamental skill for developers, system administrators, and data scientists. Whether you’re automating tasks, running a web server, or training a machine learning model, understanding the proper execution methods is essential. This article provides a comprehensive overview of how to run Python scripts on Ubuntu, covering everything from basic commands to professional best practices.
Python is a programming language that is interpreted, object-oriented, and considered to be high-level.|
__________________________________________________________________________________________________________________
Testing Environment: Ubuntu 24.04.2 LTS Hostname - ip Address -
__________________________________________________________________________________________________________________
lsb_release -a
apt update ; apt install build-essential net-tools curl git software-properties-common
python3 --version
sudo apt install python3 python3-venv python3-pip
cd ~/path-to-your-script-directory
nano demo_ai.py
from sklearn.tree import DecisionTreeClassifier
import numpy as np
import random
# Generate sample data
x = np.array([[i] for i in range(1, 21)]) # Numbers 1 to 20
y = np.array([i % 2 for i in range(1, 21)]) # 0 for even, 1 for odd
# Create and train the model
model = DecisionTreeClassifier()
model.fit(x, y)
# Function to predict if a number is odd or even
def predict_odd_even(number):
prediction = model.predict([[number]])
return "Odd" if prediction[0] == 1 else "Even"
if __name__ == "__main__":
num = random.randint(0, 20)
result = predict_odd_even(num)
print(f"The number {num} is an {result} number.")
python3 -m venv python-env
source python-env/bin/activate
pip install scikit-learn numpy
Run Python Script - python3 demo_ai.py
Script Executable [OPTIONAL] -
nano demo_ai.py
#!/usr/bin/env python3
chmod +x demo_ai.py
./demo_ai.py
___________________________________________________________________________________________________________________