Python on Linux for businesses: virtual environments, services with systemd, automation and troubleshooting. Linux Service helps you in Stockholm and across Sweden.
Python is already on your server
Almost every Linux distribution has Python installed from the start, because the system’s own tools use it. This makes the language a natural choice for automation and integrations. For that very reason, however, you should be careful. If you install packages directly into the system Python, you can break tools that the package manager needs. On Debian and Ubuntu, the system therefore stops such attempts with the error externally-managed-environment.
The right way to work with Python on Linux is to use virtual environments. Each program then gets its own directory with its own packages and versions. In other words, program A can use an older version of a library, while program B uses a newer one. In addition, the program becomes portable to another server. We build all Python solutions in that way.

From script to service with systemd
A Python program that should run all the time needs a service file for systemd. Then the program starts automatically when the server starts, and moreover systemd restarts it if it crashes. We set which user the program runs as, which directory it works in and where the logs end up. As a result, the program never runs as root unless it is absolutely necessary.
The logs end up in the system journal, and there we can search them with journalctl. This makes troubleshooting much easier than when every program writes to its own file. We also add environment variables for settings, so that the same code can run in test and production. Sensitive details such as passwords go in a separate file with strict permissions.

Automation and integrations
Python is particularly well suited when data needs to be fetched, processed and passed on. For example, we build programs that fetch orders from an online shop, create reports in Excel or synchronise customers between two systems. Another common assignment is to read logs and send alerts when something deviates. In addition, we have written our own tools for examining domains and email settings.
Every integration gets error handling and logging from the start. If an external system does not respond, the program tries again after a while instead of giving up. For larger amounts of data, we use libraries such as pandas, and then even millions of rows can be processed in a short time. See also Bash scripts for simpler tasks.

Troubleshooting Python programs
Many of our cases concern programs that stopped working after an update of the operating system. Often Python has received a new version, and as a result the old virtual environment no longer works. The solution is to rebuild the environment with the same package list. We also check whether a library has removed a function that the program uses. It shows clearly in the error message if you know where to look.
Other problems concern performance and memory. For example, a program that reads all the data at once can fill the memory on a small server. In that case, we measure with tools such as py-spy and tracemalloc, and then we rewrite the parts that use the most resources. Often a small change is enough to make the program many times faster.

Security in Python projects
A Python program is never more secure than its dependencies. Therefore, we pin all package versions in a file and check them against known vulnerabilities with pip-audit. When a new vulnerability becomes known, we update the affected package and test the program. In addition, we make sure that the program only has the permissions it needs. This limits the damage if something goes wrong anyway.
We always check data from users and other systems before the program uses it. This applies especially to database queries and file names. We use parameterised queries, so that an attacker cannot slip in their own commands. In this way, even smaller internal tools become secure enough for production.
Contact Linux Service about Python on Linux
Linux Service is part of Datorkirurgen AB and has worked with Linux and programming for more than 30 years. We develop, operate and troubleshoot Python on Linux for businesses across Sweden. The official documentation is at python.org, but we are happy to take care of both the code and the server.
Therefore, contact us when a program has stopped working or when you want to automate a task. We make a quick assessment and propose a solution. We work remotely, and you always receive the code with documentation. Moreover, we can take over the operation of the program afterwards.
Frequently asked questions about Python on Linux
Questions about Python on Linux
- Why is Python already installed on the server?
- Why should you not install packages into the system Python?
- What does externally-managed-environment mean?
- What is a virtual environment?
- How do you create a virtual environment?
- Can two programs use different versions of the same library?
- Which Python version should we use?
- How do you move a program to another server?
- When is Python better than Bash?
- Is Python suitable for large amounts of data?
Python on Linux: services and troubleshooting
- How do you turn a Python program into a service?
- What does Restart=always do in systemd?
- Where do the logs from a Python service end up?
- How do you store passwords securely for a program?
- Why does the program stop working after an update?
- How do you rebuild a virtual environment?
- How do you find what makes the program slow?
- What does py-spy do?
- Why does the program fill the memory?
- How do you test a program before production?
More about security and assignments
- What does pip-audit do?
- How do you protect database queries against attacks?
- Can you build integrations between our systems?
- Is it possible to create reports in Excel automatically?
- Can you troubleshoot a program that someone else has written?
- Do you take over the operation of our Python programs?
- Do you work remotely?
- How long does a normal assignment take?
- What does your help cost?
- How do I contact you about Python?
