ubuntu python 2.6 Ubuntu软件中心

各位老铁们好,相信很多人对ubuntu python 2.6都不是特别的了解,因此呢,今天就来为大家分享下关于ubuntu python 2.6以及Ubuntu软件中心的问题知识,还望可以帮助大家,解决大家的一些困惑,下面一起来看看吧!

ubuntu16.04下切换python版本

对于ubuntu 16.04,由于本身是自带python,这样就减少了在windows下的下载和环境变量配置,非常不错。但是他本身是自带两个版本的python,2.X和3.X,两个版本,两个版本默认的是使用2.X,毕竟3.X是一个趋势,今后我这边应该也会主要使用3.X版本,所以这里记录一下如果在版本间切换以及如何把python版本切换到3.X下的方法。

注意:要以root身份操作

一:确认本机下的python默认版本。调出终端,输入python即可查看默认的版本:

二:如果想要查看本机自带的python的另一个版本为多少,需要命令python3即可:

(博主电脑里自带的是2.7和3.5并没有3.6,我自己电脑的3.6是另外安装的,这个简单,就不多说了,但是输入Python3时显示的是3.5并不是3.6,显示3.6要输入Python3.6,估计是因为我电脑里面3.5的优先级高于3.6,关于优先级的操作,在下面)

三:如何切换这两个版本以及切换默认的python版本:

我们可以使用 update-alternatives来为整个系统更改Python版本。以 root身份登录,首先罗列出所有可用的python替代版本信息:

update-alternatives--list python

update-alternatives: error: no alternatives for python

如果出现以上所示的错误信息,则表示 Python的替代版本尚未被update-alternatives命令识别。想解决这个问题,我们需要更新一下替代列表,将python2.7和 python3.4放入其中。

# update-alternatives--install/usr/bin/python python/usr/bin/python2.7 1

update-alternatives: using/usr/bin/python2.7 to provide/usr/bin/python(python) in auto mode

# update-alternatives--install/usr/bin/python python/usr/bin/python3.5 2

update-alternatives: using/usr/bin/python3.4 to provide/usr/bin/python(python) in auto mode

(这里我设置没有成功,但是我还是把电脑里的三个Python版本全都设置了一遍,最后还是成功切换Python版本了)

--install选项使用了多个参数用于创建符号链接。最后一个参数指定了此选项的优先级,如果我们没有手动来设置替代选项,那么具有最高优先级的选项就会被选中。这个例子中,我们为/usr/bin/python3.4设置的优先级为2,所以update-alternatives命令会自动将它设置为默认 Python版本。

# python--version

Python 3.5.2

(因为上面博主没有设置成功,所以这一步显示的还是2.7)

接下来,我们再次列出可用的 Python替代版本。

# update-alternatives--list python

/usr/bin/python2.7

/usr/bin/python3.5

现在开始,我们就可以使用下方的命令随时在列出的 Python替代版本中任意切换了。

# update-alternatives--config python

接下来,我们再次列出可用的 Python替代版本。

# update-alternatives--list python

/usr/bin/python2.7

/usr/bin/python3.5

现在开始,我们就可以使用下方的命令随时在列出的 Python替代版本中任意切换了。

(这一步是最关键的)

# update-alternatives--config python

下面就简单了,会提示你输入序号,你想用哪个版本为默认,就输入序号就可以了!

ubuntu 下怎样安装python

1.先检查当前系统中是否已经安装python,直接使用python-V查看

2.也可以直接使用上图中提示的命令来进行安装python,但建议使用官网的安装包进行安装,如下图,选择相应的版本进行下载

3.下载完成后进行解压,解压完成后进入软件目录

4.使用如下命令进行编译安装,如下图

5.安装完成后,再对已经安装完成的软件检测发现已经安装成功,

ubuntu python怎么作为守护进程一直运行

测试程序

先写一个测试程序,用于输出日志和打印到控制台。

#-*- coding: utf-8-*-

import logging

import time

from logging.handlers import RotatingFileHandler

def func():

init_log()

while True:

print"output to the console"

logging.debug("output the debug log")

logging.info("output the info log")

