Python - Environment Setup

Hello, aspiring Pythonistas! I'm thrilled to guide you through the exciting journey of setting up your Python environment. As someone who's been teaching Python for years, I can assure you that this first step is crucial, but don't worry – we'll make it fun and easy!

Python - Environment Setup

Local Environment Setup

Before we dive into coding, we need to create a cozy home for Python on your computer. Think of it as building a nest for a pet python (the digital kind, of course!). This process involves downloading and installing Python, and then telling your computer where to find it.

Downloading Python

First things first, let's get Python onto your computer. Head over to the official Python website (python.org) and look for the Downloads section. You'll see different versions available, but for beginners, I recommend sticking with the latest stable version of Python 3.

Pro tip: Always download from the official source to avoid any sneaky surprises!

Installing Python

Now that you've downloaded Python, it's time to roll out the red carpet and welcome it to your computer. The installation process varies slightly depending on your operating system, so let's break it down:

Install Python on Ubuntu Linux

For my Ubuntu users out there, you're in luck! Python usually comes pre-installed on Ubuntu. To check, open your terminal and type:

python3 --version

If you see a version number, you're good to go! If not, no worries. Just type:

sudo apt-get update
sudo apt-get install python3

Install Python on other Linux

If you're using a different flavor of Linux, the process is similar. You can use your distribution's package manager. For example, on Fedora, you'd use:

sudo dnf install python3

Install Python on Windows

Windows users, your process is a bit different, but equally straightforward:

  1. Run the installer you downloaded.
  2. Make sure to check the box that says "Add Python to PATH" – this will save us a step later!
  3. Click "Install Now" and let the magic happen.

Setting up PATH

Now, we need to make sure your computer knows where to find Python when you want to use it. This is like giving your computer a map to Python's house.

Setting path at Unix/Linux

For my Linux and Mac users, open your terminal and edit your .bashrc or .bash_profile file:

nano ~/.bashrc

Add this line at the end:

export PATH="/usr/local/bin:$PATH"

Save and exit, then run:

source ~/.bashrc

Setting path at Windows

Windows users who checked "Add Python to PATH" during installation can skip this step. If you didn't, no worries! Here's what to do:

  1. Right-click on 'This PC' and choose 'Properties'
  2. Click on 'Advanced system settings'
  3. Click on 'Environment Variables'
  4. Under 'System variables', find and select 'Path', then click 'Edit'
  5. Click 'New' and add the path to your Python installation (usually something like C:\Python39)

Python Environment Variables

Environment variables are like secret messages you can leave for Python. They can affect how Python behaves. Here's a table of some common environment variables:

Variable Description
PYTHONPATH Augments the default search path for module files
PYTHONSTARTUP Path to an initialization file executed on interactive startup
PYTHONCASEOK Used in Windows to enable case-insensitive module importing
PYTHONHOME Alternative module search path

You don't need to set these now, but it's good to know they exist for future reference!

Running Python

Congratulations! You've set up your Python environment. Now, let's make sure everything is working. Open your terminal (or command prompt on Windows) and type:

python

You should see something like this:

Python 3.9.5 (default, May 3 2021, 08:33:23) 
[GCC 10.2.1 20201125 (Red Hat 10.2.1-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

This is the Python interactive shell. Let's try a simple command:

>>> print("Hello, Python!")
Hello, Python!

If you see "Hello, Python!" printed out, give yourself a pat on the back – you've successfully set up Python and run your first command!

Remember, setting up your environment is like preparing your kitchen before cooking a delicious meal. Now that we have everything in place, we're ready to start creating amazing things with Python.

In my years of teaching, I've found that students who take the time to properly set up their environment have a much smoother learning experience. So, well done on taking this important first step!

Next time, we'll dive into writing and running more complex Python programs. Until then, feel free to experiment in the interactive shell. Who knows? You might discover something cool before our next lesson!

Happy coding, future Python masters!

Credits: Image by storyset