Setting Up Python: A Complete Guide for All Operating Systems
Python is one of the most popular and versatile programming languages, widely used in web development, data science, and automation. To get started with Python, you need to install it and set up an IDE (Integrated Development Environment). This guide will walk you through the steps to install Python and configure your environment on Windows, macOS, and Linux. It will also help you set up popular IDEs like VS Code, PyCharm, and Jupyter Notebook.
Installing Python
On Windows
Download the Python Installer:
- Visit the official Python website: python.org/downloads.
- Choose the latest version for Windows (Python 3.x).
- Click on the Download Python button.
Run the Installer:
- Once the installer is downloaded, run it.
- Important: Check the box that says “Add Python to PATH” before clicking Install Now.
- Follow the prompts and complete the installation.
Verify the Installation:
- Open Command Prompt (press
Windows + R
, then typecmd
and press Enter). - Type
python --version
to verify the installation. You should see the Python version installed.
- Open Command Prompt (press
python --version
On macOS
Install Python via Homebrew (Recommended):
- First, ensure you have Homebrew installed. If not, open the Terminal and install it by running:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install Python using Homebrew by running:
brew install python
Verify the Installation:
- Open the Terminal and type
python3 --version
to verify Python 3 is installed:
python3 --version
- You can also use
python3
to run Python on macOS.
- You can also use
On Linux
Install Python via Package Manager:
- Most Linux distributions come with Python pre-installed. To check if Python is installed, open the terminal and run:
python3 --version
If not installed, use the following commands to install Python:
Ubuntu/Debian-based:
sudo apt update
sudo apt install python3
Fedora:
sudo dnf install python3
CentOS:
sudo yum install python3
Verify the Installation:
- Use the command
python3 --version
to verify that Python has been installed successfully.
Setting Up an IDE (VS Code, PyCharm, Jupyter Notebook)
On Windows, macOS, and Linux:
VS Code Setup
Download and Install VS Code:
- Visit the VS Code download page and choose the version for your operating system.
- Download the installer and follow the prompts to install.
Install the Python Extension:
- Open VS Code, go to the Extensions view by clicking the Extensions icon on the left sidebar, and search for “Python.”
- Click Install on the Python extension by Microsoft.
Configure VS Code to Use Python:
- Open the Command Palette (
Ctrl + Shift + P
orCmd + Shift + P
on macOS) and typePython: Select Interpreter
. - Choose the Python interpreter from the list.
- Open the Command Palette (
Verify Installation:
- Create a new Python file (
.py
) in VS Code and write a simple script:
pythonprint("Hello, Python!")
- Run it using the green play button in the top right or by pressing
Ctrl + Shift + P
and selecting “Run Python File.”
- Create a new Python file (
PyCharm Setup
Download and Install PyCharm:
- Visit the PyCharm download page.
- Choose the community version (free) and follow the installation steps.
Configure Python Interpreter:
- Once PyCharm is installed, open it and create a new project.
- In the Project Interpreter section, select the Python version you installed earlier.
Write and Run Code:
- Create a new Python file in your project and write a script:
print("Hello, PyCharm!")
- Run the script by right-clicking on the file and selecting Run.
Jupyter Notebook Setup
Install Jupyter Notebook:
- Open your terminal (or Command Prompt on Windows) and install Jupyter using pip:
pip install notebook
A new tab will open in your default browser. You can create a new Python notebook by selecting New > Python 3.
Write and Run Code in Jupyter:
- Inside the notebook, you can write Python code and execute it in cells. For example:
print("Hello, Jupyter!")
Run the cell by pressing Shift + Enter
.
Best Practices for Python Setup
- Use Virtual Environments: Create a virtual environment for each project to manage dependencies separately.
python3 -m venv myenv
Activate the virtual environment:
- Windows:
myenv\Scripts\activate
- macOS/Linux:
source myenv/bin/activate
- Windows:
Keep Python and IDE Updated: Regularly check for updates for both Python and your IDE to take advantage of the latest features and bug fixes.
Organize Your Projects: Use proper folder structures for your Python projects. Keep source code, tests, and resources in separate directories.