time.sleep(3);

def init_log():

logging.getLogger().setLevel(logging.DEBUG)

console= logging.StreamHandler()

console.setLevel(logging.DEBUG)

formatter= logging.Formatter('%(asctime)s%(filename)s[line:%(lineno)d]%(levelname)s%(message)s')

console.setFormatter(formatter)

logging.getLogger().addHandler(console)

# add log ratate

Rthandler= RotatingFileHandler("backend_run.log", maxBytes=10* 1024* 1024, backupCount=100,

encoding="gbk")

Rthandler.setLevel(logging.INFO)

# formatter= logging.Formatter('%(asctime)s%(filename)s[line:%(lineno)d]%(levelname)s%(message)s')

Rthandler.setFormatter(formatter)

logging.getLogger().addHandler(Rthandler)

if __name__=='__main__':

func()

后台启动Python脚本

可以使用下面的命令来启动上面的脚本,让Python在后台运行。

nohup python-u main.py> test.out 2>&1&

来解释一下这几个命令的参数。这一段来自

其中 0、1、2分别代表如下含义:

0– stdin(standard input)

1– stdout(standard output)

2– stderr(standard error)

nohup python-u main.py> test.out 2>&1&

nohup+最后面的&是让命令在后台执行

>out.log是将信息输出到out.log日志中

2>&1是将标准错误信息转变成标准输出,这样就可以将错误信息输出到out.log日志里面来。

运行命令后,会返回一个pid。像下面这样:

[1] 9208

后续可以学习Hadoop它们,把pid存起来,到时候stop的时候就把它杀掉。

跟踪输出文件变化

为了验证脚本可以在后台继续运行,我们退出当前会话。然后重新连接一个Session,然后输入下面的命令来跟踪文件的输出:

tail-f test.out

输出内容如下:

output to the console

2017-03-21 20:15:02,632 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:02,632 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:05,635 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:05,636 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:08,637 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:08,638 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:11,640 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:11,642 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:14,647 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:14,647 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:17,653 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:17,654 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:20,655 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:20,656 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:23,661 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:23,661 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:26,665 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:26,666 main.py[line:12] INFO output the info log

output to the console

2017-03-21 20:15:29,670 main.py[line:11] DEBUG output the debug log

2017-03-21 20:15:29,671 main.py[line:12] INFO output the info log

说明我们的脚本确实在后台持续运行。

结束程序

可以直接通过之前的那个pid杀掉脚本,或者可以通过下面的命令查找pid。

ps-ef| grep python

输出的内容如下:

root 1659 1 0 17:40? 00:00:00/usr/bin/python/usr/lib/python2.6/site-packages/ambari_agent/AmbariAgent.py start

root 1921 1659 0 17:40? 00:00:48/usr/bin/python/usr/lib/python2.6/site-packages/ambari_agent/main.py start

user 8866 8187 0 20:03? 00:00:06/usr/bin/python3/usr/bin/update-manager--no-update--no-focus-on-map

root 9208 8132 0 20:12 pts/16 00:00:00 python-u main.py

root 9358 8132 0 20:17 pts/16 00:00:00 grep--color=auto python

可以看到我们的pid是9208,调用kill杀掉就可以了。

kill-9 9208

编写启动及停止脚本

启动脚本

#!/bin/sh

pid=`ps-ef|grep"python-u main.py"| grep-v"grep"|awk'{print$2}'`

if ["$pid"!="" ]

then

echo"main.py already run, stop it first"

kill-9${pid}

fi

echo"starting now..."

nohup python-u main.py> test.out 2>&1&

pid=`ps-ef|grep"python-u main.py"| grep-v"grep"|awk'{print$2}'`

echo${pid}> pid.out

echo"main.py started at pid:"${pid}

停止脚本

#!/bin/sh

pid=`ps-ef|grep"python-u main.py"| grep-v"grep"|awk'{print$2}'`

if ["$pid"!="" ]

then

kill-9${pid}

echo"stop main.py complete"

else

echo"main.py is not run, there's no need to stop it"

fi

稍后我会把实例代码上传到资料共享里面。

阅读剩余
THE END