centos7安装python3,并配置虚拟环境(超级详细教程) |
您所在的位置:网站首页 › 怎样退出python编译环境 › centos7安装python3,并配置虚拟环境(超级详细教程) |
安装python3
1. centos7自带python2环境
[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)
2. 安装可能用到的依赖
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
3. 利用wget命令下载python3.6.5,没有wget命令的话,需要先下载
[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 4. 下载的是一个压缩包,解压 [root@iZuf62qojdpdfmeohedn2jZ ~]# tar -zxvf Python-3.6.5.tgz 5. 进入该目录,进行编译,指定编译后生成文件的位置 [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 6. 接下来开始安装由于之前踩过坑,在安装的过程中可能报错,大致意思就是:缺少了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]# 7. 设置软连接 [root@iZuf62qojdpdfmeohedn2jZ /]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3 [root@iZuf62qojdpdfmeohedn2jZ /]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 8. 测试 [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 /]# 9. 至此,安装成功!!==============================分割线============================== 配置虚拟环境 1. 先新建一个文件夹,用来作为虚拟环境的目录 [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 /]# 2.下载virtualanv和virtualenvwrapper [root@iZuf62qojdpdfmeohedn2jZ /]# pip3 install virtualenv [root@iZuf62qojdpdfmeohedn2jZ /]# pip3 install virtualenvwrapper 3. 配置环境变量 [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 /]# 4. 解决报错此时输入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 5. 创建虚拟环境 [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 /]# 6. 退出、删除、列出虚拟环境 (test) [root@iZuf62qojdpdfmeohedn2jZ /]# deactivate [root@iZuf62qojdpdfmeohedn2jZ /]# rmvirtualenv test Removing test... [root@iZuf62qojdpdfmeohedn2jZ /]# workon py3scrapy [root@iZuf62qojdpdfmeohedn2jZ /]# 7. 大功告成!!! |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |