发布时间:2022-12-20 12:30
[root@iZuf62qojdpdfmeohedn2jZ ~]# python -V
Python 2.7.5
[root@iZuf62qojdpdfmeohedn2jZ ~]# pip -V
pip 19.1.1 from /usr/lib/python2.7/site-packages/pip (python 2.7)
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
[root@iZuf62qojdpdfmeohedn2jZ ~]# yum install -y wget
接下来下载python3.6.5
[root@iZuf62qojdpdfmeohedn2jZ ~]# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
[root@iZuf62qojdpdfmeohedn2jZ ~]# tar -zxvf Python-3.6.5.tgz
[root@iZuf62qojdpdfmeohedn2jZ ~]# cd Python-3.6.5/
[root@iZuf62qojdpdfmeohedn2jZ Python-3.6.5]# ./configure --prefix=/usr/local/python3
如果遇到:configure: error: no acceptable C compiler found in $PATH
yum install gcc
由于之前踩过坑,在安装的过程中可能报错,大致意思就是:缺少了zlib的解压缩类库。所以我们先下载zlib解压缩类库
[root@iZuf62qojdpdfmeohedn2jZ Python-3.6.5]# yum install -y zlib*
然后再开始安装
[root@iZuf62qojdpdfmeohedn2jZ Python-3.6.5]# make && make install
出现以下提示说明安装成功:
if test "x" != "x" ; then \
rm -f /usr/local/python3/bin/python3-32; \
(cd /usr/local/python3/bin; ln -s python3.6-32 python3-32) \
fi
rm -f /usr/local/python3/share/man/man1/python3.1
(cd /usr/local/python3/share/man/man1; ln -s python3.6.1 python3.1)
if test "xupgrade" != "xno" ; then \
case upgrade in \
upgrade) ensurepip="--upgrade" ;; \
install|*) ensurepip="" ;; \
esac; \
./python -E -m ensurepip \
$ensurepip --root=/ ; \
fi
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-9.0.3 setuptools-39.0.1
[root@iZuf62qojdpdfmeohedn2jZ Python-3.6.5]#
[root@iZuf62qojdpdfmeohedn2jZ /]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@iZuf62qojdpdfmeohedn2jZ /]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
[root@iZuf62qojdpdfmeohedn2jZ /]# python3 -V
Python 3.6.5
[root@iZuf62qojdpdfmeohedn2jZ /]# pip3 -V
pip 9.0.3 from /usr/local/python3/lib/python3.6/site-packages (python 3.6)
[root@iZuf62qojdpdfmeohedn2jZ /]# python3
Python 3.6.5 (default, Nov 13 2020, 10:51:33)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
[root@iZuf62qojdpdfmeohedn2jZ /]#
==============================分割线==============================
[root@iZuf62qojdpdfmeohedn2jZ /]# mkdir envs
[root@iZuf62qojdpdfmeohedn2jZ /]# ls
bin dev etc lib lost+found mnt proc run srv tmp var
boot envs home lib64 media opt root sbin sys usr
[root@iZuf62qojdpdfmeohedn2jZ /]#
[root@iZuf62qojdpdfmeohedn2jZ /]# pip3 install virtualenv
[root@iZuf62qojdpdfmeohedn2jZ /]# pip3 install virtualenvwrapper
[root@iZuf62qojdpdfmeohedn2jZ /]# find / -name 'virtualenvwrapper.sh'
/usr/local/python3/bin/virtualenvwrapper.sh
[root@iZuf62qojdpdfmeohedn2jZ /]# vim usr/local/python3/bin/virtualenvwrapper.sh
现修改python为python3
if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
then
# 修改之前的
# VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
# 修改之后的,python后边加个3
VIRTUALENVWRAPPER_PYTHON="$(command \which python3)"
fi
打开bashrc文件:
[root@iZuf62qojdpdfmeohedn2jZ /]# vim ~/.bashrc
并在末尾添加以下几行:
export WORKON_HOME=/envs
export VIRTUELANVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/python3/bin/virtualenvwrapper.sh
执行:source ~/.bashrc
[root@iZuf62qojdpdfmeohedn2jZ /]# source ~/.bashrc
virtualenvwrapper.user_scripts creating /envs/premkproject
virtualenvwrapper.user_scripts creating /envs/postmkproject
virtualenvwrapper.user_scripts creating /envs/initialize
virtualenvwrapper.user_scripts creating /envs/premkvirtualenv
virtualenvwrapper.user_scripts creating /envs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /envs/prermvirtualenv
virtualenvwrapper.user_scripts creating /envs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /envs/predeactivate
virtualenvwrapper.user_scripts creating /envs/postdeactivate
virtualenvwrapper.user_scripts creating /envs/preactivate
virtualenvwrapper.user_scripts creating /envs/postactivate
virtualenvwrapper.user_scripts creating /envs/get_env_details
[root@iZuf62qojdpdfmeohedn2jZ /]#
此时输入workon,什么都不输出,说明没有创建虚拟环境
[root@iZuf62qojdpdfmeohedn2jZ /]# workon
[root@iZuf62qojdpdfmeohedn2jZ /]
创建虚拟环境,报错如下:
[root@iZuf62qojdpdfmeohedn2jZ /]# mkvirtualenv py3scrapy
which: no virtualenv in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
ERROR: virtualenvwrapper could not find virtualenv in your path
[root@iZuf62qojdpdfmeohedn2jZ /]# mkvirtualenv -p python3 py3scrapy
which: no virtualenv in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)
ERROR: virtualenvwrapper could not find virtualenv in your path
[root@iZuf62qojdpdfmeohedn2jZ /]
解决办法:
[root@iZuf62qojdpdfmeohedn2jZ /]# cd /usr/bin/
[root@iZuf62qojdpdfmeohedn2jZ bin]# ls -al |grep virtualenv
[root@iZuf62qojdpdfmeohedn2jZ bin]# cd /usr/local/python3/bin/
[root@iZuf62qojdpdfmeohedn2jZ bin]# ln virtualenv /usr/bin/virtualenv
[root@iZuf62qojdpdfmeohedn2jZ /]# mkvirtualenv test
created virtual environment CPython3.6.5.final.0-64 in 191ms
creator CPython3Posix(dest=/envs/test, clear=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1
activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
virtualenvwrapper.user_scripts creating /envs/test/bin/predeactivate
virtualenvwrapper.user_scripts creating /envs/test/bin/postdeactivate
virtualenvwrapper.user_scripts creating /envs/test/bin/preactivate
virtualenvwrapper.user_scripts creating /envs/test/bin/postactivate
virtualenvwrapper.user_scripts creating /envs/test/bin/get_env_details
(test) [root@iZuf62qojdpdfmeohedn2jZ /]# wrokon
-bash: wrokon: command not found
(test) [root@iZuf62qojdpdfmeohedn2jZ /]# workon
py3scrapy
test
(test) [root@iZuf62qojdpdfmeohedn2jZ /]#
(test) [root@iZuf62qojdpdfmeohedn2jZ /]# deactivate
[root@iZuf62qojdpdfmeohedn2jZ /]# rmvirtualenv test
Removing test...
[root@iZuf62qojdpdfmeohedn2jZ /]# workon
py3scrapy
[root@iZuf62qojdpdfmeohedn2jZ /]#