Python遇到的问题汇总

print 换行问题

Python的print语句默认是在末尾添加一个换行符,如果不想换行,则可以在末尾添加一个comma即可。如果想用其他符号,可以使用end来改变。

print('echoenv...', end=' ')

提示如下错误: the end='' is invalid syntax

解决办法:
在Python脚本文件的首行添加如下一条import语句:

from __future__ import print_function

注意:这条语句放到文件的开始处

字符串格式化问题

如果要格式化的字符串里包含{},可以使用双大括号来解决此问题。

libmagic问题

在Mac下遇到了如下的问题:

ImportError: failed to find libmagic. Check your installation
brew install file-formula

EnvironmentError

EnvironmentError: mysql_config not found

解决办法:

# centos7
yum install -y mariadb-devel

flask-oauth

Py3安装flask-oauth时遇到问题:

File "/Users/liuchuan/programming/mydjango/venv3/lib/python3.5/site-packages/flask_oauth.py", line 13, in <module>
from urlparse import urljoin
ImportError: No module named 'urlparse'

问题原因:urlparse在Py3.x中已经改名。修改上述有问题的文件如下:

# urlparse module has been renamed in Python 3.x  
try:  
    from urlparse import urljoin   
except ImportError:  
    from urllib.parse import urljoin

sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread.

如何解决:

conn = sqlite3.connect('your.db', check_same_thread=False)

SSL错误

C:\Python27\lib\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.

解决办法:

# pip install requests[security]

不用CentOS6.x即可解决问题,屁事没有。如果非要解决,请往下看。

CentOS6.x自带的SSL版本过低(1.0.1),安装Python3.7需要1.0.1h以上的版本,需要单独安装OpenSSL。

wget https://www.openssl.org/source/openssl-1.0.2o.tar.gz
tar -xf openssl-1.0.2o.tar.gz
rpm -q openssl-devel
cd openssl-1.0.2o
./config --prefix=/data/apps/openssl-1.0.2o --openssldir=/data/apps/openssl-1.0.2o
make
make install

修改Python源码中的文件,Modules/Setup.dist的大概211行至214行,如下:

[root@ip-192-168-2-50 Python-3.7.2]# sed -n '211,214p' Modules/Setup.dist
SSL=/data/apps/openssl-1.0.2o
_ssl _ssl.c \
 -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
 -L$(SSL)/lib -lssl -lcrypto

这个一个BUG,根据Python官方网站提供了一个补丁文件,https://bugs.python.org/issue32627,
应用一下即可。操作步骤如下:

1. 获取补丁文件
# cat _uuidmodule.patch 
--- Modules/_uuidmodule.c.orig
+++ Modules/_uuidmodule.c
@@ -3,8 +3,7 @@
 #include "Python.h"
 #ifdef HAVE_UUID_UUID_H
 #include <uuid/uuid.h>
-#endif
-#ifdef HAVE_UUID_H
+#elif defined(HAVE_UUID_H)
 #include <uuid.h>
 #endif

# 应用补丁文件
patch < _uuidmodule.patch
# 根据提示,输入: Modules/_uuidmodule.c
# 即可
# 如果遇到_ctypes的问题,可以试着安装如下的包
yum install -y libffi-devel
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LavenLiu

常言道:投资效率是最大的投资。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值