DEV Community

Be Hai Nguyen
Be Hai Nguyen

Posted on • Updated on

Synology DS218: preparing Python 3.9 Beta compelete devepment environment.

We discuss how to set up a Python 3.9 Beta development environment on
Synology DS218 DiskStation running DSM version 7.1-42661 Update 1. The
development environment includes: pip, virtualenv, setuptools, wheel
and the flask development web server.

025-feature-image.jpg
Synology DS218: preparing Python 3.9 Beta compelete devepment environment.

Synology DS218 DiskStation -- Versatile 2-bay NAS for small offices and home users is a Linux device. It runs a Linux distro named DSM, and the version on my device is 7.1-42661 Update 1.

There're so many different Linux flavours. During my research on this topic, I realised I'll have some difficulties. And I did. I have not found a complete guide on how to do this, the steps that work for me, which I've put together in this post come from several other existing posts, I'm listing those referenced posts along the way.

I'd like to mention that, following the instructions in this post How Do I use Synology Web Station?, I've successfully got a PHP port-based virtual site hosted by Apache 2.4 to run in under 30 ( thirty ) minutes -- and I can access it from my Windows 10 machine. Python, somehow is not that straightforward.

Table of contents

Determining Synology NAS' IP address

Go to Control Panel > Network > Network Interface tab: look under LAN.

Mine's 192.168.0.6 -- and I'll reference this throughout this post.

Enable home service

Home service means the user home directory.

“behai” is the user I set up when first installed DSM. This is not the root user.

Go to Control Panel > User & Group > select behai > Advanced tab:

Check “Enable user home service”, then click “Apply”.

Enable SSH

Go to Control Panel > Terminal & SNMP > Terminal tab:

Check “Enable SSH service”, then click “Apply”.

SSH to DSM from Windows 10

From a Windows command prompt, we should now be able to access DSM command line via SSH, using:

ssh behai@192.168.0.6
Enter fullscreen mode Exit fullscreen mode

It'll ask for the password, this's the same password we use to log into the device.

After logging into SSH terminal, both commands:

$ echo $HOME
$ pwd
Enter fullscreen mode Exit fullscreen mode

will return the same value, which is the home directory, mine is:

/var/services/homes/behai
Enter fullscreen mode Exit fullscreen mode

Existing Python installation

From Package Center under Installed, it shows Python2 comes pre-installed ( when we first installed
DSM ).

But both:

$ python
$ python3
Enter fullscreen mode Exit fullscreen mode

show Python 3.8.12, please see the screen capture below:

025-01.png

Install Web Station and Python 3.9 Beta

I thought I would need Web Station, in hindsight, however, at this stage, I don't think I need Web Station yet, but I did install it. So I will just go ahead and include this step in this post.

Install Web Station

To start off, I follow the instructions in How Do I use Synology Web Station? to install Web Station, PHP 8.0, set up aPHP port-based virtual site hosted by Apache 2.4, and I was able to get this virtual site to run in under 30 ( thirty ) minutes -- and I can access it from my Windows 10 machine.

Basically, use Package Center to install Web Station. The installation will create a web root directory:

/volume1/web/
Enter fullscreen mode Exit fullscreen mode

File Station also sees this directory.

Install Python 3.9 Beta

At this point, I thought, similar to PHP, I would need to carry out the same process for Python:

Go to Main Menu > Web Station > Script Language Settings > Python tab -- please see screen capture below:

025-02.png

Python 3.8.12 has been installed, but it still said no Python 3 package. Use Package Center to install Python 3.9 Beta. Click on “Join Beta” to install. See screen capture below:

25-03.png

After this a User-defined customised profile for Python 3.9 should appear under Web Station's Script Language Settings.

Where is Python 3.9 Beta installed, how do I invoke it?

This post Re-Install pip and virtualenv Site Packages After Package Center Python 3.8.2 Upgrade, is a bit outdated ( I think ), but it helps me to figure out where Python 3.9 is installed:

/volume1/\@appstore/Python3.9/usr/bin/python3.9
Enter fullscreen mode Exit fullscreen mode

In fact:

/volume1/\@appstore/Python3.9/usr/bin/ 
Enter fullscreen mode Exit fullscreen mode

