简体   繁体   English

导入 python-pptx:ModuleNotFoundError:没有名为“pptx”的模块

[英]Importing python-pptx: ModuleNotFoundError: No module named 'pptx'

I'm running Python 3.6.6rc1 on macOS Mojave (10.14.1) and I'm trying to import python-pptx我在 macOS Mojave (10.14.1) 上运行 Python 3.6.6rc1 并且我正在尝试导入 python-pptx

Currently, my first line is causing a problem:目前,我的第一行导致了一个问题:

import python-pptx

I deleted that and added this, to no avail.我删除了它并添加了它,但无济于事。

from pptx import Presentation

This is my error:这是我的错误:

ModuleNotFoundError: No module named 'pptx'

I have downloaded python-pptx using pip:我已经使用 pip 下载了 python-pptx:

sudo pip install python-pptx

Running pip show python-pptx in the Terminal, I get:在终端中运行pip show python-pptx ,我得到:

Name: python-pptx
Version: 0.6.16
Summary: Generate and manipulate Open XML PowerPoint (.pptx) files
Home-page: http://github.com/scanny/python-pptx
Author: Steve Canny
Author-email: python-pptx@googlegroups.com
License: The MIT License (MIT)
Location: /Library/Python/2.7/site-packages
Requires: lxml, Pillow, XlsxWriter
Required-by: 

As you can see, the Location is different than the Version .如您所见, LocationVersion不同。 Is that a problem?那是问题吗?


Running sys.path in the shell shows:在 shell 中运行sys.path显示:

['/Users/gstrickland/Desktop', '/Users/gstrickland/Documents', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages']

Running python -m pip show python-pptx I get this:运行python -m pip show python-pptx我得到这个:

Name: python-pptx
Version: 0.6.16
Summary: Generate and manipulate Open XML PowerPoint (.pptx) files
Home-page: http://github.com/scanny/python-pptx
Author: Steve Canny
Author-email: python-pptx@googlegroups.com
License: The MIT License (MIT)
Location: /Users/gstrickland/Library/Python/2.7/lib/python/site-packages
Requires: lxml, Pillow, XlsxWriter
Required-by: 

Different location, but still in 2.7不同的位置,但仍在2.7


Running python -c'import sys; print(sys.path)'运行python -c'import sys; print(sys.path)' python -c'import sys; print(sys.path)' gives me: python -c'import sys; print(sys.path)'给我:

['', '/Library/Python/2.7/site-packages/pip-18.1-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Users/gstrickland/Library/Python/2.7/lib/python/site-packages', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']

How do I fix this error?我该如何解决这个错误?

You've installed python-pptx with a pip corresponding to the system Python 2.7, not the Python 3.6 you're trying to use.您已经使用与系统 Python 2.7 相对应的 pip 安装了 python-pptx,而不是您尝试使用的 Python 3.6。 Install things with安装东西

python -m pip install --user ...

instead of代替

sudo pip install ...

to ensure you're using the right pip for your Python, and to avoid some of the other problems associated with running pip through sudo.确保您为 Python 使用正确的 pip,并避免与通过 sudo 运行 pip 相关的一些其他问题。

Check if the module is available in any of the paths printed by "sys.path".检查模块是否在“sys.path”打印的任何路径中可用。
Either the module isn't installed or isn't available in module search path.模块未安装或在模块搜索路径中不可用。

You need to install python-pptx package as :您需要将 python-pptx 包安装为:

pip install python-pptx

You can test the code to verify installation :您可以测试代码以验证安装:

from pptx import Presentation


def main():
    prs = Presentation()
    title_slide_layout = prs.slide_layouts[0]
    slide = prs.slides.add_slide(title_slide_layout)
    title = slide.shapes.title
    subtitle = slide.placeholders[1]

    title.tetx = "Hello World fromm pptx"
    subtitle.text = "using python-ppts!!!"
    prs.save("test.pptx")


if __name__ == "__main__":
    main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM