Bug #97351 mysql-shell plugin module can‘t search PYTHONPATH
Submitted: 24 Oct 2019 5:23 Modified: 12 Nov 2019 9:42
Reporter: Bin Hong (OCA) Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Shell General / Core Client Severity:S3 (Non-critical)
Version:8.0.18 OS:MacOS (10.15)
Assigned to: CPU Architecture:Any

[24 Oct 2019 5:23] Bin Hong
Description:
My macOS uses homebrew to install python 3.7, and set PYTHONPATH in ~/.mysqlsh/mysqlshrc.py file, but mysqlsh starts loading python plugin, import library, can't search library file from PYTHONPATH

2019-10-24 05:08:13: Error: Error loading Python file '/Users/hongbin/.mysqlsh/plugins/ext/router/init.py':
	Execution failed:
Traceback (most recent call last):
  File "init.py", line 5, in <module>
  File "/Users/hongbin/.mysqlsh/plugins/ext/router/status.py", line 2, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

/usr/local/lib/python3.7/site-packages/requests is exist

How to repeat:
Set PYTHONPATH in ~/.mysqlsh/mysqlshrc.py file , create a plugin to import third party libraries
[11 Nov 2019 5:10] MySQL Verification Team
Hello Bin Hong,

Thank you for the report.

regards,
Umesh
[12 Nov 2019 9:27] Pawel Andruszkiewicz
Posted by developer:
 
When Shell is executing the mysqlshrc.py file it is too late to modify the PYTHONPATH environment variable, as at that point Python runtime has already been initialized. In order to change the Python search path, the PYTHONPATH environment variable needs to be set before shell is launched. Alternatively, mysqlshrc.py can be used to modify the sys.path variable (please note, that in that case shell needs to be launched with --py command line option, as startup scripts are executed only when corresponding scripting modes are entered, see https://dev.mysql.com/doc/mysql-shell/en/mysql-shell-creating-startup-scripts.html for details).

$ ll ~/tmp/fibo.py 
-rw-r--r-- 1 pawel pawel 334 lis 12 09:35 /home/pawel/tmp/fibo.py

$ cat ~/.mysqlsh/plugins/dummy/init.py
import fibo
print(fibo.fib(1000))

$ PYTHONPATH=~/tmp mysqlsh
MySQL Shell 8.0.18

Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.

Type '\help' or '\?' for help; '\quit' to exit.
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 
None
 MySQL  JS > 

$ echo -e "import sys\nsys.path.append('/home/pawel/tmp')" > ~/.mysqlsh/mysqlshrc.py

$ mysqlsh --py
MySQL Shell 8.0.18

Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.

Type '\help' or '\?' for help; '\quit' to exit.
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 
None
 MySQL  Py >
[12 Nov 2019 9:42] Bin Hong
Thank you very much.