is where Python 3.9 related tools live. This path is recognised by the system. Command:

$ python3.9
Enter fullscreen mode Exit fullscreen mode

should just run.

Install pip, upgrade pip, and verify pip has been installed

Install pip

I tried various instructions, they did not work. This https://jackgruber.github.io/2021-06-27-install-pip-on-synology/ post works. I changed to python3.9:

$ sudo python3.9 -m ensurepip
Enter fullscreen mode Exit fullscreen mode

Please see the following screen capture:

025-05-installing-pip.png

Regarding the warning:

WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

I don't know what it means yet. As for the warning:

WARNING: The scripts pip3 and pip3.9 are installed in '/var/packages/Python3.9/target/usr/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

I've not taken any action yet... I'll be using the full path initially.

Upgrade pip

To upgrade pip, I ran the following command:

$ sudo python3.9 -m pip install --upgrade pip
Enter fullscreen mode Exit fullscreen mode

I'm also just sleeping on the warnings for the time being, till something stops working.

Verify pip has been installed

Using this already mentioned post Re-Install pip and virtualenv Site Packages After Package Center Python 3.8.2 Upgrade, the following command verifies that pip directory exists:

$ ls -l /volume1/\@appstore/Python3.9/usr/lib/python3.9/site-packages
Enter fullscreen mode Exit fullscreen mode

pip, pip3, pip3.9 and pip3.10 also exist in:

$ ls -l /volume1/\@appstore/Python3.9/usr/bin
Enter fullscreen mode Exit fullscreen mode

Checking pip version

**I HAVE TRIED VARIOUS COMMANDS, but none has worked. I don't know how to do this presently**. I'll post an update after I have figured it out.

Update setuptools and wheel packages

Run this command:

$ sudo python3.9 -m pip install --upgrade pip setuptools wheel
Enter fullscreen mode Exit fullscreen mode

I included pip, which was unnecessary as it has already been done.

Install virtualenv and verify installation

Install virtualenv

Run this command:

$ sudo /volume1/\@appstore/Python3.9/usr/bin/python3.9 -m pip install virtualenv
Enter fullscreen mode Exit fullscreen mode

I included the full path for python3.9, which I don't think is necessary.

Verify virtualenv has been installed

To verify, run this command:

$ ls /volume1/\@appstore/Python3.9/usr/lib/python3.9/site-packages
Enter fullscreen mode Exit fullscreen mode

See the screen capture below, virtualenv has been installed:

025-07-installing-virtualenv-a.png

The bin directory is where related executables live:

$ ls -l /volume1/\@appstore/Python3.9/usr/bin/
Enter fullscreen mode Exit fullscreen mode

Please see the output in the screen capture below:

025-07-installing-virtualenv-b.png

Create virtual environment venv

Under $HOME, i.e. /var/services/homes/behai/ directory, create directory app_demo with command:

$ mkdir app_demo
Enter fullscreen mode Exit fullscreen mode

Then go to this directory:

$ cd app_demo
Enter fullscreen mode Exit fullscreen mode

Issue the below command to create virtual environment venv:

$ sudo /volume1/\@appstore/Python3.9/usr/bin/virtualenv -p python3.9 venv
Enter fullscreen mode Exit fullscreen mode

Verify venv created:

$ ls -l venv/
Enter fullscreen mode Exit fullscreen mode

Check venv/bin directory:

$ ls -l venv/bin/
Enter fullscreen mode Exit fullscreen mode

-- We can also see the content of these directories using File Station.

The running of these commands is shown in the screen capture below:

025-08-creating-virtual-environment-venv.png

Please note, in Windows, venv\Scripts\ is the equivalence of venv/bin.

Activate and Deactivate the virtual environment venv

Activate the virtual environment venv

To activate, run:

$ source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

The prompt changes as seen in the screen capture below:

025-09-activate-virtual-environment-venv.png

Deactivate the virtual environment venv

**I DON'T KNOW HOW TO DEACTIVATE IT YET. I'm not sure what venv/bin/deactivate.nu is for. I can't find info on it either**. I just terminate the SSH session, get back in, and it still works.

The test app

This part is similar to Python: Application ( Self ) Installation, Built Distribution and Test the Built Distribution. , which I have written for Windows 10.

