分类目录归档:python

rockylinux 9 和Fedora 36的rpmbuid python模块依赖

在我的rpmbuild SPEC里边刚好需要用到Google depot_tools的ninja

本来 正常编译pip install ninja就完事了,但是在rpm环境里发现出错了

ModuleNotFoundError: No module named ‘ninia

这个事情很奇怪, 查了下资料, 在rpmbuild 环境里边python -m site 和命令行 发现了不同

rpmbuild里边的sys.path

sys.path = [
‘/root/rpmbuild/BUILD’,
‘/usr/lib64/python39.zip’,
‘/usr/lib64/python3.9’,
‘/usr/lib64/python3.9/lib-dynload’,
‘/usr/lib64/python3.9/site-packages’,
‘/usr/lib/python3.9/site-packages’,
]

命令行里边的sys.path

sys.path = [
‘/root/nginx’,
‘/usr/lib64/python39.zip’,
‘/usr/lib64/python3.9’,
‘/usr/lib64/python3.9/lib-dynload’,
‘/usr/local/lib64/python3.9/site-packages’,
‘/usr/lib64/python3.9/site-packages’,
‘/usr/lib/python3.9/site-packages’,
]

检查python的配置文件可以看到, 识别到RPMBUILD环境, 就会去掉/local/这个路径的包

当然, 解决办法也很简单, 在rpmbuild spec里边加个pip install ninja就行了

这是 rocky linux 9 的python 3.9 以及 Fedora 36 的python 3.10开始引入的一个变化

参考文档:

https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe

https://hackmd.io/@python-maint/BkqScKJW5

https://bugzilla.redhat.com/show_bug.cgi?id=1937494

https://github.com/rhinstaller/anaconda/pull/3646

使用line-profiler进行python代码调优

这里主要介绍下line-profiler

https://pypi.org/project/line-profiler/

pip install line_profiler

如果是python2,则使用3.1.0, 这是最后可用的版本

pip2 install line_profiler==3.1.0

然后在需要监测的代码函数块前边加上

@profile

然后执行

kernprof -l -v your_python_scripts.py

就能看到以行为单位的执行时间占比, 从而分析出代码的性能问题主要出在什么地方

python2 pip 安装和升级的问题

一些古老的代码是基于python2的, 需要安装一些模块的时候发现提示出错了

#pip install requests

You are using pip version 7.1.0, however version 23.1.2 is available.
You should consider upgrading via the ‘pip install –upgrade pip’ command.
Collecting requests
/usr/lib/python2.6/site-packages/pip/vendor/requests/packages/urllib3/util/ssl.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
/usr/lib/python2.6/site-packages/pip/vendor/requests/packages/urllib3/util/ssl.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning

看提示是SSL 协商失败了, 嗯嗯, 猜测是pip的SSL版本过于旧, 需要升级下pip,结果又失败了

#pip install –upgrade pip

You are using pip version 7.1.0, however version 23.1.2 is available.
You should consider upgrading via the ‘pip install –upgrade pip’ command.
Collecting pip
Using cached https://files.pythonhosted.org/packages/fa/ee/74ff76da0ab649eec7581233daeb43d8aa35383d8f75317b2ab3b80c922f/pip-23.1.2.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File “”, line 20, in
File “/tmp/pip-build-p1lQDp/pip/setup.py”, line 7
def read(rel_path: str) -> str:
^
SyntaxError: invalid syntax

----------------------------------------

Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-build-p1lQDp/pip

其实多年以前pip的在线安装和升级已经不支持python2了, 需要手工升级下

https://bootstrap.pypa.io/pip/2.7/get-pip.py

https://bootstrap.pypa.io/pip/2.6/get-pip.py

这两个分别是python的2.6 和 2.7版本的pip手工升级包, 当前时间的版本应该是pip-20.3.4, 下载后用python执行就可以自动升级pip了, 然后安装其他模块也没有问题

python 替换\n

一般来说,python可以通过strip() 或者replace(‘\n’,”)的方式来替换掉\n 字符

不过改动某个功能代码的时候异常的发现两种替换方式都失败了,debug定位了下,发现数据是通过readlines()的方式读取进来的
而readlines 会自动的把字符中的\r\n 更改为 \\r\\n,从而导致替换失败

嗯,如果是readlines的数据,只能用replace(‘\\n’,”)了
当然,不要用readlines比较好

半吊子的程序员之: python 抓不到的execpt

最近写了个try except的时候,发现每次都出现except,但是注释掉try except代码却不出错
try:
code line 1
code line 2

except:
excpet code …

查了下文档,可以用sys.exc_info()[0]把excpet信息打印出来
我的这个例子中刚好是在函数块中直接sys.exit(X),导致了exceptions.SystemExit
于是改了下代码,在main函数中处理了下,才sys.exit(X),错误自然也就没了

恩恩,我是半吊子的程序猿

python mysql乱码问题

最近临时有个需求需要用python 爬点中文数据,结果悲催的发现乱码了

查了下资料,这里做下总结:

1. 首先python的代码需要是UTF-8的

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

2.确认mysql的数据库和表编码是UTF8的

show create database …

show create table…

如果不是,alter table或者database设置下编码(请注意alter database编码造成的影响)

3.pytho mysql连接时确认使用UTF8

MySQLdb.connect (…,charset=’utf8′)

4.暂时来说,应该是正常的了,如果还不行,请尝试如下操作:

修改/etc/my.cnf

[client]default-character-set = utf8

[mysqld]default-character-set = utf8

在python代码中增加:

reload(sys)

sys.setdefaultencoding(‘utf-8’)

python 的与或非

def intersect(a, b):

“”” return the intersection of two lists “””
return list(set(a) & set(b))

def union(a, b):
“”” return the union of two lists “””
return list(set(a) | set(b))

def difference(a, b):
“”” show whats in list b which isn’t in list a “””
return list(set(b).difference(set(a)))
备注:这里应该是list(set(b)-set(a))

这个代码非常方便的使用在比较两份海量url的共同元素上