• centos 创建python虚拟环境遇到的问题及解决方法


    centos自带了python3.6.8的python, 然后我想安装更高版本的python,比如python3.8.8, 可以自行搜索一下安装方法,有很多(如果安装后的python出现No module named ‘_ssl’ 这个报错,请拉到最后看)
    值得注意的是,安装好之后,由于python3,pip3软链接指向的是python3.6的
    在这里插入图片描述
    这里有2个思路,一个是设置软链接python,pip对应python3.8.8

    # /usr/local/python  这个是我python3.8.8的安装目录,这样设置之后,python指向的就是python3.8.8,pip安装的也是python3.8.8的
    ln -s /usr/local/python/bin/python3 /usr/bin/python
    ln -s /usr/local/python/bin/pip3 /usr/bin/pip
    
    • 1
    • 2
    • 3

    另一个思路是将原来python3,pip3的软链接删掉, 重新建立链接

    rm -rf /usr/bin/python3
    rm -rf /usr/bin/pip3
    # 再重新把python3的软链接指向python3.8.8的
    ln -s /usr/local/python/bin/python3 /usr/bin/python3
    ln -s /usr/local/python/bin/pip3 /usr/bin/pip3
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2种方式没有优劣之分,如果安装很多版本的python的话,比如python3.6, python3.7,python3.8等等, 那么建议每个python版本创建不同的软链接,比如python3.6指向的就是python3.6, python3.7指向的就是python3.7…

    安装好python之后,安装virtualenv 和 virtualenvwrapper

    pip install virtualenv
    pip install virtualenvwrapper
    
    • 1
    • 2

    如果安装virtualenvwrapper报错如下

    Looking in indexes: http://mirrors.cloud.aliyuncs.com/pypi/simple/
    Collecting virtualenvwrapper
      Using cached http://mirrors.cloud.aliyuncs.com/pypi/packages/c1/6b/2f05d73b2d2f2410b48b90d3783a0034c26afa534a4a95ad5f1178d61191/virtualenvwrapper-4.8.4.tar.gz (334 kB)
      Preparing metadata (setup.py) ... error
      error: subprocess-exited-with-error
      
      × python setup.py egg_info did not run successfully.
      │ exit code: 1
      ╰─> [35 lines of output]
          WARNING: The wheel package is not available.
          WARNING: The repository located at mirrors.cloud.aliyuncs.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host mirrors.cloud.aliyuncs.com'.
          ERROR: Could not find a version that satisfies the requirement pbr (from versions: none)
          ERROR: No matching distribution found for pbr
          Traceback (most recent call last):
            File "/usr/local/python/lib/python3.8/site-packages/setuptools/installer.py", line 128, in fetch_build_egg
              subprocess.check_call(cmd)
            File "/usr/local/python/lib/python3.8/subprocess.py", line 364, in check_call
              raise CalledProcessError(retcode, cmd)
          subprocess.CalledProcessError: Command '['/usr/local/python/bin/python3.8', '-m', 'pip', '--disable-pip-version-check', 'wheel', '--no-deps', '-w', '/tmp/tmp97rcd69z', '--quiet', '--index-url', 'http://mirrors.cloud.aliyuncs.com/pypi/simple/', 'pbr']' returned non-zero exit status 1.
          
          The above exception was the direct cause of the following exception:
          
          Traceback (most recent call last):
            File "", line 2, in <module>
            File "", line 34, in <module>
            File "/tmp/pip-install-q_8kre6_/virtualenvwrapper_564bbfde4dde45cb9571ba4b69c38f83/setup.py", line 5, in <module>
              setup(
            File "/usr/local/python/lib/python3.8/site-packages/setuptools/__init__.py", line 164, in setup
              _install_setup_requires(attrs)
            File "/usr/local/python/lib/python3.8/site-packages/setuptools/__init__.py", line 159, in _install_setup_requires
              dist.fetch_build_eggs(dist.setup_requires)
            File "/usr/local/python/lib/python3.8/site-packages/setuptools/dist.py", line 699, in fetch_build_eggs
              resolved_dists = pkg_resources.working_set.resolve(
            File "/usr/local/python/lib/python3.8/site-packages/pkg_resources/__init__.py", line 779, in resolve
              dist = best[req.key] = env.best_match(
            File "/usr/local/python/lib/python3.8/site-packages/pkg_resources/__init__.py", line 1064, in best_match
              return self.obtain(req, installer)
            File "/usr/local/python/lib/python3.8/site-packages/pkg_resources/__init__.py", line 1076, in obtain
              return installer(requirement)
            File "/usr/local/python/lib/python3.8/site-packages/setuptools/dist.py", line 758, in fetch_build_egg
              return fetch_build_egg(self, req)
            File "/usr/local/python/lib/python3.8/site-packages/setuptools/installer.py", line 130, in fetch_build_egg
              raise DistutilsError(str(e)) from e
          distutils.errors.DistutilsError: Command '['/usr/local/python/bin/python3.8', '-m', 'pip', '--disable-pip-version-check', 'wheel', '--no-deps', '-w', '/tmp/tmp97rcd69z', '--quiet', '--index-url', 'http://mirrors.cloud.aliyuncs.com/pypi/simple/', 'pbr']' returned non-zero exit status 1.
          [end of output]
      
      note: This error originates from a subprocess, and is likely not a problem with pip.
    error: metadata-generation-failed
    
    × Encountered error while generating package metadata.
    ╰─> See above for output.
    
    note: This is an issue with the package mentioned above, not pip.
    hint: See above for details.
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    执行pip install pbr 即可
    然后找一个目录放虚拟环境的文件,我这里放在home目录下,cd /home之后
    mkdir .virtualenvs
    然后在root目录下,vim .bashrc 在最下面添加如下

    # 指定virtualenvwrapper执行的python版本
    VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
    # 指定virtualenv的路径
    export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/python/bin/virtualenv
    export WORKON_HOME=$HOME/.virtualenvs
    source /usr/local/python/bin/virtualenvwrapper.sh
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    然后source ~/.bashrc 激活
    再次说明一下,/usr/loacl/python 是我的python3.8.8的安装目录
    在这里插入图片描述
    Python-3.8.8是解压后的,python是安装后的目录,目录名称是在执行以下命令的时候赋予的,当然也可以改为python3,./configure --prefix=/usr/local/python3/

    ./configure --prefix=/usr/local/python/
    make
    make install
    
    • 1
    • 2
    • 3

    如果执行 mkvirtualenv django报以下错的话

    No module named '_ssl'
    
    • 1

    请参考https://blog.csdn.net/sinat_34149445/article/details/105387170
    需要安装依赖,修改解压后的Python-3.8.8/Modules/Setup 文件,然后重新编译安装python3.8.8, 然后重新安装virtualenv,virtualenvwrapper, source ~/.bashrc

  • 相关阅读:
    1.jetson与摄像头的对接
    Spring 面试题及答案整理,最新面试题
    若依 Spring Security 短信,扫码登录
    从一个程序员的角度看东方甄选“小作文”事件
    数据库基础知识详解四:存储过程、视图、游标、SQL语句优化以及索引
    第五章 输入/输出(I/O)管理
    浏览器简介
    [PHP]empty一直返回true
    机器学习-集成学习LightGBM
    Python基础之列表,元组,字典,集合,推导式,迭代器
  • 原文地址:https://blog.csdn.net/weixin_44936542/article/details/126062646