[PY3]——环境配置(1)——pyenv | pip | ipython | jupyter(含安装pyenv环境shell脚本)

时间:2021-01-14 19:51:41

1.关于pyenv

(1)pyenv是一个开源的、shell脚本编写的工具;Simple Python version management

(2)为什么使用pyenv:当多个项目同时在开发与维护时,各个项目可能使用着不同的python版本,使用pyenv能更方便帮助我们管理不同的python版本环境

(3)pyenv源码及其下载安装皆可以在GitHub上找到:

https://github.com/yyuu/pyenv

https://github.com/yyuu/pyenv-installer

[PY3]——环境配置(1)——pyenv | pip | ipython | jupyter(含安装pyenv环境shell脚本)


2.安装pyenv

1. 安装git

# yum -y install git

2. 安装pyenv

# curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
WARNING: seems you still have not added 'pyenv' to the load path.
# Load pyenv automatically by adding
# the following to ~/.bash_profile:
export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

3. 其默认会安装在家目录的.pyenv下,我们可以查看到pyenv命令其实是个链接文件

# ls ~/.pyenv/
CHANGELOG.md LICENSE bin libexec shims versions
COMMANDS.md Makefile cache plugins src
CONDUCT.md README.md completions pyenv.d test
# ll ~/.pyenv/bin/
total
lrwxrwxrwx root root 16 Jan 4 10:52 pyenv -> ../libexec/pyenv

4. 配置环境变量, 在 `~/.bash_profile`里增加如下内容

# vim ~/.bash_profile
export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

5. 更新.bash_profile文件或重新登录shell使之生效

# source ~/.bash_profile

6. 这时pyenv命令就可以直接使用了。验证:

# pyenv
pyenv 1.0.6
Usage: pyenv <command> [<args>]
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 <command>' for information on a specific command.
For full documentation, see: https://github.com/yyuu/pyenv#readme

7. 安装pyenv环境的shell脚本,注意执行这个脚本需要用“source **.sh”的方式

#!/bin/bash

#title:configuration python(pyenv) environment
#author:Jelly_lyj
#date:--
#version:v0. #========================
# Install depend package
#=========================
Ins_pac()
{
echo "Is about to start download depend on the package,please wait..."
yum -y install git gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel &>/dev/null
if [ $? -eq ]
then
echo "All depend on the package installation is successful"
else
echo "There is an error, please check"
fi
} #==========================
# download & install pyenv
#==========================
Ins_pyenv()
{
echo "downloading pyenv from github,please wait..."
curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash &>/dev/null
if [ $? -eq ]
then
echo "pyenv download successful"
else
echo "pyenv download error,please check"
fi
} #===============================
# configuration path environment
#===============================
Conf_path()
{
sed -i '$i export PATH="~/.pyenv/bin:$PATH"\neval "$(pyenv init -)"\neval "$(pyenv virtualenv-init -)"' ~/.bash_profile
source ~/.bash_profile
} #==========================
# test pyenv
#==========================
Test_pyenv()
{
read -p "现在你可以输入一下pyenv命令,看看是否安装成功了:" py
case $py in
pyenv)
pyenv &>/dev/null
if [ $? -eq ]
then
echo "pyenv命令执行成功,pyenv安装成功"
echo "接下来你可以使用pyenv相关命令和选项操作管理你的python环境啦"
exit
else
echo "没有pyenv这个命令,pyenv没有安装成功"
fi
;;
*)
echo "你咋连pyenv都输错了..."
Test_pyenv
;;
esac
} #=========
# Main()
#=========
Main()
{
Ins_pac
Ins_pyenv
Conf_path
echo "Install and configuration done"
Test_pyenv
} #=========
# 程序入口
#=========
Main

Install_pyenv.sh


3. 使用pyenv安装python

1. 安装编译工具与依赖

*centos环境:
# yum -y install gcc make patch
# yum -y install gdbm-devel openssl-devel sqlite-devel readline-devel zlib-devel bzip2-devel *ubuntu环境:
# apt-get -y install gcc make patch
# apt-get -y install libgdbm-dev libssl-dev libsqlite-dev libreadline-dev zlib1g-dev libbz2-dev

2. 安装Python的命令:pyenv install

# pyenv install 3.5.
Downloading Python-3.5.2.tar.xz...
-> https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tar.xz
...... //pyenv会从官网下载安装包然后自行安装,花的时间可能稍微会长一些

3. 如果上一步进行不下去的解决方法:可在.pyenv下新建cache目录,并将自己下载的安装包放在其中

# mkdir ~/.pyenv/cache
# ls ~/.pyenv/cache/
Python-3.5.2.tar.xz
# pyenv install 3.5.
Installing Python-3.5.2...
patching file Lib/venv/scripts/posix/activate.fish
Installed Python-3.5.2 to /root/.pyenv/versions/3.5.2

4. 使用pyenv管理python环境的常用命令

1. versions命令:查看系统全部全部python版本

(1) 注意pyenv也会管理系统自带的python
(2) *号所在的行表示当前目录正在使用的python版本

# pyenv versions
* system (set by /root/.pyenv/version)
3.5.2

2. local命令:切换Python版本

