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 python3Verify the installation:
python3 --versionSetting Up a Virtual Environment
Create a new project directory:
mkdir my_python_project
cd my_python_projectInitialize a virtual environment:
python3 -m venv venvActivate the virtual environment:
source venv/bin/activateYour 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 flaskDeactivate when done:
deactivateStructuring 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.txtSetting Up on macOS
Install Python
For macOS users with Homebrew, Python can be installed easily:
brew install pythonVerify the installation: 0
Setting Up a Virtual Environment
In the project directory, initialize the virtual environment:
python3 -m venv venvActivate the environment:
source venv/bin/activateCheck the installed Python version:
python --versionInstalling Python Packages
Install your project dependencies:
pip install requests flaskDeactivate when done:
deactivateStructuring 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 flaskThis 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.Python Fundamentals Cheat Sheet for Scripting Beginners
- 2.Python Development Environment Setup on Linux and macOS
- 3.Mastering Python Virtual Environments with python3 -m venv
6 more parts coming soon.

Written by Robin te Hofstee
Technology enthusiast with a deep love for programming. Sharing practical guides on React, Next.js, Linux and self-hosting.
Related Posts
Mastering Python Virtual Environments with python3 -m venv
Learn how to use python3 -m venv to create, activate, and manage virtual environments, ensuring clean and isolated development spaces.
Python Fundamentals Cheat Sheet for Scripting Beginners
A quick reference guide for essential Python scripting concepts, including data structures, control flow, and function definitions.
Setting Up Python Virtual Environments
Learn how to set up and manage Python virtual environments for your projects, ensuring isolated and reproducible development.