Install required packages

Under /var/services/homes/behai/app_demo, create setup.py file and src/ directory.

/var/services/homes/behai/app_demo
|
|-- setup.py
|
|-- src/
Enter fullscreen mode Exit fullscreen mode
File /var/services/homes/behai/app_demo/setup.py
Enter fullscreen mode Exit fullscreen mode
"""Installation script for flask_restx demo project."""
from pathlib import Path
from setuptools import setup, find_packages

setup(
    name='dsm-python-demo',
    description='flask dev server on Synology DSM demo.',
    version='1.0.0',
    author='Van Be Hai Nguyen',
    author_email='behai_nguyen@hotmail.com',
    packages=find_packages(where="src"),
    package_dir={"": "src"},
    python_requires='>=3.9',
    install_requires=[
        'Flask',
        'python-dotenv',
    ],
)
Enter fullscreen mode Exit fullscreen mode

Run the command below to install packages for the test application that we are going to write:

$ sudo venv/bin/pip install -e .
Enter fullscreen mode Exit fullscreen mode

This should run with no problems. After finished, use File Station to scan through the directories to verify that we have the specified packages installed.

Complete the test application

Updated 21/07/2022 -- the code can be cloned from GitHub using:

git clone -b v1.0.0 https://github.com/behai-nguyen/app-demo.git
Enter fullscreen mode Exit fullscreen mode

Now create the rest of the application:

/var/services/homes/behai/app_demo/
|
|-- .env
|-- app.py
|-- setup.py
|
|-- src/
|   |
|   |-- app_demo/
|       |   
|       |-- __init__.py
|       |-- config.py
|
|-- venv/
Enter fullscreen mode Exit fullscreen mode
File /var/services/homes/behai/app_demo/.env
Enter fullscreen mode Exit fullscreen mode
FLASK_APP=app.py
FLASK_ENV=development
SECRET_KEY=">s3g;?uV^K=`!(3.#ms_cdfy<c4ty%"
Enter fullscreen mode Exit fullscreen mode
File /var/services/homes/behai/app_demo/app.py
Enter fullscreen mode Exit fullscreen mode
"""Flask Application entry point."""

from app_demo import create_app

app = create_app()
Enter fullscreen mode Exit fullscreen mode
File /var/services/homes/behai/app_demo/src/app_demo/__init__.py
Enter fullscreen mode Exit fullscreen mode
"""Flask app initialization via factory pattern."""
from flask import Flask

from app_demo.config import get_config

def create_app():
    app = Flask( 'dsm-python-demo' )

    app.config.from_object( get_config() )

    @app.route( '/' )
    def hello_world():
        return '<p>Hello, World!</p>'

    return app
Enter fullscreen mode Exit fullscreen mode
File /var/services/homes/behai/app_demo/src/app_demo/config.py
Enter fullscreen mode Exit fullscreen mode
"""Flask app config settings."""
import os

class Config:
    """Set Flask configuration from .env file."""

    # General Config
    SECRET_KEY = os.getenv( 'SECRET_KEY' )
    FLASK_APP = os.getenv( 'FLASK_APP' )
    FLASK_ENV = os.getenv( 'FLASK_ENV' )

def get_config():
    """Retrieve environment configuration settings."""
    return Config
Enter fullscreen mode Exit fullscreen mode

Run the test application

Run it with:

$ sudo venv/bin/flask run --host=0.0.0.0 --port=9090
Enter fullscreen mode Exit fullscreen mode

025-10-run-test-app-a.png

From Windows, open this URL http://192.168.0.6:9090/:

The application responds as expected:

025-10-run-test-app-b.png

Recall that 192.168.0.6 is my Synology NAS address.

Concluding remarks

This's been an interesting exercise. I've learned a lot along the way. What I really want to achieve is to get the DSM Apache 2.4 web server to hostPython web applications in a similar manner as Internet Information Services (IIS) -- which I've done and described in Python: hosting a virtualenv Flask application in Windows 10 Pro's Internet Information Services (IIS). I'll continue to work towards this objective, I'll do another post after I've learned how to do it, and do it successfully.

Thank you for reading, and I hope you find this post useful.

Top comments (0)