1. 程式人生 > >Python環境搭建

Python環境搭建

python

1.1.1 1python多版本管理工具pyenv安裝

1.作用

管理python解釋器、python版本、python的虛擬環境

軟件下載地址:https://github.com/pyenv/pyenv

2.pyenv軟件包安裝

a、配置yum源(配置文件內容如下)

vi CentOS6-Base-163.repo

[base]

name=CentOS-$releasever - Base - 163.com

baseurl=http://mirrors.163.com/centos/6/os/$basearch/

enable=1

gpgcheck=0

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#released updates

[updates]

name=CentOS-$releasever - Updates - 163.com

baseurl=http://mirrors.163.com/centos/6/updates/$basearch/

gpgcheck=0

enable=1

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#additional packages that may be useful

[extras]

name=CentOS-$releasever - Extras - 163.com

baseurl=http://mirrors.163.com/centos/6/extras/$basearch/

gpgcheck=0

enable=1

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#additional packages that extend functionality of existing packages

[centosplus]

name=CentOS-$releasever - Plus - 163.com

baseurl=http://mirrors.163.com/centos/6/centosplus/$basearch/

gpgcheck=0

enabled=1

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

#contrib - packages by Centos Users

[contrib]

name=CentOS-$releasever - Contrib - 163.com

baseurl=http://mirrors.163.com/centos/6/contrib/$basearch/

gpgcheck=0

enabled=1

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

b、安裝git軟件和依賴包

yum install git -y

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

c、創建用戶

useradd python

passwd python

d、安裝pyenv軟件

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

e、配置環境變量

vim ~/.bash_profile

export PATH="/home/python/.pyenv/bin:$PATH"

eval "$(pyenv init -)"

eval "$(pyenv virtualenv-init -)"

1.1.2 3.使用pyenv軟件安裝和管理python

a、列出所有可用版本

pyenv versions

b、安裝指定python版本

pyenv install --list

pyenv install 3.5.3 –v

/home/python/.pyenv

/home/python/.pyenv/cache---需手工建立

c、創建一個目錄讓local捆綁在一個目錄下

pyenv versions------利用pyenv工具查看當前已安裝python版本

* system (set by /home/python/.pyenv/version)

3.5.3

3.6.1

3.6.3

mkdir -p magedu/projects

cd magedu/projects/

pyenv local 3.5.3----------------#讓python版本捆綁在一個目錄下

python -V

Python 3.5.3

cd-------#你看如果在其它目錄,python版本就是系統默認的2.6,local登錄基本做到隔離

python -V

Python 2.6.6

pyenv virtualenv用法

pyenv virtualenv 3.6.3 mage363

pyenv versions

system

3.5.3

3.6.1

* 3.6.3 (set by /home/python/magedu/projects/web/.python-version)

3.6.3/envs/mage363

* mage363 (set by /home/python/magedu/projects/web/.python-version)

pyenv local mage363

(mage363) [python@python web]$

1.2 開發工具IPython Notebook的安裝

1.2.1 1、配置pip國內源

mkdir /root/.pip

vim /root/.pip/pip.conf

[global]

index-url = http://mirrors.aliyun.com/pypi/simple/

[install]

trusted-host=mirrors.aliyun.com

pip -V

pip 9.0.1 from /home/python/.pyenv/versions/3.5.3/lib/python3.5/site-packages (python 3.5)

技術分享圖片

1.2.2 2 、安裝jupyter

pip install IPython

pip install urllib3 –安裝IPython Notebook的依賴

pip install jupyter –安裝IPython Notebook

1.2.3 3、啟動和修改密碼

jupyter notebook --ip=0.0.0.0 --port 8000 --allow-root --no-browser

http://192.168.12.128:8080/

jupyter notebook --generate-config-----生成配置文件

生產密碼和修改配置文件

Preparing a hashed password

Ipython

You can prepare a hashed password manually, using the function notebook.auth.security.passwd():

In [1]: from notebook.auth import passwd

In [2]: passwd()

Enter password:

Verify password:

Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'

vi jupyter_notebook_config.py(添加密碼行)

c.NotebookApp.password = u'sha1:a025bae1e107:247f2a22dbd90d2bbeb0aa4e4af6cb3268dcee8f'

重啟驗證

jupyter notebook --ip=0.0.0.0 --port 8000 --allow-root --no-browser

1.3 3利用pip工具快速在不同項目導入導出包

1.3.1 1、接下來在這個環境,把安裝的所有包導出:

(magedu353) [python@python9 cmdb]$ pip freeze >/tmp/packs.txt

(magedu353) [python@python9 cmdb]$ cat /tmp/packs.txt

1.3.2 2、#我們去web環境目錄

pip install -r /tmp/packs.txt

#這樣就相當於在部署跟cmdb上的環境包了(導入包)

1.4 4win開發工具PyCharm安裝

1、 jupyter安裝

安裝

pip install jupyter

啟動

jupyter notebook


Python環境搭建