关于python环境后续问题解决方案

在Linux环境中安装Django时遇到Python版本冲突。系统显示为Python2.7,但已安装Python3.8。通过修改默认Python软链接解决版本问题,备份原有链接,将`python`和`pip`链接指向Python3.8的执行文件。然而,安装Django时因缺少依赖导致失败。进一步排查发现,`yum`依赖于python2.7.5,需要修改和中的Python路径以避免版本不匹配。通过编辑这两文件并更新为指向Python3的软链接,成功解决依赖问题。

安装django的时候错误报告:


    This version of Django requires Python 3.6, but you're trying to
    install it on Python 2.7.
    
    This may be because you are using a version of pip that doesn't
    understand the python_requires classifier. Make sure you
    have pip >= 9.0 and setuptools >= 24.2, then try again:
    
        $ python -m pip install --upgrade pip setuptools
        $ python -m pip install django
    
    This will install the latest version of Django which works on your
    version of Python. If you can't upgrade your pip (or Python), request
    an older version of Django:
    
        $ python -m pip install "django<2"
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-pOvD4I/Django
You are using pip version 7.1.2, however version 20.2.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

这里面说我的python版本是2.7 而我刚刚安装的python版本是3.8 应该满足要求了
我们命令行输入python,获取到的版本号确实是2.7
样例输出

Python 2.7.5 (default, Aug  7 2019, 00:51:29) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

我们改变以下默认python启动的软链接

  1. 把python默认启动软链接备份(文件位于/usr/bin/目录下)
    这里我为什么改名为python_back_2.7而不是python2.7.5 ,因为我发现/usr/bin/目录下python2.7.5已存在,所以我们最好是在备份之前看看python的软链接文件都有哪些
mv /usr/bin/python /usr/bin/python_back_2.7
  1. 把pip默认启动软链接备份(文件位于/usr/bin/目录下)
mv /usr/bin/pip /usr/bin/pip_back_2.7
  1. 建立新的软链接

/usr/local/bin/python3/bin/python3改为你安装的python3的bin目录下的python执行文件,pip同理
python

ln -s /usr/local/bin/python3/bin/python3 /usr/bin/python

pip

ln -s /usr/local/python3/bin/pip3 /usr/bin/pip

修改完成后,下载django成功
但是下载viapi-utils失败,错误信息如下

ModuleNotFoundError: No module named '_ctypes'

查资料发现是缺少依赖:libffi
尝试yum安装依赖发现报错
报错信息如下:

-bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory
File "/usr/bin/yum", line 30
    except KeyboardInterrupt, e:
                            ^
SyntaxError: invalid syntax

查资料得知,这个是因为系统默认是通过python2.7.5来解析yum,我们把python的默认启动软链接改为python3会出现版本不匹配的问题,我们要修改两个文件来使其一致
文件为:/usr/bin/yum/usr/libexec/urlgrabber-ext-down

我们把这两个文件开头的#!/usr/bin/python 改为#!/usr/bin/刚刚备份的python2.7.5的软链接的名字,我这里是python_back_2.7,所以我改成#!/usr/bin/python_back_2.7,如果/usr/bin下有pyhton2.7.5的软链接,可以直接改为#!/usr/bin/python2.7.5

/usr/bin/yum原文件内容如下

#!/usr/bin/python
import sys
try:
    import yum
except ImportError:
    print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

   %s

Please install a package which provides this module, or
verify that the module is installed correctly.

It's possible that the above module doesn't match the
current version of Python, which is:
%s

If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://yum.baseurl.org/wiki/Faq
  
""" % (sys.exc_value, sys.version)
    sys.exit(1)

sys.path.insert(0, '/usr/share/yum-cli')
try:
    import yummain
    yummain.user_main(sys.argv[1:], exit_code=True)
except KeyboardInterrupt, e:
    print >> sys.stderr, "\n\nExiting on user cancel."
    sys.exit(1)

我们可以在第30行看到错误报告中的内容

except KeyboardInterrupt, e:
    print >> sys.stderr, "\n\nExiting on user cancel."
    sys.exit(1)

/usr/share/yum-cli文件中第26行的内容引起了我的注意

sys.path.insert(0, '/usr/share/yum-cli')

这应该是一个关联文件夹,我们进到文件夹里面可以看到一些文件如下
在这里插入图片描述
查看这些文件发现,文件开头都为#!/usr/bin/python -t

/usr/libexec/urlgrabber-ext-down原文件内容如下

#! /usr/bin/python
#  A very simple external downloader
#  Copyright 2011-2012 Zdenek Pavlas

#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public
#   License as published by the Free Software Foundation; either
#   version 2.1 of the License, or (at your option) any later version.
#
#   This library is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public
#   License along with this library; if not, write to the
#      Free Software Foundation, Inc.,
#      59 Temple Place, Suite 330,
#      Boston, MA  02111-1307  USA

import time, os, errno, sys
from urlgrabber.grabber import \
    _readlines, URLGrabberOptions, _loads, \
    PyCurlFileObject, URLGrabError

def write(fmt, *arg):
    try: os.write(1, fmt % arg)
    except OSError, e:
        if e.args[0] != errno.EPIPE: raise
        sys.exit(1)

class ProxyProgress:
    def start(self, *d1, **d2):
        self.next_update = 0
    def update(self, _amount_read):
        t = time.time()
        if t < self.next_update: return
        self.next_update = t + 0.31
        write('%d %d\n', self._id, _amount_read)

def main():
    import signal
    signal.signal(signal.SIGINT, lambda n, f: sys.exit(1))
    cnt = 0
    while True:
        lines = _readlines(0)
        if not lines: break
        for line in lines:
            cnt += 1
            opts = URLGrabberOptions()
            opts._id = cnt
            for k in line.split(' '):
                k, v = k.split('=', 1)
                setattr(opts, k, _loads(v))
            if opts.progress_obj:
                opts.progress_obj = ProxyProgress()
                opts.progress_obj._id = cnt

            dlsz = dltm = 0
            try:
                fo = PyCurlFileObject(opts.url, opts.filename, opts)
                fo._do_grab()
                fo.fo.close()
                size = fo._amount_read
                if fo._tm_last:
                    dlsz = fo._tm_last[0] - fo._tm_first[0]
                    dltm = fo._tm_last[1] - fo._tm_first[1]
                ug_err = 'OK'
            except URLGrabError, e:
                size = 0
                ug_err = '%d %d %s' % (e.errno, getattr(e, 'code', 0), e.strerror)
            write('%d %d %d %.3f %s\n', opts._id, size, dlsz, dltm, ug_err)

if __name__ == '__main__':
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值