iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔧

Installing Python 3.9 and Enabling pip on Synology NAS via Official Methods

に公開

TL;DR

  • Current Synology NAS allows you to install Python 3.9 as a standard feature
  • To use pip, you can either install pip using the ensurepip package or create a virtual environment with venv

Background

Synology NAS (Network Attached Storage) can run various self-made programs. In particular, using Python enables a wide range of processes such as website monitoring, IoT, and backups. I periodically run a program that automatically reflects my own GitHub repositories onto the NAS.

https://zenn.dev/kn1cht/articles/github-backup-nas

Previously, Python could be installed from the Package Center (the standard package manager), but since the versions were relatively old, there were many cases of using external package managers or Docker.

Since around 2022, Python 3.9 has been added to the standard packages available from the Package Center. As of 2023, the beta label has disappeared, and it is now stable to use. However, out of the box, the pip command is not available, and a little bit of work is required for a comfortable experience.
Therefore, in this article, I will explain how to enable the pip command with the standard Python 3.9.

Python 3.9 package installation screen

Environment

Python Installation

Open the Package Center and search for or find Python 3.9 in the list to install it.

Python 3.9 package installation screen

Log in via SSH and run python3.9 to verify that it is available.

$ python3.9
Python 3.9.14 (main, Mar 24 2023, 10:05:06)
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

Enabling pip

On the other hand, you will likely see a "command not found" error if you try to run pip. Since this prevents us from installing Python packages, we will now set up pip.

While there are methods to install modules using Web Station, they involve many GUI steps and can be quite a hassle. In this article, I will introduce two methods that can be completed entirely via the command line.

Installing pip directly

Reference page: How to install Python pip on Synology NAS - Sindastra's info dump

https://www.sindastra.de/p/2290/how-to-install-python-pip-on-synology-nas

This method uses the ensurepip module, which has been available since Python 3.4.

$ sudo python3.9 -m ensurepip --upgrade
$ sudo python3.9 -m pip install --upgrade pip

Once the installation is complete, a message will appear showing the installation directory (in my environment, /var/packages/Python3.9/target/usr/bin) and prompting you to add it to your PATH. After updating your PATH, commands such as pip3.9 will be enabled.

$ echo export PATH='/var/packages/Python3.9/target/usr/bin:$PATH' >> ~/.bashrc
$ source ~/.bashrc

(If you are using a different shell such as zsh, please read ~/.bashrc as ~/.zshrc, etc.)

After that, you can install any packages you like.

$ sudo pip3.9 install numpy

Creating a Virtual Environment with venv

Reference page: How can I set up a Python virtual environment on Synology NAS? - Synology Knowledge Center

https://kb.synology.com/en-global/DSM/tutorial/Set_up_Python_virtual_environment_on_NAS

If you use pip with the method above, you will be warned that "running as root is dangerous, use a virtual environment instead." So, this time we will use venv, the virtual environment module built into modern Python, to use pip.

$ cd # Move to the directory where you want to store the virtual environment files
$ python3.9 -m venv default
$ source default/bin/activate

This activates the virtual environment, and pip will be available under {environment_name}/bin. With this method, sudo is not required.

$ pip3.9 install numpy

When automatically calling a script with the Task Scheduler, be careful as it will result in an error unless you execute source /{path_to_virtual_env_directory}/bin/activate first.

GitHubで編集を提案

Discussion