日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

centos 創(chuàng)建python虛擬環(huán)境遇到的問題及解決方法

作者:bianlidou 更新時間: 2022-07-30 編程語言

centos自帶了python3.6.8的python, 然后我想安裝更高版本的python,比如python3.8.8, 可以自行搜索一下安裝方法,有很多(如果安裝后的python出現(xiàn)No module named ‘_ssl’ 這個報(bào)錯,請拉到最后看)
值得注意的是,安裝好之后,由于python3,pip3軟鏈接指向的是python3.6的
在這里插入圖片描述
這里有2個思路,一個是設(shè)置軟鏈接python,pip對應(yīng)python3.8.8

# /usr/local/python  這個是我python3.8.8的安裝目錄,這樣設(shè)置之后,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種方式?jīng)]有優(yōu)劣之分,如果安裝很多版本的python的話,比如python3.6, python3.7,python3.8等等, 那么建議每個python版本創(chuàng)建不同的軟鏈接,比如python3.6指向的就是python3.6, python3.7指向的就是python3.7…

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

pip install virtualenv
pip install virtualenvwrapper

如果安裝virtualenvwrapper報(bào)錯如下

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.

執(zhí)行pip install pbr 即可
然后找一個目錄放虛擬環(huán)境的文件,我這里放在home目錄下,cd /home之后
mkdir .virtualenvs
然后在root目錄下,vim .bashrc 在最下面添加如下

# 指定virtualenvwrapper執(zhí)行的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是安裝后的目錄

如果執(zhí)行 mkvirtualenv django報(bào)以下錯的話

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

原文鏈接:https://blog.csdn.net/weixin_44936542/article/details/126062646

欄目分類
最近更新