ModuleError: No module named pptx found - python

I am writing a python code for merging ppts. It takes the location of the 2 ppts, merges them and puts the merged ppt formed in the folder given by user. The code used is:
import sys
from pptx import Presentation
#import Aspose.Words.License
#import aspose.slides as a_slides
#import os
#import win32com.client
def merge_powerpoint_ppts(pres_loc1, pres_loc2, output_loc):
p1 = open(pres_loc1)
pres1 = Presentation(p1)
p2 = open(pres_loc2)
pres2 = Presentation(p2)
for slide in pres2.slides:
for lide in pres1.slides:
if slide.shape.title.text == lide.shape.title.text:
pres1.slides.add_Clone(slide)
pres1.save(output_loc)
p1.close()
p2.close()
When I try to debug the code, I get the following:
[Error][1]
[1]: https://i.stack.imgur.com/yID2l.png
I have already installed the module pptx on my system and it is updated, but I am still getting this error.

First if you have a folder named Presentation or pptx change it cause this could happen because of naming confusion of files or folder and python modules
Secondly make sure you use the correct python interpreter or env where you install pptx in in
last Option uninstall pptx and write the following command
conda install -c conda-forge python-pptx

Related

Import on shared object fails with ModuleNotFound only when script is executed from command line

ran into a weird problem where there is a shared-object import error only when I run the script from command line. It succeed if i run the script in the python console using exec(...)
I have a class that needs a shared object: foo.py:
import os
cur_dir = os.curdir()
os.chdir('/tmp/dir_with_shared_object/')
import shared_object_class
os.chdir(cur_dir)
class Useful:
... # use the shared object import
Then there is a script like this:
from foo import Useful
If I enter python console and run:
exec(open('script.py').read())
Everything works fine.
If I run this on command line:
python script.py
I will get
ModuleNotFoundError: No module named 'shared_object_class'
The python is the same. It is 3.7.3, GCC 7.3.0 Anaconda. Anyone knows what is causing this discrepancy in behavior for shared object import?
A standard way of importing from a custom directory would be to include it in the PYTHONPATH environmental variable, with export PYTHONPATH=/tmp/dir_with_shared_object/.
update
It could also be done dynamically with
import sys
p = '/tmp/dir_with_shared_object/'
sys.path.insert(0, p)
PS
I think I have an explanation for why OP's original code didn't work. According to this python reference page, the import system searches, inter alia, in "[t]he directory containing the input script (or the current directory when no file is specified)." So the behavior in the REPL loop is different from how it is when running a script. Apparently the current directory is evaluated each time an import statement is encountered, while the directory containing the input script doesn't change.

Python module script.vis install

Really sorry for my novice question.
I am trying to install a module in python for neo4j but I got an error.
here is the import :
from scripts.vis import vis_network
from scripts.vis import draw
Here is the error:
ModuleNotFoundError: No module named 'scripts'
I have tried "pip install scripts"
Thanks in advance
ModuleNotFoundError simply means the Python interpreter couldn't find the module. I suggest that you read about python modules and packaging here.
I have looked at the source code you pointed to and it works perfectly fine. I suspect your paths are not well set up.
Make sure that in you are running importing scripts.vis in app.py, the directory structure looks like this:
./scripts
./scripts/__init__.py
./scripts/vis.py
....
./app.py #in app.py, you can import as 'from scripts.vis import x'
Here's what it looks on my system:
app.py is successfully able to make the import from vis sub-module. You can use a IPython notebook, that should work fine too.
If you want to visualize the graph in the python environment (Jupyter), you can try using neo4jupyter library. Here you will use neo4jupyter.draw to visualize the graph.
Install !pip install neo4jupyter
For example:
import neo4jupyter
neo4jupyter.init_notebook_mode()
from py2neo import Node
nicole = Node("Person", name="Nicole", age=24)
drew = Node("Person", name="Drew", age=20)
graph.create(nicole | drew)
options = {"Person": "name"}
neo4jupyter.draw(graph, options)
You may find this useful:
https://github.com/merqurio/neo4jupyter
https://nicolewhite.github.io/neo4j-jupyter/hello-world.html

Import python package in the module

I am beginner in Python. I am using set python librarys and I want to take the part of my code in .py module. Where should I write "import" of the set of libraries, in the module, or in main file? If I dont write it in the module, program doesn't work.
#mainfile.py
import cv2
import faceResearch
faceResearch.mn()
#faceResearch.py
import cv2
def mn():
image = cv2.imread("Smiling/3--1873301-Smiling woman looking at camera.jpg")
cv2.imshow("im", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
#so, in wich file should I write "import cv2"? in mainfile? in file of the module? or in both?
You should keep import cv2 in your module (faceResearch.py ?) not in main.py. With that all others scripts importing your module will automatically import cv2 ; if cv2 is installed. You can check if cv2 is installed, and if not display an error message : link

Windows Python 2.7 wxwidgets and 'gizmos'

Can anyone give me precise instructions on how to access the wx 'gizmos' module?
import wx.gizmos
ImportError: No module named gizmos
The code in question has this:
import wx
import string
import wx.gizmos
from wx.lib.mixins import treemixin
import Descriptor
'pip list' reports
wxPython-Phoenix (3.0.3.dev1830+0b5f910)
Do I have the right package installed? I should add that these files are present:
\python27\Lib\wxpython\wx-3.0-msw\wx\gizmos.py
\python27\Lib\wxpython\wx-3.0-msw\wx\_gizmos.pyd
[edit] For clarification, this seems to be OK so I'm reasonably sure the WX module is installed correctly.
import wx
import copy
# had to add .agw to get this to load
import wx.lib.agw.customtreectrl as CT
import DescriptorDetailsPanel
TAIA
Congrats, you have two installs of wx. You can use pkg_resources to get the one you want. Put the following at the top of the script:
__requires__ = ["wx >= 3.0"]
import pkg_resources
This will tell pkg_resources to set things up so that a version of wx of at least 3.0 will be available if you import wx rather than the default 2.x.
You can try to remove wxpython from pip install and reinstall wxpython from this site:
https://www.wxpython.org/download.php
It worked for me!

Using portable python to connect to an Access database

I am wanting to use portable python 2.7.x to connect to an Access database. I can't seem to get it working as it doesn't have the pyodbc libraries. Is there another way to use portable python to connect?
The newest version of portable python has an option to install pyodbc but you have to select the option it doesn't go in by default.
Click on the modules option
Select the option for pyodbc
I have did it in different way.. .
follow what i have just done on my mac snow leopard!!
Download the the pyodbc's source from where it is on internet.
Extract and 'cd' into that dir.. . Run 'python setup.py build' and then take 'pyodbc.so' file from that build's dir. Make new python file named as 'pyodbc.py' and write the content given below.(and put that 'pyodbc.so' file with it)
def __bootstrap__():
global __bootstrap__, __loader__, __file__
import sys, pkg_resources, imp
__file__ = pkg_resources.resource_filename(__name__,'pyodbc.so')
__loader__ = None; del __bootstrap__, __loader__
imp.load_dynamic(__name__,__file__)
__bootstrap__()
(remember put above code in file named as 'pyodbc.py' and put that 'pyodbc.so' file with that)
and at last ..put all these where ever you want to use or in run time add that location into sys.path as:
>>> import sys
>>> sys.path.insert(0,"/my_portable/location") # location to dir which contains those two files
after doing all this i have put those two files with my test python file..and in that i am able to import 'pyodbc' without installing it.
>>> import pyodbc
>>> dir(pyodbc)

Categories