您的位置:首页 > 运维架构 > Linux

Linux 下安装 Python 3.4

2015-08-24 14:06 387 查看
Linux 下安装 Python 3.4
系统环境:
·       Ubuntu  x64

下载 Python 3.4 源码包

# wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz
安装 Python 3.4

# tar xf Python-3.4.3.tgz -C /usr/local/src/
# cd /usr/local/src/Python-3.4.3/
# ./configure --prefix=/usr/local/python3.4
# make -j4 && make install

添加 python3.4 命令到环境变量
添加 python3.4 到环境变量,编辑 ~/.bash_profile,将:

PATH=$PATH:$HOME/bin
改为:
PATH=$PATH:$HOME/bin:/usr/local/python3.4/bin

使 python3.4 环境变量生效:

# . ~/.bash_profile

安装 pip 管理扩展包
通常我们安装 Python 扩展包有 3 种方式:

1.
使用python 自带的标准安装工具
`distutils` 方式安装:下载扩展包源码,解压并进入源码目录,执行
`pythonsetup.py install` 进行安装
2.
使用 `easy_install package-name`
方式安装(自动下载扩展包并安装)
3.
使用 `pip install package-name`
方式安装(自动下载扩展包并安装)

而 pip 不仅具有安装扩展包的功能,还具有管理扩展包的功能:安装、卸载、更新、查看已安装的扩展包列表等等。推荐使用。
安装 pip:从 pip v1.5.1 开始,安装 pip 不必先安装 setuptools,如果没有安装 setuptools 的话,它将自动帮你安装 
事实上 easy_install 命令在 setuptools 包里,所以安装完 pip 连 easy_install 命令也有了。

# wget https://pypi.python.org/packages/source/p/pip/pip-1.5.6.tar.gz # tar xf pip-1.5.6.tar.gz -C /usr/local/src/
# cd /usr/local/src/pip-1.5.6/
# python3.4 setup.py install

现在使用 pip 查看一下已经安装的扩展包列表:

# pip3.4 list
pip (1.5.6)
setuptools (2.1)

使用 pip3.4 --help 查看更多 pip 的可用参数信息:

# pip3.4 --help
 
Usage:
  pip <command>
[options]
 
Commands:
  install                     Installpackages.
  uninstall                   Uninstallpackages.
  freeze                      Outputinstalled packages
in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  search                      Search
PyPI for packages.
  wheel                       Buildwheels
from your requirements.
  zip                         DEPRECATED.
Zip individual packages.
  unzip                       DEPRECATED.
Unzip individual packages.
  bundle                      DEPRECATED.
Create pybundles.
  help                        Show help
for commands.
 
General
Options:
  -h,
--help                 
Show help.
  -v,
--verbose              
Give moreoutput.
Option is additive,
and can be used up to
3 times.
  -V,
--version              
Showversion and
exit.
  -q,
--quiet                
Give lessoutput.
  --log-file
<path>          Path to a verbose non-appendinglog, that only logs failures.
This log is active
by default at
/root/.pip/pip.log.
  --log
<path>                Path to a verbose appending log.
This log is inactive
by default.
  --proxy
<proxy>             Specify a proxy
in the form [user:passwd@]proxy.server:port.
  --timeout<sec>            
Set the socket timeout
(default 15 seconds).
  --exists-action
<action>    Default action
when a path already exists:
(s)witch,
(i)gnore,
(w)ipe,
(b)ackup.
  --cert
<path>               Path to alternate CA bundle.

注意:以上命令中我们都用了 python3.4 和 pip3.4 使得我们的扩展包安装在 python 3.4版本下,
如果是安装在其他 python 版本下,请使用对应版本的 python
命令.

补充:


configure/make/make install简要说明

 

Linux里编译安装软件会用到诸如configure/make/make
install的命令,这些都是典型的使用GNU的autoconf和automake产生的程序的安装步骤。

./configure是用来检测你的安装平台的目标特征的。比如它会检测你是不是有CC或GCC,并不是需要CC或GCC,它是个shell脚本。

make是用来编译的,它从Makefile中读取指令,然后编译。

make install是用来安装的,它也从Makefile中读取指令,安装到指定的位置。

AUTOMAKE和AUTOCONF是非常有用的用来发布C程序的东西。如果你也写程序想使用AUTOMAKE和AUTOCONF,可以参考CNGNU.ORG上的相关文章。

1、configure,这一步一般用来生成 Makefile,为下一步的编译做准备,你可以通过在 configure 后加上参数来对安装进行控制,比如

./configure –prefix=/usr

上面的意思是将该软件安装在 /usr 下面,执行文件就会安装在 /usr/bin (而不是默认的 /usr/local/bin),资源文件就会安装在 /usr/share(而不是默认的/usr/local/share)。同时一些软件的配置文件你可以通过指定 –sys-config= 参数进行设定。有一些软件还可以加上 –with、–enable、–without、–disable 等等参数对编译加以控制,你可以通过允许 ./configure –help 察看详细的说明帮助。

2、make ,这一步就是编译,大多数的源代码包都经过这一步进行编译(当然有些perl或python编写的软件需要调用perl或python来进行编译)。如果 在 make 过程中出现 error ,你就要记下错误代码(注意不仅仅是最后一行),然后你可以向开发者提交 bugreport(一般在 INSTALL 里有提交地址),或者你的系统少了一些依赖库等,这些需要自己仔细研究错误代码。

3、make insatll,这条命令来进行安装(当然有些软件需要先运行 make check 或 make test 来进行一些测试),这一步一般需要你有 root 权限(因为要向系统写入文件)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python linux