yyccR
2/27/2018 - 7:58 AM

py

一些问题总结

出现 No module name "xxxx" 时在入口文件的头部加入如下语句:

import sys
sys.path.append("your project director")

python format格式化出现 keyError XXX:

对那些不需要往里面加东西的大括号,再加一个大括号:
e.g: 
test = """some test data {"test":{}} """.format(1)    # error
test = """some test data {{"test":{}}} """.format(1)  # right

a目录下的A.py文件读取b目录下的文件b.txt, 如果当前程序入口在c目录的c.py文件,那么a里面的相对路径需要注意:

# 获取当前文件的目录,而非运行文件的目录
cur_path = os.path.dirname(os.path.abspath(__file__)) + "/"
# 再将相对路径组合上面的father_path即可。

python 虚拟环境 virtualenvwrapper

vim /etc/profile
export WORKON_HOME=/data/bigData/lujingqiao/py-virtualenv
source /usr/local/bin/virtualenvwrapper.sh

mkvirtualenv --no-site-packages -p /usr/bin/python3.5 py35-tf16-env
pip install tensorflow-gpu==1.6

pip 更新导致的 no module named _internal 问题

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py --force-reinstall

python: Depends: python3 (< 3.5) but 3.5.1-3 is to be installed 这种问题:

替换下源就可以:
先备份 /etc/apt/sources.list

然后 vim /etc/apt/sources.list
添加下面的源:
# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse

python: Python.h: No such file or directory 问题:

sudo apt-get install python-dev   # for python2.x installs
sudo apt-get install python3-dev  # for python3.x installs

python:代理

pip install XXX --proxy="http://192.168.1.209:3128"
HTTP_PROXY=http://192.168.1.209:3128 HTTPS_PROXY=http://192.168.1.209:3128 mkvirtualenv --no-site-packages -p /usr/bin/python3.5 py35-tf16-env

centos6.8 安装pip

问题:yum安装提示pip找不到问题:No package pip available. Error: Nothing to do

解决方案:
sudo yum -y install epel-release
yum -y install python-pip

pip upgrade 出现问题 ImportError: cannot import name 'main'

参考:https://github.com/pypa/pip/issues/5447
解决方案:
hash -d pip

python出现:'ascii' codec can't encode characters 问题

一般是window中文放到Linux下出现的问题

解决方案
python2的话:
import requests, re, sys
reload(sys)
sys.setdefaultencoding("utf-8")

python3的话:
1.删掉那些print出现中文的
2.读文件时 encode参数为“utf-8”

python 'utf-8' codec can't decode byte 0xe9 问题

字符编码utf-8无法转换问题,一般是包含了一些其他语言字符
解决方案:
1. 读文件的时候 encoding="ISO-8859-1"
2. 直接对文本 .decode("ISO-8859-1")

win10下 tensorflow2.1 tensorflow2.2 出现ImportError: DLL load failed: 找不到指定的模块

参考官网 https://www.tensorflow.org/install/pip?hl=zh_cn 对pip安装的说明:
Windows 7 或更高版本(64 位)(仅支持 Python 3)需要安装VS2015/17/19的新扩展插件:
https://support.microsoft.com/zh-tw/help/2977003/the-latest-supported-visual-c-downloads

python json.dumps(data) 中文时出现Unicode

解决方案:json.dumps(data, ensure_ascii=False)

add-apt-repository command not found

apt-get install software-properties-common

mac jupyter指定不同版本jupyter

jupyter安装时根据不同Python版本会放到对应的library目录
如:
/usr/bin/pip3 install jupyter
会安装到:
/Users/yy(your_user_name)/Libraries/Python/3.8(your_version)/bin/jupyter

启动的时候直接:
/Users/yy(your_user_name)/Libraries/Python/3.8(your_version)/bin/jupyter notebook即可

或者将前面一串路径(/Users/yy(your_user_name)/Libraries/Python/3.8(your_version)/bin)加到path路径

labelme 重新加载上一次打标的数据

labelme --keep-prev --output 你的标记文件目录

python虚拟环境启动 python3 -m venv venv/ 报错: Error: Command '['/home/root/stable-diffusion-webui/venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

# 如果是python3.8, 其他版本同理
apt-get install python3.8-venv

[nltk_data] [Errno 99] 问题

手动下载对应的nltk数据, 拷贝到nltk加载的对应目录下,

查看nltk搜索的目录列表:
import nltk
nltk.find(".")