[LINUX] How to avoid import errors when running python in Synology DSM Task Scheduler

The error that occurred

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.

Check the path

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')

When executed from an SSH connection

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

When executed from the task scheduler

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!

Workaround

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.

How to deal with it in the script

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?

How to pass the default path from the task scheduler

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

reference

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

How to avoid import errors when running python in Synology DSM Task Scheduler
How to hide the command prompt when running python in visual studio 2015
Summary of how to import files in Python 3
How to solve "attempted relative import beyond top-level package" when importing in Python
How to exit when using Python in Terminal (Mac)
How to develop in Python
How to import a file anywhere you like in Python
How to collect images in Python
How to use SQLite in Python
[Python] How to import the library
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
How to not escape Japanese when dealing with json in python
How to avoid duplication of data when inputting from Python to SQLite.
How to deal with errors when installing Python and pip with choco
[Introduction to Python] How to use class in Python?
How to dynamically define variables in Python
How to do R chartr () in Python
Decorator to avoid UnicodeEncodeError in Python 3 print ()
How to work with BigQuery in Python
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to write a string when there are multiple lines in python
How to dynamically zero pad in Python
How to use regular expressions in Python
[Python] How to deal with module errors
How to use is and == in Python
How to write Ruby to_s in Python
How to take multiple arguments when doing parallel processing using multiprocessing in python
How to start Python (Flask) when EC2 starts
How to use the C library in Python
How to receive command line arguments in Python
How to deal with errors when hitting pip ②
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to generate permutations in Python and C ++
How to embed a variable in a python string
How to simplify restricted polynomial fit in python
How to use Python Image Library in python3 series
How to implement shared memory in Python (mmap.mmap)
How to deal with run-time errors in subprocess.call
Summary of how to use MNIST in Python
How to specify TLS version in python requests
How to notify a Discord channel in Python
How to get the files in the [Python] folder
How to run Leap Motion in non-Apple Python
Difference in object ID due to import in Python
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python
Error when trying to install psycopg2 in Python
How to handle datetime type in python sqlite3
How to make Python Interpreter changes in Pycharm
How to plot autocorrelation and partial autocorrelation in python