CentOS solves the problem of modulenotfounderror when salt calls python3 script remotely, and solves the problem that PIP3 module cannot be shared by different users

to describe the scenario, a function on the company’s Jboss server needs to call a python script on a centos7.3 machine with salt mode. Python version is 3.7, and the script will call back an HTTP interface to feedback the execution result. In the test environment, I had Linux machine root privileges, installed Python and dependent modules, and tested that the local execution of the PY script was normal and the JBoss Salt calls were normal. After publishing production, it was found that there were no callbacks after the call was issued, and the problem was located.

production environment configuration, the python script has been manually executed, confirm that the script can execute properly. First confirm that the command was actually sent to the Linux server. Listen to the /var/log/messages log and see the failed salt call:

there is an error reason: salt-minion: ModuleNotFoundError: No module named ‘openpyxl’, which is very strange. It is executed directly on the machine, and all the dependencies are there. Why does salt call fail?Therefore, I started to search various baidu sites, and found that there were a lot of materials in this field but not many directly related ones. Either it was a method to solve the installation of Python modules, or the salt-related package missing problem occurred when salt stack called Python, which could not solve my problem.

after communication with colleagues, colleagues suspected that it was caused by different paths of PIP installation modules for different users on the Linux machine. I had no root rights in the production environment, which was the most significant difference from the test environment. Based on this idea, I first inquired the path of the module installed in pip3:

modules were stored in the private path of the current user. Meanwhile, when I wanted sudo to execute the pip3 installation, I found that the system prompt command did not exist and the same error message appeared when I executed the python script as in the messages log:

$ sudo -H python3 static_analyzer.py

File "static_analyzer.py", line 7, i <module>
import openpyxl
ModuleNotFoundError: no module named 'openpyxl'


$ sudo -H pip3 install openpyxl -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com --user

sudo:pip3: command not found

is a good idea, so I looked for ways to share the PIP installation module by user, or at least make it Shared by root. According to the command that this does not exist, one of the blog posts suggests a simple solution, which is to hang the soft connection directly so that root users can find python3 and pip3:

#如下软连接是为了使命令在root用户下 也能正常使用
sudo ln -snf /usr/local/python3/bin/python3  /usr/bin/python3

sudo ln -snf /usr/local/python3/bin/pip3  /usr/bin/pip3

Sure enough, after a soft connection, sudo mode can use python3 and pip3 normally:

sudo -H pip3 install openpyxl -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com --user

With this in mind, let’s run the python script in sudo mode to verify that the current environment root user can execute properly:

sudo -H python3 python_script.py

is completely normal. Without the above soft connection, a ModuleNotFoundError in the Messages log would have appeared in the same command, and so far we have solved the problem that the root user of the PIP3 installed module cannot find.

at the end of the last, through the jboss production environment service page to submit the request, the whole process smoothly completed, perfect!

reference:

view the path to installing a python module using PIP install under Linux

solve pip3 command to install the installation package in the home. Local/lib/python3.5/dist – the problem of packages directory

python3: command not found

sudo used with python

Read More: