Installing Python and Setting Up the Environment

How you can get setup fast and coding Python in minutes

Author: Jeremy Morgan
Published: October 13, 2024


Coding with AI

I wrote a book! Check out A Quick Guide to Coding with AI.
Become a super programmer!
Learn how to use Generative AI coding tools as a force multiplier for your career.


Installing Python and Setting Up the Environment

Hey there, tech enthusiasts! Let’s dive into one of the most essential steps for anyone starting their Python journey—installing Python and setting up a proper environment. Whether you’re gearing up for web development, data science, or just tinkering around with automation, this guide will get you up and running. We’ll cover everything from installing Python on different platforms to creating virtual environments. Ready? Let’s go!

1. What is Python and Why Should You Care?

Python is a versatile, high-level programming language that’s gained popularity for its simplicity and power. It’s used in everything from web development (think Django, Flask) to data science (hello, Pandas and NumPy!), machine learning, and even automation scripts.

One of the main reasons people love Python is because it’s easy to read and write. The syntax feels intuitive, which means less time scratching your head and more time solving problems.

2. Python 2.x vs. 3.x: Why Python 3.x is the Way to Go

Before we jump into the installation steps, let’s clear something up. Python has two major versions: Python 2.x and Python 3.x. Python 2.x is deprecated, which means it’s no longer supported or updated. All the new cool features and updates are happening in Python 3.x.

So, the takeaway? Install Python 3.x. It’s the version that will future-proof your work, and most libraries are now optimized for Python 3.

You can check if Python is already installed and which version with:

python --version

or, if you’re using macOS/Linux:

python3 --version

3. Installing Python on Different Operating Systems

Now, let’s dive into installing Python on your system.

Windows Installation

  1. Download Python: Head to python.org and download the latest Python 3.x installer for Windows.
  2. Run the Installer: Double-click the installer. Here’s the crucial part: Check the box that says “Add Python to PATH”. This makes it easier to run Python from any directory in the command line.
  3. Follow the Instructions: Go through the installation process and let Python do its thing!

macOS Installation

Good news! macOS ships with Python 2.x, but that’s not what we want. So, let’s install the latest Python 3.x.

  1. Install Homebrew (if you haven’t yet):

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  2. Install Python 3:

    brew install python3
    
  3. Verify Installation:

    python3 --version
    

Linux Installation

Most Linux distributions already come with Python 3.x pre-installed. But if you want to ensure you’re running the latest version, follow these steps (for Ubuntu/Debian):

  1. Update the package list:

    sudo apt-get update
    
  2. Install Python 3:

    sudo apt-get install python3
    
  3. Verify Installation:

    python3 --version
    

4. Verifying Python Installation

No matter which OS you’re using, open your terminal (or Command Prompt in Windows) and type:

python --version

For macOS/Linux, you may need:

python3 --version

You should see the version number of Python 3.x, which means you’re good to go!

5. Setting Up a Code Editor or IDE

Now that Python is installed, let’s set up a tool to write and manage your code.

VS Code

  1. Download and Install: Go to Visual Studio Code.
  2. Install the Python Extension: Open VS Code, and you’ll see an option to install the Python extension when you open a .py file. Alternatively, you can install it from the extensions marketplace by searching for “Python”.
  3. Set Python as the Default Interpreter: Press Ctrl+Shift+P, type Python: Select Interpreter, and choose the Python 3.x version.

VS Code is light, fast, and has great support for Python!

6. Creating and Activating a Virtual Environment

One of the best practices in Python is using virtual environments. A virtual environment is a self-contained directory that holds your project’s dependencies, ensuring they don’t conflict with other projects. Pretty neat, right?

Here’s how you set one up:

Windows

  1. Open your terminal (or PowerShell) and navigate to your project directory.

  2. Run the following to create a virtual environment:

    python -m venv env
    
  3. Activate the environment:

    .\env\Scripts\activate
    

macOS/Linux

  1. In your terminal, navigate to your project directory.

  2. Run:

    python3 -m venv env
    
  3. Activate the environment:

    source env/bin/activate
    

Now, you’ll see (env) before your command line prompt, meaning you’re working in a virtual environment.

Deactivating the Virtual Environment

When you’re done, you can deactivate the virtual environment by typing:

deactivate

7. Managing Packages with pip

Python uses a package manager called pip. This tool helps you install and manage libraries and packages in Python.

For example, to install a package like requests (used for making HTTP requests), you’d run:

pip install requests

You can also view installed packages using:

pip list

Creating a requirements.txt File

When working on projects, it’s good practice to document your dependencies in a requirements.txt file. This way, anyone can install the exact same packages you’re using by running:

pip freeze > requirements.txt

To install all dependencies from a requirements.txt file:

pip install -r requirements.txt

8. Running Python Scripts

Let’s test everything with a simple Python script. Create a new file called hello.py and add the following code:

print("Hello, World!")

Now run it from the terminal:

python hello.py

On macOS/Linux, you might need to use:

python3 hello.py

Pretty cool, right?

9. Managing Python Versions with pyenv (Advanced)

For advanced users, you can manage multiple Python versions on your system using a tool like pyenv. This allows you to easily switch between different versions of Python for different projects. If you’re interested, check out the pyenv documentation.

Key Takeaways

  1. Python Installation: You know how to download and install Python on any system.
  2. Virtual Environments: You can create and activate virtual environments for project isolation.
  3. Package Management: Installing and managing Python libraries with pip is a breeze.
  4. Running Scripts: You can now run Python scripts and see them in action.

And that’s it! Now you’re ready to start building cool things in Python. Let me know how your setup process went, and if you have any questions, feel free to reach out. Happy coding!


Coding with AI

I wrote a book! Check out A Quick Guide to Coding with AI.
Become a super programmer!
Learn how to use Generative AI coding tools as a force multiplier for your career.