Python Development with ServBay
Python is a powerful and versatile programming language widely used for web development, data science, automation scripts, and more. ServBay is a local web development environment designed specifically for developers, offering robust support for multiple Python versions. It allows seamless integration with other ServBay components (such as web servers and databases), helping you create a complete local development workflow.
What is Python?
Python is a high-level, interpreted, general-purpose programming language renowned for its clean, readable syntax and powerful capabilities. Its design philosophy emphasizes code readability and simplicity, making it ideal for rapid application development. Python supports multiple programming paradigms, including object-oriented, imperative, functional, and procedural programming. Its true strength lies in its rich standard library and vast third-party ecosystem, making it a popular choice in web development (for example, with Django and Flask frameworks), data analysis, artificial intelligence, scientific computing, and more.
ServBay’s Python Support
ServBay is designed to give you a convenient way to install and manage different Python versions without worrying about conflicts with your system's Python or complex installation procedures. With ServBay, you can easily switch among various Python versions to suit the needs of different projects.
ServBay supports the following Python versions (the exact versions available may vary as ServBay updates):
- Python 2.7
- Python 3.5
- Python 3.6
- Python 3.7
- Python 3.8
- Python 3.9
- Python 3.10
- Python 3.11
- Python 3.12
- Python 3.13
- Python 3.14 (and future versions)
These versions cover legacy Python 2.7 as well as the current mainstream and latest Python 3.x releases, ensuring you can choose the version that best suits your project's requirements.
Installing Python
Installing Python via ServBay’s graphical user interface (GUI) panel is straightforward and intuitive.
- Open the ServBay GUI panel.
- Navigate to the
Packages
menu on the left. - Locate the
Python
category in the package list. - Expand the
Python
category to see the list of Python versions supported by ServBay. - Select the Python version you need (for example, Python 3.12), and click the green
Install
button next to it. - ServBay will automatically download and install the selected Python version and its related components. Wait for the installation to finish—when complete, the button will turn green to indicate installation.
The ServBay GUI package panel showing the list of Python versions and installation buttons
You may install multiple Python versions as needed.
Using Python from the Command Line
The Python versions installed via ServBay are added to the ServBay environment, making them directly accessible from your terminal. ServBay intelligently sets your PATH environment variable so you can easily use the installed Python interpreters.
Typically, ServBay provides the following commands:
python
: Points to the latest installed Python 3 version.python2
: Points to the Python 2.7 version (if installed).python3
: Points to the latest installed Python 3 version.pythonX.Y
: Points to a specific Python version, e.g.,python3.12
(if installed).
You can verify installed Python versions in the ServBay terminal (accessed via the terminal button in the ServBay GUI) or your system terminal:
# Check which version the default python command points to
python -V
# Example output: Python 3.12.2
# Check which version the python3 command points to
python3 -V
# Example output: Python 3.12.2
# If Python 2.7 is installed, check its version
python2 -V
# Example output: Python 2.7.18
# If a specific version is installed, try calling it directly
python3.10 -V
# Example output: Python 3.10.13
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Running a Simple Python Script:
In your website root directory (such as
/Applications/ServBay/www
) or anywhere convenient, create a new file—e.g.,hello.py
.Add the following Python code to this file:
python# hello.py print("Hello from ServBay Python!")
1
2Save the file.
Open a terminal (open the ServBay terminal directly, or use your system terminal—just make sure ServBay is running and your PATH is set).
Navigate to the folder containing
hello.py
.Run the script using the
python
command:bash# Assuming you created hello.py under ServBay's www directory cd /Applications/ServBay/www python hello.py # Output: Hello from ServBay Python!
1
2
3
4Or use a specific version:
bashpython3.12 hello.py # Output: Hello from ServBay Python!
1
2
Managing Python Packages (Using pip)
Each Python version installed by ServBay comes bundled with the pip
package management tool for installing and managing third-party libraries.
TIP
ServBay includes the pip package manager by default, so there’s no need for separate installation.
The basic command to install a Python package is pip install <package-name>
. When you have multiple Python versions installed, ensure you're using the correct pip version. Typically, the pip command associated with a specific Python version is pipX.Y
or invoked via pythonX.Y -m pip
.
- Use the default
pip
(usually linked topython
orpython3
):bashpip install requests
1 - Use pip for a specific Python version:bash
pip3.12 install requests
1 - Or invoke pip using the module method for the desired version (recommended for greater clarity):bash
python3.12 -m pip install requests
1
Isolating Project Dependencies with Python Virtual Environments
While ServBay allows you to install and access multiple global Python versions, it’s strongly recommended to use Python virtual environments for actual project development.
Why Use Virtual Environments?
- Dependency Isolation: Each project may have different library version requirements (for example, Project A needs
requests==2.25.0
, Project B needsrequests==2.28.1
). A virtual environment gives each project an isolated Python environment with its ownsite-packages
, ensuring dependencies in Project A don’t interfere with those in Project B. - Consistency Across Environments: Ensures that development, testing, and (possibly) production environments use the same set of dependencies, reducing the chance of "it works on my machine" issues. Dependencies are usually tracked with
requirements.txt
orpyproject.toml
files. - Keep Your System Clean: Avoid cluttering ServBay’s global Python installations with project-specific packages.
How to Create and Use a Virtual Environment (using Python’s built-in venv
module):
Assume you want to use ServBay’s Python 3.12 for your my_flask_app
project:
- Choose the Python version: Decide which Python version to use (e.g.,
python3.12
). - Create a virtual environment:bashThis will create a
# Move to your project directory cd /Applications/ServBay/www # Create the project folder mkdir my_flask_app cd my_flask_app # Create a venv using ServBay's specified Python version # '.venv' is the recommended name for the virtual environment directory python3.12 -m venv .venv
1
2
3
4
5
6
7
8
9.venv
folder in yourmy_flask_app
directory with an isolated Python interpreter,pip
, andsite-packages
. - Activate the virtual environment:bashOnce activated, you'll usually see
# On macOS source .venv/bin/activate
1
2(.venv)
in your terminal prompt, and bothpython
andpip
commands will use the.venv
versions directly. - Install dependencies:bash
# Now pip install will target .venv only pip install Flask requests # See installed packages pip list
1
2
3
4
5 - Develop your application...
- Deactivate the virtual environment: When finished, simply run:bash
deactivate
1
Virtual environments work hand-in-hand with the .servbay.config
feature described next, giving you a flexible and isolated development experience.
Managing Project-level Python Versions with .servbay.config
ServBay provides a powerful feature that lets you configure the required development environment—such as the Python version—automatically for each project, simply by placing a .servbay.config
file at the project’s root directory.
How It Works:
When you cd
into any directory containing a .servbay.config
file (in the ServBay integrated terminal), ServBay automatically reads the file and temporarily adjusts your terminal session environment variables so commands like python
, php
, node
, etc., point to the versions specified there.
.servbay.config
affects only the command-line environment and only applies to the project directory containing the file (and its subdirectories). It does not influence the web server configuration.
File Format:
.servbay.config
is a simple key-value text file. To specify the Python version for a project, add a PYTHON_VERSION
key with the version number installed in ServBay (e.g., 3.11
or 3.12
):
# Example .servbay.config file
# Specify Python 3.11 for this project
PYTHON_VERSION=3.11
# You can also specify other tool versions
PHP_VERSION=8.2
NODE_VERSION=20
# ... other options, such as registry settings and cache paths ...
# NPM_CONFIG_REGISTRY=https://registry.npmmirror.com/
# GOPROXY=https://goproxy.cn,direct
2
3
4
5
6
7
8
9
10
11
Advantages:
- Automatic Version Switching: No need to manually switch global Python versions or repeatedly activate a specific virtual environment (though virtual environments remain best practice for managing dependencies). Simply enter the project directory, and the environment adapts automatically.
- Project Isolation: Guarantees each project runs with its intended Python version, preventing issues from changes in the global environment.
- Team Collaboration:
.servbay.config
can be added to version control (e.g., Git), letting all team members get the same runtime environment simply by checking out the project.
Typical Usage:
Suppose you have two projects:
project-a
requires Python 3.11project-b
requires Python 3.14
You can create a .servbay.config
file in each project’s directory:
/Applications/ServBay/www/project-a/.servbay.config
:iniPYTHON_VERSION=3.11
1/Applications/ServBay/www/project-b/.servbay.config
:iniPYTHON_VERSION=3.14
1
After you cd /Applications/ServBay/www/project-a
in the ServBay terminal and run python -V
, you'll see Python 3.11.x. If you then cd /Applications/ServBay/www/project-b
and run python -V
, you get Python 3.14.x.
Note: .servbay.config
sets the base Python version detected by ServBay. You should still use a Python virtual environment (venv
, pipenv
, or poetry
) to handle your project's dependencies on top of this.
Integrating with Other ServBay Components
One of ServBay’s main strengths is its integrated local development environment. You can easily combine Python with other ServBay components:
- Web Development: Use ServBay’s Caddy or Nginx web server to host web applications built with Python frameworks like Django or Flask. You can configure the web server to proxy requests to your Python application server (such as Gunicorn or uWSGI).
- Databases: Python apps often require databases. ServBay offers MySQL, PostgreSQL, MongoDB, and more. Use Python’s database libraries (like
psycopg2
for PostgreSQL ormysql-connector-python
for MySQL) to connect to ServBay’s running databases. - Caching: Python applications can take advantage of ServBay’s Redis or Memcached instances for caching to improve performance.
Important Notes
- PATH Environment Variable: ServBay modifies your terminal’s PATH to prioritize tools installed within ServBay. If you have Python installed elsewhere, be aware of which Python version is the default. Use
which python
orwhich python3
to check the currently active executable path. - Virtual Environments: While ServBay provides the base Python installations, using virtual environments for project development is best practice. This keeps dependencies isolated and your system clean.
- Dependencies: Some Python packages may require system-level libraries or development tools to be compiled. If you encounter installation errors, check the package documentation or error messages—you may need to use Homebrew (or similar tools) to install additional dependencies.
Frequently Asked Questions (FAQ)
Q: Does ServBay support concurrent installation of Python 2.x and Python 3.x?
A: Yes, you can install both Python 2.x and Python 3.x with ServBay and use python2
and python3
commands to run them.
Q: I installed multiple Python versions. How do I set the default python
command to a specific version?
A: You can set the default Python 2.x and Python 3.x versions in ServBay’s "Settings". For finer control or if you need project-specific versions, it’s strongly recommended to use .servbay.config
. With .servbay.config
, the python
command is automatically forced to the specified version for that project.
Q: How do I install third-party libraries for Python managed by ServBay?
A: Use the pip
command corresponding to the Python version you want (for example, for Python 3.12, use python3.12 -m pip install <package-name>
). Ideally, perform installs inside the activated virtual environment.
Q: How should I configure ServBay’s web server for Python web applications?
A: This usually involves editing the Caddy or Nginx site configuration files, setting up reverse proxy rules to forward incoming HTTP requests to the port your Python application server is listening on. The specifics will depend on the Python web framework and application server you use. Check ServBay’s site configuration documentation for basic guides on web server setup.
Summary
ServBay provides Python developers on macOS with a streamlined, integrated local development environment. With ServBay, you can easily install and manage multiple Python versions, handle dependencies with pip
, and seamlessly integrate Python apps with ServBay’s web servers and databases for efficient local development and testing. Combined with virtual environments, ServBay lets you build stable, isolated development setups tailored to each project.