python虚拟环境完美部署教程

 更新时间:2019年08月06日 13:49:14   作者:一木浮生  
这篇文章主要介绍了python虚拟环境完美部署教程,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

一、前言

预处理

建议仔细看完本文章之后在进行操作,避免失误,本环境可以用于生产环境,有利于生产环境python之间的环境隔离,互相不会产生环境冲突;pyenvpyenv-virtualenv可以完美结合使用,具体使用情况看项目具体要求;

依赖环境要求

yum install readline readline-devel readline-static -y
yum install openssl openssl-devel openssl-static -y
yum install sqlite-devel -y
yum install bzip2-devel bzip2-libs -y
yum install libffi-devel -y

二、pyenv安装

pyenv获取

[root@tset ~]# git clone https://github.com/yyuu/pyenv.git ~/.pyenv
Cloning into '/root/.pyenv'...
remote: Enumerating objects: 17352, done.
remote: Total 17352 (delta 0), reused 0 (delta 0), pack-reused 17352
Receiving objects: 100% (17352/17352), 3.37 MiB | 24.00 KiB/s, done.
Resolving deltas: 100% (11818/11818), done.

配置pyenv系统环境

[root@tset ~]# vim ~/.bashrc
## add by liuchao at 20190806     //添加以下内容
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)" 
//保存退出
[root@tset ~]# source ~/.bashrc    //执行配置生效

三、pyenv使用

pyenv 安装python版本

个人建议:自己在python官网下载想要安装的版本;首先要在pyenv目录下创建cache文件夹,用来存放python安装包。(可以解决下载安装慢的问题)

[root@tset ~]# mkdir ~/.pyenv/cache        //创建cache文件夹,将下载的python安装包存放,
[root@tset ~]# cd ~/.pyenv/cache/
[root@tset cache]# ls               //python安装包后缀名必须是.tar.xz
Python-2.7.16.tar.xz Python-3.7.3.tar.xz
pyenv install

[root@tset cache]# cd
[root@tset ~]# pyenv install 3.7.3 -v
/tmp/python-build.20190806095432.10104 ~
/tmp/python-build.20190806095432.10104/Python-3.7.3 /tmp/python-build.20190806095432.10104 ~
Installing Python-3.7.3...
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.7... no
checking for python3... no
checking for python... python
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for a sed that does not truncate output... /usr/bin/sed
checking for --with-cxx-main=<compiler>... no
checking for g++... no
configure:

检查是否安装成功

[root@tset ~]# pyenv versions
* system (set by /root/.pyenv/version)
 3.7.3

更新数据库

[root@tset ~]# pyenv rehash

pyenv切换python版本

[root@tset ~]# pyenv global 2.7.8 // 全局
[root@tset ~]# pyenv local 2.7.8  // 当前目录下
[root@tset ~]# pyenv shell 2.7.8  // shell版本
[root@tset ~]# pyenv shell --unset // 取消当前的shell设定的版本
#创建垫片路径(为所有已安装的可执行文件创建 shims,如:~/.pyenv/versions/*/bin/*,因此,每当你增删了 Python 版本或带有可执行文件的包(如 pip)以后,都应该执行一次本命令)
[root@tset ~]# pyenv rehash 

安装与卸载

[root@tset ~]# pyenv install -l # 查看可安装的版本
[root@tset ~]# pyenv install 2.7.8 # 安装python2.7.8的版本
[root@tset ~]# pyenv rehash # 安装模块后
[root@tset ~]# pyenv pyenv uninstall 2.7.8 # 卸载

四、pyenv-virtualenv

安装pyenv-virtualenv

[root@tset ~]# git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
Cloning into '/root/.pyenv/plugins/pyenv-virtualenv'...
remote: Enumerating objects: 2064, done.
remote: Total 2064 (delta 0), reused 0 (delta 0), pack-reused 2064
Receiving objects: 100% (2064/2064), 580.31 KiB | 17.00 KiB/s, done.
Resolving deltas: 100% (1413/1413), done.

环境配置

[root@tset ~]# echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
[root@tset ~]# source ~/.bash_profile

使用方法

[root@tset ~]# pyenv virtualenv 2.7.10 env-2.7.10  //创建虚拟环境
[root@tset ~]# pyenv activate env-2.7.10  //激活虚拟环境
[root@tset ~]# pyenv deactivate  //退出虚拟环境
[root@tset ~]# pyenv uninstall env-2.7.10  //删除虚拟环境
[root@tset ~]# rm -rf ~/.pyenv/versions/env-2.7.10  //删除真实目录

总结

以上所述是小编给大家介绍的python虚拟环境完美部署教程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

  • Python3转换html到pdf的不同解决方案

    Python3转换html到pdf的不同解决方案

    今天小编就为大家分享一篇关于Python3转换html到pdf的不同解决方案,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • Python 文件与文件对象及文件打开关闭

    Python 文件与文件对象及文件打开关闭

    这篇文章主要介绍了Python 中的文件与文件对象,Python中常有的数据文件类型有文本文件、二进制文件和CSV文件,文本文件是ASCII编码,汉子存储的是机内码,更多详细内容,需要的小伙伴可以参考一下
    2022-03-03
  • Pycharm如何添加源与添加库

    Pycharm如何添加源与添加库

    这篇文章主要介绍了Pycharm如何添加源与添加库问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • Pyqt5 基本界面组件之inputDialog的使用

    Pyqt5 基本界面组件之inputDialog的使用

    今天小编就为大家分享一篇Pyqt5 基本界面组件之inputDialog的使用,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • 解决Atom安装Hydrogen无法运行python3的问题

    解决Atom安装Hydrogen无法运行python3的问题

    今天小编就为大家分享一篇解决Atom安装Hydrogen无法运行python3的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • Python isdigit()函数判断字符串是否全都是数字字符示例

    Python isdigit()函数判断字符串是否全都是数字字符示例

    这篇文章主要为大家介绍了Python判断字符串是否全都是数字字符示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • Python中最强大的错误重试库(tenacity库)

    Python中最强大的错误重试库(tenacity库)

    本文要给大家介绍的tenacity库,可能是目前Python生态中最好用的错误重试库,主要介绍tenacity的主要使用方法和特性,具有一定的参考价值,感兴趣的可以了解一下
    2022-04-04
  • python numpy实现文件存取的示例代码

    python numpy实现文件存取的示例代码

    这篇文章主要介绍了python numpy实现文件存取的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • Python实现图像去噪方式(中值去噪和均值去噪)

    Python实现图像去噪方式(中值去噪和均值去噪)

    今天小编就为大家分享一篇Python实现图像去噪方式(中值去噪和均值去噪),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • python实现发送和获取手机短信验证码

    python实现发送和获取手机短信验证码

    这篇文章主要介绍了python实现发送和获取手机短信验证码的相关资料,讲解了python如何解决接口测试获取手机验证码问题,感兴趣的小伙伴们可以参考一下
    2016-01-01

最新评论