1.4Python环境准备

操作系统

Linux

osx

推荐使用OSX

所有代码可以放在Linux上运行

IDE & EDITOR

PyCharm

编辑器

Atom 下载后可以直接使用

vim

emacs

推荐使用PyCharm

Python版本 3.4.2

异步部分介绍3.5的'async'和'await'关键字

安装Python

使用pyenv来管理Python环境

安装pyenv, pyenv是使用bash写的

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

来下载安装

一般会先下载下来,而后检查里面是否有恶意代码,没有则可以执行,这里安装依赖git

mac安装

brew install git

依赖包问题

1.依赖包的安装

yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel

2. configure: error: no acceptable C compiler found in $PATH

yum install gcc

3. Build failed: "ERROR: The Python zlib extension was not compiled. Missing the zlib?"

CFLAGS="-I$(xcrun --show-sdk-path)/usr/include" pyenv install -v 2.7.6

4. /root/.pyenv/plugins/python-build/bin/python-build: line 1326: patch: command not found

yum install patch

pyenv默认是安装在家目录的.pyenv下

如果要安装在其他目录,可以在安装前使用

export PYENV_ROOT=/PATH 则安装时,会安装到指定的PATH下面

安装好以后,有一些环境变量需要配置

vim ~/.bash_profile

添加下面的内容

#pyenv

#export PYENV_ROOT=/usr/local/opt/pyenv

if which pyenv > /dev/null; then

eval "$(pyenv init -)";

fi

把.bash_profile执行一下

把pyenv命令加入到PATH中

# cat /etc/profile.d/pyenv.sh

export PATH=/root/.pyenv/bin:${PATH}

# . /etc/profile.d/pyenv.sh

pyenv使用

#pyenv install 3.4.2 安装新的Python环境

# pyenv install --list 查看可以安装哪些程序

pyenv所有可用的命令

# pyenv commands

commands

completions

exec

global

help

hooks

init

install

local

prefix

rehash

root

shell

shims

uninstall

version

--version

version-file

version-file-read

version-file-write

version-name

version-origin

versions

whence

which

# pyenv versions

system 系统自带的Python

* 3.4.2 (set by /root/.python-version)

#pyenv local 3.4.2 当前使用及其所有创建的子目录下使用3.4.2的Python

在当前目录下会生成一个.python-version的文件

# cat .python-version

3.4.2

此时pip已经安装好了

# pip -V

pip 1.5.6 from /root/.pyenv/versions/3.4.2/lib/python3.4/site-packages (python 3.4)

#pyenv global 3.4.2 指定全局Python使用3.4.2的版本,任何一个目录没有使用local的话都会使用3.4.2的版本,这对系统上默认安装的软件会有影响的,尽量不要使用系统自带的Python环境,因为Python版本会跟着系统一起升级,升级过后,如果你的项目不支持新版本的Python则会出错。

#pyenv virtualenv virtualenv是一个虚拟环境,Python它是基于set的。virtualenv是用来创建虚拟环境的

# pyenv versions

system

* 3.4.2 (set by /root/.python-version)

# pyenv virtualenv 3.4.2 magedu_test

Ignoring indexes: https://pypi.python.org/simple/

Requirement already satisfied (use --upgrade to upgrade): setuptools in /root/.pyenv/versions/3.4.2/envs/magedu_test/lib/python3.4/site-packages

Requirement already satisfied (use --upgrade to upgrade): pip in /root/.pyenv/versions/3.4.2/envs/magedu_test/lib/python3.4/site-packages

Cleaning up...

# pyenv versions

system

* 3.4.2 (set by /root/.python-version)

3.4.2/envs/magedu_test

magedu_test (pyenv比较久的版本是这种形式的)

# pyenv local magedu_test

# pyenv versions

system

3.4.2

3.4.2/envs/magedu_test

* magedu_test (set by /root/.python-version)

激活虚拟环境,pip中的环境也改变了

pyenv activate <pyenv_virtual>

# cat .python-version

magedu_test

# pyenv activate magedu_test0000

pyenv-virtualenv: deactivate

pyenv-virtualenv: activate magedu_test0000

pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.

(magedu_test0000) [root@iptables2 ~]# pip -V pip环境也一起装了,这个环境装了,就不用激活activate

pip 1.5.6 from /root/.pyenv/versions/3.4.2/envs/magedu_test0000/lib/python3.4/site-packages (python 3.4)

(magedu_test0000) [root@iptables2 ~]# cat .python-version

magedu_test0000

在虚拟环境中也要安装ipython

$ cat .bashrc

#export PYENV_VIRTUALENV_DISABLE_PROMPT=1

时会用提示(magedu_test0000), 此时己进入你指定的虚拟环境

退出和切换虚拟环境只需要,pyenv activate version就可以了,exit也可以不过会退出登录

安装ipython

(magedu_test0000) [root@iptables2 ~]# pip install ipython

安装jupyter,jupyter以前是ipython的一部分,在ipython发展到4时,就分离出来了

# pip install jupyter

ipython 4之前有一个#ipython notebook 命令

在ipython 4之后是用

#jupyter notebook --ip=0.0.0.0

# jupyter notebook

[I 16:31:01.498 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret

[I 16:31:01.585 NotebookApp] Serving notebooks from local directory: /root

[I 16:31:01.585 NotebookApp] 0 active kernels

[I 16:31:01.585 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/

[I 16:31:01.585 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

[W 16:31:01.586 NotebookApp] No web browser found: could not locate runnable browser.

^C[I 16:33:22.198 NotebookApp] interrupted

Serving notebooks from local directory: /root

0 active kernels

The Jupyter Notebook is running at: http://localhost:8888/

Shutdown this notebook server (y/[n])? y

[C 16:33:25.108 NotebookApp] Shutdown confirmed

[I 16:33:25.109 NotebookApp] Shutting down kernels

# jupyter notebook --ip=0.0.0.0

[I 16:33:39.073 NotebookApp] Serving notebooks from local directory: /root

[I 16:33:39.073 NotebookApp] 0 active kernels

[I 16:33:39.073 NotebookApp] The Jupyter Notebook is running at: http://0.0.0.0:8888/

[I 16:33:39.073 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

[W 16:33:39.074 NotebookApp] No web browser found: could not locate runnable browser.

[I 16:34:14.901 NotebookApp] 302 GET / (192.168.16.213) 1.60ms

通过浏览器访问 serverip:8888 是一个web版的ipython

clip_image002

clip_image004

点untitled可以命名标题

clip_image006

clip_image008

Markdown是用来标记注释的

按Ctrl + enter是执行

Ctrl + Alt 是跳到新的行

查看快捷键

clip_image010

在ipython 4之前,ipython和jupyter是同一个项目,在ipython 4之后分开,因为jupyter现在是一个语言无关的,现在jupyter的能支持至少50种语言的web非交互式环境。是在远程来执行的。

转载于:https://my.oschina.net/u/1762991/blog/666407

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值