Importing python-pptx: ModuleNotFoundError: No module named 'pptx'

允我心安 提交于 2020-07-08 03:18:21

问题


I'm running Python 3.6.6rc1 on macOS Mojave (10.14.1) and I'm trying to import 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:

sudo pip install python-pptx

Running pip show python-pptx in the Terminal, I get:

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. Is that a problem?


Running sys.path in the shell shows:

['/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:

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


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

['', '/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?


回答1:


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. 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.




回答2:


Check if the module is available in any of the paths printed by "sys.path".
Either the module isn't installed or isn't available in module search path.




回答3:


You need to install python-pptx package as :

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()


来源:https://stackoverflow.com/questions/53599384/importing-python-pptx-modulenotfounderror-no-module-named-pptx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!