按位置导入包

Import package by location

已在 ubuntu 16.04 上为 python3.5 安装 python-docx 软件包。

包安装成功,pip3 show python-docx给出:

Name: python-docx
Version: 0.8.10
Summary: Create and update Microsoft Word .docx files.
Home-page: https://github.com/python-openxml/python-docx
Author: Steve Canny
Author-email: python-docx@googlegroups.com
License: The MIT License (MIT)
Location: /home/otariki/.local/lib/python3.5/site-packages
Requires: lxml
Required-by: 

然后,我尝试将包导入到我的 python 文件中,例如:

import sys
sys.path.append('/home/otariki/.local/lib/python3.5/site-packages/docx')
from docx import Document

给出错误:ImportError: No module named 'docx'

我的错误是什么?

您需要附加 /home/otariki/.local/lib/python3.5/site-packages/ 文件夹。为什么?该包位于文件夹 docx 中(__init__.py 文件表明 docx 是一个包)。

注意:请注意 $HOME/.local/lib/python3.5/site-packages/ 文件夹默认应该是 PYTHONPATH 的一部分。我的意思是:您不需要将该文件夹添加到 sys.path,它应该由 Python.

自动完成