 |
|
| |
| Introduction
| |
Python is a clear, general-purpose programming language that is excellent for scripting and automation
and one of the most popular choices for building web applications. Both FreeBSD 15 and
Rocky Linux 10 run Python 3. This page covers installing it, writing and running a
script, managing packages safely with virtual environments, and serving a Python web application behind
Apache.
| |
| Installing Python
| |
On FreeBSD 15, install the Python 3 package (Python 3.12 as of FreeBSD 15.1):
# pkg install python3
$ python3 --version # confirm the version
On Rocky Linux 10, Python 3.12 is the system Python and is usually already present;
if not, add it along with pip:
# dnf install python3 python3-pip
$ python3 --version
Note the interpreter’s location — it is what goes on the first line of a script: on FreeBSD
it is /usr/local/bin/python3, on Rocky Linux /usr/bin/python3. The package installer,
pip, comes with it; invoke it as python3 -m pip so you always use the matching version.
| |
| Writing a Script
| |
A Python script is a plain text file. Start it with a “shebang” naming the interpreter, mark
the file executable, and run it:
$ cat > hello.py <<'EOS'
#!/usr/local/bin/python3
import sys
name = sys.argv[1] if len(sys.argv) > 1 else "world"
print(f"Hello, {name}!")
EOS
$ chmod +x hello.py
$ ./hello.py Dan
Hello, Dan!
(On Rocky Linux, change the first line to #!/usr/bin/python3.) You can also run a script without
the executable bit by passing it to the interpreter directly: python3 hello.py. Indentation is
part of Python’s syntax — keep it consistent (spaces are conventional) or the script will not
run.
| |
| Virtual Environments and pip
| |
Python’s package installer, pip, pulls libraries from the
Python Package Index (PyPI).
Do not install them into the system Python: both systems mark it “externally managed,”
and mixing pip-installed packages with OS packages leads to breakage. Instead create a virtual
environment — a private, self-contained Python tree for one project — and install into
that:
$ python3 -m venv ~/myapp/venv # create the environment
$ source ~/myapp/venv/bin/activate # activate it (your prompt now shows "(venv)")
(venv) $ pip install flask requests # installs only inside the venv
(venv) $ pip freeze > requirements.txt # record the exact versions you used
(venv) $ deactivate # leave the environment
Everything installed while the environment is active stays inside it, so a system upgrade can’t
disturb your project and you never need root. Recreate the same set of packages elsewhere with
pip install -r requirements.txt. If you genuinely need a library available system-wide, prefer the
OS package (a py312- package on FreeBSD, a python3- package on Rocky Linux) over a
system-wide pip install.
| |
| Running a Web Application
| |
Python web applications — whether built with Flask, Django, or FastAPI —
are not run directly by Apache. You run the app under a dedicated application server (gunicorn for
WSGI apps like Flask and Django, uvicorn for async/ASGI apps like FastAPI), manage it as a
service, and put Apache in front
as a reverse proxy:
(venv) $ pip install gunicorn
(venv) $ gunicorn --bind 127.0.0.1:8000 myapp:app # serve the app on localhost:8000
Then enable Apache’s proxy modules (see
Dynamic Modules) and forward to it:
<VirtualHost *:80>
ServerName app.example.com
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:8000/
ProxyPassReverse / http://127.0.0.1:8000/
</VirtualHost>
This is the same pattern used for Java/Tomcat
and for PHP applications: keep the
app bound to localhost so Apache — with HTTPS
— is the only public face. For a small dynamic page rather than a full application, a Python
CGI script is also an option.
| |
| Documentation
| |
On the server, python3 -m pydoc <name> shows the documentation for any installed module.
Online:
|
Toll Free 1-866-GSP-4400 • 1-301-464-9363 • service@gsp.com
Copyright © 1994-2026 GSP Services, Inc.
|