Linux下Python开发环境部署
推荐对Python有一定了解的时候,阅读书籍
《Python Cookbook》
《Learn Python The Hard Way》
《Google’s Python Class》
《简明Python教程》
开发环境 Pyenv
1.Python版本:Python 3.6
2. Python多版本管理工具
- 管理Python解释器
- 管理Python版本
- 管理Python的虚拟环境
官网 https://github.com/pyenv/pyenv
3. 下载Pyenv (Git)
下载: $ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
环境变量设置:$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
安装插件(另一种安装方式):
https://github.com/pyenv/pyenv-installer;
$
4. 安装Pyenv
安装准备
- 时间同步,各主机用ntp服务器同步时间;
- IP设置成静态IP;
安装方式
Git安装
1 . 安装Git
$sudo yum install git -y
3. 安装Python编译依赖
$sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
$git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ vim ~/.bash_profile
$ export PATH="~/.pyenv/bin:$PATH"
$ eval “$(pyenv init -)”
$ resource ~/.bash_profile
查看是否安装成功
$ pyenv install --list
4. 创建用户python
#useradd python
5. 使用pyhon 用户登录后安装
$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
$ git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
$ vim ~/.bash_profile
$ export PATH="/home/python/.pyenv/bin:$PATH"
$ eval “$(pyenv init -)”
$ eval “$(pyenv virtualenv-int -)”
$ source ~/.bash_profile
查看是否安装成功
$ pyenv install --list
pyenv install -l
查看课安装内容,选择(在线安装) pyenv install 3.5.3 -v
离线安装
$ cd .pyenv
$ mkdir cache
$ cd cache
上传Python-3.5.3.tar.xz ;Python-3.5.3.tar.gz ;Python-3.5.3.tgz 到cache文件夹
$ pyenv install 3.5.3
pyenv global 用户下的python环境变更
$ pyenv global 3.5.3
$ pyenv versions
设置全局使用版本3.5.3(设置之后打开的python 是3.5.3)
$ pyenv global system
设置回系统默认安装版本(2.6.6)
pyenv local 文件夹对应的python环境
在文件夹下(及其子目录),pyenv local 3.5.3 开发环境设置,
pyenv virtualenv 虚拟环境(开发版本及库的隔离)
在local 设置好python版本后,不同项目兼容性问题,可能用的包的版本不一样等问题,
pyenv virtualenv 3.5.3 levy35
pyenv local levy353
(用3.5.3版本开发环境,衍生虚拟版本3.5.3版本)
管理pip包
创建.pip 隐藏文件(mkdir .pip)
进入隐藏文件(cd .pip)
新建pip.conf文件(vim pip.conf)内容如下:
[global]
index-url=https://mirrors.aliyun.com/pypi/simple/
trusted-host=mirrors.aliyun.com
安装ipython:
pip install ipython
安装jupyter:
pip install jupyter
jupyter notebook --help
设置密码:jupyter notebook password 123
使用: jupyter notebook
默认开启:http://localhost:8888
带IP使用: jupyter notebook --ip=0.0.0.0 --no-browser
监听查看IP:$ ss -tanl
当前文件夹下的pip安装及版本导出:
$ pip freeze > requirement
$ cat requirement
使用这个导出的项目所需pip 版本
$ pip install -r ../test/requirement