When I tried to run a Python script in Synology's DS118 from DSM's Task Scheduler, I got a module import error.
Error in the email received from DSM
Traceback (most recent call last):
File "hoge.py", line 1, in <module>
import tweepy
ModuleNotFoundError: No module named 'tweepy'
Isn't it funny? I can do it with an SSH connection. ..
Moreover, I had a similar error with requests
before, but at that time I didn't need to do it with the task scheduler so much, so I left it.
However, this time I had to do it with the task scheduler, so I decided to deal with it seriously.
When I looked it up without knowing it, it seemed that the pass did not pass. Perhaps it's subtly different from running in SSH and running in Task Scheduler.
So, I tried to deal with it as follows.
Note that python is installed from the Package Center and the version is 3.8.2
, and the SDK version is DSM 6.2.3-25426 Update 3
.
From each of the SSH connection and Task Scheduler, run the following code to see if there is a difference in the list of paths.
check_module_path.py
import sys
path_list = sys.path
with open('/your_storage/your_directory/check_module_path.txt', 'w', encoding='utf-8') as f:
for path in path_list:
f.write(path + '\n')
Path when executed from SSH connection
/my_storage/my_directory
/var/packages/py3k/target/usr/local/lib/python38.zip
/var/packages/py3k/target/usr/local/lib/python3.8
/var/packages/py3k/target/usr/local/lib/python3.8/lib-dynload
/var/services/homes/synology_user_name/.local/lib/python3.8/site-packages
/var/packages/py3k/target/usr/local/lib/python3.8/site-packages
/usr/lib/python3/site-packages
Path when executed from task scheduler
/my_storage/my_directory
/var/packages/py3k/target/usr/local/lib/python38.zip
/var/packages/py3k/target/usr/local/lib/python3.8
/var/packages/py3k/target/usr/local/lib/python3.8/lib-dynload
/var/packages/py3k/target/usr/local/lib/python3.8/site-packages
/usr/lib/python3/site-packages
Hmmm, /var/services/homes/synology_user_name/.local/lib/python3.8/site-packages
is in the SSH connection but not in the task scheduler.
There seems to be a module for tweepy
and requests
here, so check the inside of this directory with ls -l
from the SSH connection.
python
ls -l /var/services/homes/synology_user_name/.local/lib/python3.8/site-packages
#Abbreviation (many)
# drwxrwxrwx+ 3 synology_user_name users 4096 Jan 14 05:57 tweepy
#Abbreviation (many)
There was tweepy
!
Of course, there was also requests
!
There are two ways to deal with it.
The first is to add about two lines of code in the script, which is easier. Of course, I'm a lazy guy who chooses the easier one if he gets lost. ..
The second method is to pass the default path from the task scheduler. Perhaps I should deal with it here, but I don't know how to do it at my level. .. But for the time being, I'll just write it.
Write the following code ** before the module import statement.
Described before the import statement of the module!
import sys
sys.path.append('/var/services/homes/synology_user_name/.local/lib/python3.8/site-packages')
With this alone, you can avoid errors! It's easy to understand, but it's difficult to understand, isn't it?
In order to pass the path, it may be possible to add the following from ~/.bashrc
with an editor such as vi
, but I do not know how to do this on the task scheduler. ..
python
export PATH=$PATH:"/var/services/homes/synology_user_name/.local/lib/python3.8/site-packages"
Is it possible to do vi
in the task scheduler script? ??
If you know, I would appreciate it if you could teach me m (_ _) m
Check / add the path of the target directory of import in Python (sys.path etc.)| note.nkmk.me [Python environment construction] Explanation of environment variables!| WEBCAMP NAVI [Python package storage location-Qiita](https://qiita.com/ophhdn/items/4d3ecc6354d92b7ac0ba"pythonのパッケージの保存場所 - Qiita") [How to pass the default path in Python (Windows, Linux)-Qiita](https://qiita.com/maech/items/72559402556eb2af73ad"Python内のデフォルトパスを通す方法(Windows, Linux) - Qiita")
Recommended Posts