(1) 这是pyenv十分便利的地方,即可以针对目录来设置python版本
(2) 应当注意:子目录默认会继承父目录的版本
(3) 我们在某个目录中使用了local命令,即会在此目录下生成一个.python-version文件,这里就记录目录的python版本

//新建一个目录py3
# mkdir /py3
//进入py3目录我们可以查看一下这个目录下的当前python版本是什么
# cd py3/
# python -V
Python 2.6.
# pyenv versions
* system (set by /.python-version)
3.5.
//使用local命令切换版本(这些命令tab都可以补全的)
# pyenv local 3.5.2
# python -V
Python 3.5.2s
# pyenv versions
system
* 3.5.2 (set by /py3/.python-version)
//看看当前目录下.python-version文件的内容
# cat .python-version
3.5.
//在py3下新建一个目录看看它的版本
# mkdir /py3/workpace1
# cd workpace1/
# pyenv versions
system
* 3.5.2 (set by /py3/.python-version)

3. global命令:切换全局默认Python版本

4. virtualenv命令
(1) 理解虚拟环境的概念

//python中有很多的第三方包/库
# ls ~/.pyenv/versions/3.5.2/lib/python3.5/site-packages/
README easy_install.py pip-8.1.1.dist-info setuptools
__pycache__ pip pkg_resources setuptools-20.10.1.dist-info
//当多个项目都使用同一个python版本时,不同的项目可能会使用不同版本的第三方包,那么这个问题该如何解决呢?
//故而引出了虚拟环境的概念
//虚拟环境的目的在于隔离,隔离第三方包,因为每个项目可能依赖相同的第三方包,但这些第三方包版本可能不一样

(2)创建虚拟环境 `pyenv virtualenv $python_version $name`

//创建一个名为virtenvname的虚拟环境
# pyenv virtualenv 3.5.2 virtenvname
Ignoring indexes: https://pypi.python.org/simple
Requirement already satisfied (use --upgrade to upgrade): setuptools in /root/.pyenv/versions/3.5.2/envs/virtenvname/lib/python3.5/site-packages
Requirement already satisfied (use --upgrade to upgrade): pip in /root/.pyenv/versions/3.5.2/envs/virtenvname/lib/python3.5/site-packages
//这是我们查看versions目录可以看到这个virtenvname"版本",可以看到这是个链接文件
# ll ~/.pyenv/versions/
total 4
drwxr-xr-x 7 root root 4096 Jan 5 23:27 3.5.2
lrwxrwxrwx 1 root root 44 Jan 5 23:27 virtenvname -> /root/.pyenv/versions/3.5.2/envs/virtenvname
//查看这个链接的目录下的内容,我们可以看到virtenvname的第三方包
# ls ~/.pyenv/versions/3.5.2/envs/virtenvname/lib/python3.5/site-packages/
__pycache__ pkg_resources
easy_install.py setuptools
pip setuptools-20.10.1.dist-info
pip-8.1..dist-info
//虚拟环境就当做“一个版本”按平常的方法来使用即可
# pyenv versions
system
* 3.5.2 (set by /py3/.python-version)
3.5.2/envs/virtenvname
virtenvname

5. uninstall命令:卸载某个版本, 包括虚拟环境

# pyenv uninstall virtenvname
pyenv-virtualenv: remove /root/.pyenv/versions/3.5.2/envs/virtenvname? y
# pyenv versions
system
* 3.5.2 (set by /py3/.python-version)

pip工具

pip是一个Python包的管理工具

//更改pip源的方法(aliyun的快得飞起)
# mkdir /root/.pip
# vim /root/.pip/pip.conf
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
trusted-host=mirrors.aliyun.com

ipython | jupyter工具

ipython是一个python交互shell的增强工具
jupyter是可以在web端使用python
(注:我的jupyter部署在云服务器上、系统为CentOS、浏览器为firefox)

//创建虚拟环境magedu1
[root@TXY_LYJ py3]# pyenv virtualenv 3.5.2 magedu1
Ignoring indexes: https://pypi.python.org/simple
Requirement already satisfied (use --upgrade to upgrade): setuptools in /root/.yenv/versions/3.5.2/envs/magedu1/lib/python3.5/site-packages
Requirement already satisfied (use --upgrade to upgrade): pip in /root/.pyenv/vrsions/3.5.2/envs/magedu1/lib/python3.5/site-packages
[root@TXY_LYJ py3]# pyenv local magedu1
//使用pip安装ipython和jupyter
(magedu1) [root@TXY_LYJ py3]# pip install ipython
(magedu1) [root@TXY_LYJ py3]# pip install jupyter
//打开ipython,为notebook远程生成一个密码
(magedu1) [root@TXY_LYJ py3]#ipython
In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'sha1:033c3e7745a6:......'
//修改jupyter的配置文件
# cat /root/.jupyter/jupyter_notebook_config.py|egrep -v '^#|^$'
c.NotebookApp.ip = '*'
c.NotebookApp.password = u'sha1:033c3e7745a6:......'

[PY3]——环境配置(1)——pyenv | pip | ipython | jupyter(含安装pyenv环境shell脚本)

[PY3]——环境配置(1)——pyenv | pip | ipython | jupyter(含安装pyenv环境shell脚本)