不同用户python path 权限问

发布时间:2019-10-18 09:07:03编辑:auto阅读(2450)

    问题背景

    root 用户可以运行pip install mkdocs
    但gitlab-runner 运行pip install mkdocs 则下列错误

    pip install mkdocs
    Traceback (most recent call last):
      File "/bin/pip", line 7, in <module>
        from pip._internal import main
    ImportError: No module named 'pip._internal'

    经过排查,发现是gitlab-runner 在某些python path 的权限问题

    解决方法

    1. cat /bin/pip 查看使用的python 代码

    #!/usr/bin/python3
    
    # -*- coding: utf-8 -*-
    import re
    import sys
    
    from pip._internal import main
    
    if __name__ == '__main__':
        sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
        sys.exit(main())

    可知,使用的是python3

    查看path 路径

    切换到root 用户
    然后在命令行运行:

    [root@test75vm1 ~]# python3
    Python 3.4.5 (default, May 29 2017, 15:17:55) 
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> sys.path
    ['', '/usr/lib64/python34.zip', '/usr/lib64/python3.4', '/usr/lib64/python3.4/plat-linux', '/usr/lib64/python3.4/lib-dynload', '/usr/lib64/python3.4/site-packages', '/usr/lib/python3.4/site-packages']

    check path 路径的权限

    ls -rlt /usr/lib/python3.4/site-packages

    修改python path 路径的权限

    ls -rlt 发现有写目录是没有执行权限的。
    执行命令

    chmod 755 -R /usr/lib64/python3.4 /usr/lib64/python3.4/plat-linux /usr/lib64/python3.4/lib-dynload /usr/lib64/python3.4/site-packages /usr/lib/python3.4/site-packages

关键字