CentOS 7上默认安装的python版本是2.7.5,系统自带的旧版本python被系统很多其他软件环境依赖,因此不能卸载原Python,但可以再安装另一个Python版本
[root@python ~]# python -V
Python 2.7.5
#Python3可能使用的依赖
[root@python ~]# yum install gcc gcc-c++ make zlib* libffi-devel openssl-devel -y
[root@python ~]# wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz
[root@python ~]# ll Python-3.7.3.tar.xz
-rw-r--r--. 1 root root 17108364 3月 26 19:00 Python-3.7.3.tar.xz
[root@python ~]# tar xf Python-3.7.3.tar.xz -C /usr/local/src
[root@python ~]# vim /usr/local/src/Python-3.7.3/Modules/Setup.dist
# Socket module helper for socket(2)
_socket socketmodule.c timemodule.c
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
[root@python ~]# cd /usr/local/src/Python-3.7.3
[root@python Python-3.7.3]# ./configure --with-ssl --prefix=/usr/local/python3.7.3
[root@python Python-3.7.3]# make && make install
#新建软连接,指向新安装的python3.7.3
[root@python ~]# ln -s /usr/local/python3.7.3/bin/python3.7 /usr/bin/python3
#查看ssl是否安装成功
[root@python ~]# python3
>>> import ssl
>>>
在./configure过程中,如果没有加上--with-ssl参数时,默认安装的软件涉及到ssl的功能不可用,但pip3过程需要ssl模块
一般情况下,Linux系统自带的Python2没有安装pip,Python3.7.3自带pip,可以进入python3.7.3的bin目录下进行查看
[root@python bin]# pwd
/usr/local/python3.7.3/bin
[root@python bin]# ll pip*
-rwxr-xr-x. 1 root root 244 3月 27 05:18 pip
-rwxr-xr-x. 1 root root 244 3月 27 05:18 pip3
-rwxr-xr-x. 1 root root 244 3月 27 05:18 pip3.7
#创建软连接
[root@python ~]# ln -s /usr/local/python3.7.3/bin/pip3 /usr/bin/pip3
#在使用pip进行操作时,可能会提示pip版本不匹配,需要更新,执行更新命令
[root@python ~]# pip3 install --upgrade pip
Successfully installed pip-20.0.2
[root@python bin]# pip3 -V
pip 20.0.2 from /usr/local/python3.7.3/lib/python3.7/site-packages/pip (python 3.7)
wget https://download.jetbrains.com/python/pycharm-community-2019.3.5.tar.gz?_ga=2.180981863.1159140351.1594194737-340521325.1585143870
tar xvf pycharm-community-2019.3.5.tar.gz?_ga=2.180981863.1159140351.1594194737-340521325.1585143870 -C /usr/local/src/
cd /usr/local/src/pycharm-2018.3.7/bin
./pycharm.sh
ln -s /usr/local/src/pycharm-2018.3.7/bin/pycharm.sh /usr/sbin/pycharm
什么是Virtualenv
Virtualenv用于创建虚拟的Python环境,一个专属于项目的Python环境,不同的项目可以创建不同的Pthon环境(依赖项和版本等),各个环境之间完全隔离,不需要担心和其他项目的Python环境以及全局的Python环境发生冲突。
[root@python ~]# pip3 install virtualenv
Successfully installed appdirs-1.4.3 distlib-0.3.0 filelock-3.0.12 importlib-[root@python ~]# metadata-1.5.2 six-1.14.0 virtualenv-20.0.14 zipp-3.1.0
ln -s /usr/local/python3.6.7/bin/virtualenv /usr/bin/virtualenv
[root@python ~]# virtualenv --version
15.1.0
[root@python ~]# virtualenv py1 #创建名为py1的虚拟环境
New python executable in /root/py1/bin/python
Installing setuptools, pip, wheel...done.
直接使用命令virtualenv可以创建一个独立的Python运行环境,加上参数--no-site-packages
,已经安装到系统Python环境中的第三方包都不复制到独立环境中
[root@python ~]# cd /root/py1/bin/
[root@python bin]# source activate
(py1) [root@python bin]#
(py1) [root@python bin]# deactivate
[root@python bin]#
查看当前虚拟机环境目录 worken
切换虚拟环境 workon venv2
退出虚拟环境 deactivate
删除虚拟环境 rmvirtualenv venv
[root@python ~]# pip3 install virtualenvwrapper
Successfully installed pbr-5.4.4 stevedore-1.32.0 virtualenv-clone-0.5.3 virtualenvwrapper-4.8.4
[root@python ~]# find / -name virtualenvwrapper.sh
/usr/local/python3.7.3/bin/virtualenvwrapper.sh
[root@python ~]# vim .bashrc
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/python3.7.3/bin/virtualenvwrapper.sh
#测试
[root@python ~]# mkvirtualenv py1
[root@python ~]# mkvirtualenv --python=/usr/bin/python3 py2 #指定python环境为3版本
(1) ./Modules/_ssl.c:57:25: 致命错误:openssl/rsa.h:没有那个文件或目录
错误原因 尝试编译的程序使用OpenSSL,但是缺少和OpenSSL链接的文件(库和头文件)
解决办法 yum install openssl-devel
(2)pip install -i virtualenv DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7
已停止维护,用pip3 https://www.jianshu.com/p/7f1b9a203045
(3)Python3.7安装后pip pip3 pip3.7的区别
https://blog.csdn.net/u013077984/article/details/85333713
(4)执行virtualenv --version命令提示-bash: virtualenv: command not found
按照如下方式找到virtualenv并创建软链接,再执行上述查询版本信息的命令
find / -name virtualenv #全局查找virtualenv
/usr/local/python3/bin/virtualenv #找到路径
ln -s /usr/local/python3.7.3/bin/virtualenv /usr/bin/virtualenv #添加软链接
(5)安装虚拟环境/usr/bin/python: No module named virtualenvwrapper
错误原因 系统上安装了2.7和3.x两个版本的python,在安装时使用的是sudo pip3 install virtualenvwrapper,在运行的时候默认使用的是python2.x,但在python2.x中不存在对应的模块
在bashrc文件里面加入如下命令
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 #选择Python3解释器
(6)AttributeError: module ‘enum’ has no attribute 'IntFlag’
错误原因 国内源更新不及时,安装的包少东西
(7)python安装虚拟环境出现错误:Error while finding module specification for 'virtualenvwrapper.hook_loader’
/usr/local/bin/python3: Error while finding module specification for 'virtualenvwrapper.hook_loader' (ModuleNotFoundError: No module named 'virtualenvwrapper')
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 and that PATH is
set properly.
解决办法1 python3 -m pip install --user virtualenvwrapper --upgrade
解决办法2 没遇到问题(5)需要用本方法解决
因为在virtualenvwrapper.sh中有如下代码,脚本会默认使用python2环境,但是virtualenvwrapper装在了python3环境中,所以会有上面的报错
# Locate the global Python where virtualenvwrapper is installed.
if [ "$VIRTUALENVWRAPPER_PYTHON" = "" ] then
VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
fi
直接将VIRTUALENVWRAPPER_PYTHON默认值修改为/usr/bin/python3即可
# Locate the global Python where virtualenvwrapper is installed.
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
if [ "$VIRTUALENVWRAPPER_PYTHON" = "" ] then
VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
fi