Robin.te.Hofstee

Python Development Environment Setup on Linux and macOS

Robin te Hofstee4 min readViews1
Python Development Environment Setup on Linux and macOS

A comprehensive guide to setting up a Python development environment on Linux and macOS for self-hosters and developers.

Python Development Environment Setup on Linux and macOS

Python is a versatile programming language that powers various applications, from web services to desktop software. As a self-hoster or developer, setting up a Python environment is crucial. This guide will help you establish a clean and efficient Python development environment on Linux and macOS.

Python versions prior to 3.9 support the presence of multiple versions installed concurrently, but for maintainability and best practices, this guide recommends using a virtual environment to manage dependencies and isolate project environments.

Setting Up on Linux

Install Python

Linux distributions often come with Python pre-installed, but to ensure you have the latest and compatible version for your projects, you should install it from the distribution's package manager:

sudo apt update && sudo apt install python3

Verify the installation:

python3 --version

Setting Up a Virtual Environment

Create a new project directory:

mkdir my_python_project
cd my_python_project

Initialize a virtual environment:

python3 -m venv venv

Activate the virtual environment:

source venv/bin/activate

Your prompt should now reflect the active environment:

(venv) user@linux-machine:~/my_python_project$

Installing Python Packages

Use pip to install required packages:

pip install requests flask

Deactivate when done:

deactivate

Structuring Your Project

Organize your project directory with the following structure:

my_python_project/
│
├── app/
│   ├── __init__.py
│   ├── routes.py
│   └── views.py
│
├── tests/
│   ├── __init__.py
│   ├── test_routes.py
│   └── ...
│
├── manage.py
│
├── requirements.txt
│
└── db.sqlite3

Create a requirements.txt for dependency management:

pip freeze > requirements.txt

Setting Up on macOS

Install Python

For macOS users with Homebrew, Python can be installed easily:

brew install python

Verify the installation: 0

Setting Up a Virtual Environment

In the project directory, initialize the virtual environment:

python3 -m venv venv

Activate the environment:

source venv/bin/activate

Check the installed Python version:

python --version

Installing Python Packages

Install your project dependencies:

pip install requests flask

Deactivate when done:

deactivate

Structuring Your Project

Follow the same project directory structure as provided for Linux.

Managing the System Path

Add the Python bin directory to your PATH in .bash_profile or .zshrc:

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

Managing Package Installations Safely

To ensure package integrity and security, use pip's --no-cache-dir and --no-binary options:

pip install --no-cache-dir --no-binary :all: requests flask

This prevents installing pre-compiled binaries, which can be a security risk on self-hosted systems.

Conclusion

Setting up a Python development environment on Linux and macOS is essential for building and maintaining robust applications. By following this guide, you've ensured a clean setup with isolated environments and reliable dependency management. Remember to activate your environment before running your Python scripts and deactivate it when you're finished to maintain a clean working state.

This is the second article in the Python Scripting Series. Check out Part 1: Python Basics for Self-Hosters and upcoming articles for more insights into Python scripting and self-hosting.

Tags: ['Python', 'Linux', 'MacOS', 'development', 'self-hosting', 'guide']

This guide is part of the Python Scripting Series. You can find more articles about Python scripting for self-hosters and developers at the blog.

Tutorial series

Python Scripting Series

A step-by-step Python series for self-hosters and developers: from your first script and a clean environment to databases and local AI APIs.

  1. 1.Python Fundamentals Cheat Sheet for Scripting Beginners
  2. 2.Python Development Environment Setup on Linux and macOS
  3. 3.Mastering Python Virtual Environments with python3 -m venv

6 more parts coming soon.

Share this post
Robin te Hofstee

Written by Robin te Hofstee

Technology enthusiast with a deep love for programming. Sharing practical guides on React, Next.js, Linux and self-hosting.