2. Python环境安装

Centos 下环境安装

我们通过pyenv来管理python环境,更好的帮助开发者避免在环境上出现各种各样的问题

准备工作

安装之前,确保已经安装了git

yum install git -y

安装其他依赖

yum -y install gcc make patch gdbm-devel openssl-devel sqlite-devel readline-
devel zlib-devel bzip2-devel

创建用户

useradd python

使用python登陆,然后开始下面的安装。

安装

一共介绍两种安装方式方式,请自行选择

方法一: 官方推荐的网络自动安装方式

Github地址

curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash

上面的那个连接本身是一个bash脚本,上面命令是通过curl这个命令行的工具下载这个脚本,通过管道交给bash来运行的。

然后在python用户的 ~/.bash_profile 中追加下面环境变量:

export PYENV_ROOT=/home/python/.pyenv
export PATH="/home/python/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

最后加载一下.bash_profile文件即可

source ~/.bash_profile

最后测试:

[python@node ~]$ pyenv
pyenv 1.2.7
Usage: pyenv []
Some useful pyenv commands are:
    commands     List all available pyenv commands
    local     Set or show the local application-specific Python version
    global     Set or show the global Python version
    shell     Set or show the shell-specific Python version
    install     Install a Python version using python-build
    uninstall    Uninstall a specific Python version
    rehash     Rehash pyenv shims (run this after installing executables)
    version     Show the current Python version and its origin
    versions     List all Python versions available to pyenv
    which     Display the full path to an executable
    whence     List all Python versions that contain the given executable
See `pyenv help ' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme

方法二:GitHub CLone 安装

其实这种方法,就是需要手动的在GitHub上Clone下来pyenv项目,然后手动配置环境变量

两种获取方式:

a. 直接使用git连接Clone到项目到本地

git clone https://github.com/pyenv/pyenv.git ~/.pyenv
git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv

b. 离线安装方式,分别去官网下载pyenv和virtualenv插件

pyenv 下载地址 https://github.com/pyenv/pyenv
virtualenv 插件 https://github.com/pyenv/pyenv-virtualenv

分别下载下俩后,放到python家目录下,然后解压

再添加环境变量.bash_profile

export PYENV_ROOT=/home/python/.pyenv
export PATH="/home/python/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

最后加载一下.bash_profile文件即可

source ~/.bash_profile

pyenv常用的命令:

pyenv install --list # 列出可安装版本
pyenv install # 安装对应版本
pyenv install -v # 安装对应版本,若发生错误,可以显示详细的错误信息
pyenv versions # 显示当前使用的python版本
pyenv which python # 显示当前python安装路径
pyenv global # 设置默认Python版本
pyenv local # 当前路径创建一个.python-version, 以后进入这个目录自动切换为该版本
pyenv shell # 当前shell的session中启用某版本,优先级高于global 及 local

使用virtualenv

创建一个虚拟环境

pyenv virtualenv 3.5.3 cmdb-3.5.3

创建一个项目目录

mkdir ~/cmdb

然后进入后项目目录,执行

pyenv local cmdb-3.5.3

虚拟环境常用其他命令

pyenv activate cmdb-3.5.3 # 激活 cmdb-3.5.3 这个虚拟环境
pyenv deactivate # 停用当前的虚拟环境

自动激活
使用pyenv local 虚拟环境名
会把虚拟环境名写入当前目录的.python-version文件中
关闭自动激活 -> pyenv deactivate
启动自动激活 -> pyenv activate cmdb-3.5.3
pyenv local cmdb-3.5.3
pyenv uninstall cmdb-3.5.3 # 删除 cmdb-3.5.3 这个虚拟环境

pip包管理

pip工具和centos系统下的yum工具使用方法大同小异
pip install xxx yyy
pip list
pip search keyword 或者 pypi
pip help install

配置国内pip源

pip默认在安装包的时候,是从国外的,安装的时候很慢,经常会出现Timeout,所有我们把pip源修改为国内的,增加了安装速度

vim ~/.pip/pip.conf
[global]
index-url=http://mirrors.aliyun.com/pypi/simple
trusted-host=mirrors.aliyun.com

使用pip常用命令

pip install redis ipython
pip install jupyter
pip -V
pip list
pip freeze > requirement
pip install -r requirement

安装ipython和jupyter

Ipython

  • 增强的Python Shell,自动补全、自动缩进、支持shell,增加了很多函数

Jupyter

  • jupyter notebook password
  • jupyter notebook --ip=0.0.0.0 --port=8888
  • 从Ipython中独立出来
  • 独立的交互式笔记本,后台使用Ipython
  • 快捷键:shift + EnterCtrl + Enter 、 dd 、 m
原文地址:https://www.cnblogs.com/winstom/p/9482904.html