One of the advantages of installing python in your Synology is that you can write and schedule python scripts.
In this post I will show you how to get python 3.9 running, but in a future post I will show you how to schedule it.
Enable Home for all users
First, lets enable home for all users. That is where your python scripts will reside.
Enable SSH
I recommend you to disable SSH when you are done because it disables 2 factor auth and makes your synology more vulnerable.
Go to Control Panel > Terminal & SNMP > Terminal tab:
- Check “Enable SSH service”
- Click “Apply”
Run SSH
Before we run SSH, we need to know the IP of the server:
So now Press the windows icon and type cmd to start the cmd prompt and type:
username is your admin username.
ssh username@192.168.1.xx
Enter your admin password and you are in!
Upgrade Python
All synologies come with python 2 pre-install, but we want to use python 3. Lets upgrade it from the package center:
You will find your python 3.9 here:
/volume1/\@appstore/Python3.9/usr/bin/python3.9
If you look in the site-packages directory, you will see it is empty:
ls /volume1/\@appstore/Python3.9/usr/lib/python3.9/site-packages
so we need to install pip.
Either add the python3.9 to PATH or specify it when installing.
Sudo makes it available for all users. Your choice.
sudo /volume1/\@appstore/Python3.9/usr/bin/python3.9 -m ensurepip
Check if it has been installed:
ls /volume1/\@appstore/Python3.9/usr/lib/python3.9/site-packages
You should see pip there:
Update the pip, setuptools and wheel packages:
sudo /volume1/\@appstore/Python3.9/usr/bin/python3.9 -m pip install --upgrade pip setuptools wheel
And now install virtual environment:
sudo /volume1/\@appstore/Python3.9/usr/bin/python3.9 -m pip install virtualenv
and verify it is there:
ls /volume1/\@appstore/Python3.9/usr/lib/python3.9/site-packages
Now we are going to create a folder on our home directory
mkdir el_tracker
If you now navigate to your Home folder, you will see the folder there:
Navigate to the folder:
cd el_tracker
and create the virtual environment:
Note: elenv is the name of our virtual environment.
python3 -m venv elenv
And now you can activate it:
source elenv/bin/activate
Now that we are inside the environment we can install the packages we need.
The easiest way to get this done is to create a requirements.txt file and save it on your el_tracker project folder.
This is how the file looks like:
To get all those packages installed at once:
pip install -r requirements.txt
Now you installed everything:
- Close the cmd window
- Disable SSH
- Make sure 2FA is still enabled
and happy python coding!
Coming next….
On my next post I will show you how to schedule your python script on your NAS.