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

另一个思路是将原来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

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

如果安装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 "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", 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.

执行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

然后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

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

No module named '_ssl'

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值