文章目录
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