2

I'm facing of a strange issue, and after a couple of hour of research I'm looking for help / explanation about the issue. It's quite simple, I wrote a cgi server in python and I'm working with some libs including pynetlinux for instance. When I'm starting the script from terminal with any user, it works fine, no bug, no dependency issue. But when I'm trying to start it using a script in rc.local, the following code produce an error.

import sys, cgi, pynetlinux, logging

it produce the following error :

   Traceback (most recent call last):
      File "/var/simkiosk/cgi-bin/load_config.py", line 3, in 
      import cgi, sys, json, pynetlinux, loggin
   ImportError: No module named pynetlinux

Other dependencies produce similar issue.I suspect some few things like user who executing the script in rc.local (root normaly) and trying some stuff found on the web without success.

Somebody can help me ?

Thanx in advance.

Regards.

Ollie314

4

2 回答 2

4

首先,您需要确保您要导入的模块是否安装正确。您可以检查模块的名称是否存在于pip list


然后,在 python shell 中,检查 Python 查找模块的路径:

    import sys
    sys.path

就我而言,输出是:

['', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages']


最后,将这些路径附加到 /etc/rc.local 中的 $PATH 变量。这是我的 rc.local 的示例:

    #!/bin/sh -e
    #
    # rc.local
    #
    # This script is executed at the end of each multiuser runlevel.
    # Make sure that the script will "exit 0" on success or any other
    # value on error.
    #
    # In order to enable or disable this script just change the execution
    # bits.
    #
    # By default this script does nothing

    export PATH="$PATH:/usr/lib/python3.4:/usr/lib/python3.4/plat-x86_64-linux-gnu:/usr/lib/python3.4/lib-dynload:/usr/local/lib/python3.4/dist-packages:/usr/lib/python3/dist-packages"

    # Do stuff

    exit 0
于 2015-05-22T11:31:27.820 回答
1

安装模块的路径通常可能来自 .bashrc 或类似的东西。当 .bashrc 不是交互式 shell 时,它不会被获取。/etc/profile 是您可以进行系统范围路径更改的地方。根据它可能使用的 Linux 版本/发行版 /etc/profile.d/ 在这种情况下 /etc/profile 运行 /etc/profile.d 中的所有脚本,在那里添加一个具有执行权限和 .sh 扩展名的新 shell 脚本.

于 2012-12-11T00:26:33.513 回答