I have tried to write a sample code that converts the contents of pdf to word
this is the code :
from pdf2docsx import converter
pdf = 'main.pdf'
word = 'my.docx'
cv = converter(pdf)
cv.convert(word,start=0,end=None)
cv.close()
Although i already installed the module pdf2docx , it still doesn't exist
Traceback (most recent call last):
File "c:\Users\dell\Desktop\New folder (8)\My_Start\python\PdfToWord.py", line 2, in <module>
from pdf2docsx import converter
ModuleNotFoundError: No module named 'pdf2docsx'
The module appears to be called pdf2docx, whereas you have added an "s" to make pdf2docsx
https://pypi.org/project/pdf2docx/
Change your import to
from pdf2docx import converter
There's not supposed to be an "s" there
Related
So I'm just starting to learn Python, and I am learning classes and imports, but for some reason even when I follow the same code as my book say to do, I get a traceback error.
Traceback (most recent call last):
File "C:/Users/Programming/Desktop/Programs/EX40/main.py", line 1, in <module>
import objects.py
ModuleNotFoundError: No module named 'objects.py'; 'objects' is not a package
Here is both my Python files in which I'm trying to link:
main.py
import objects.py
print(MyStuff.tangerine)
objects.py
class MyStuff(object):
def __init__(self, arm):
self.tangerine = 'And now a thousand years between'
self.arm = arm
def apple(self):
print('I AM CLASSY APPLES!')
Try using
import objects
Because it takes py as a module in the objects file which does not exist
import objects.py is not a valid import
https://docs.python.org/3/reference/import.html
Try
from objects import MyStuff
Instantiating the class:
mystuff = MyStuff("arm value")
print(mystuff.tangerine)
I have a following directory structure:
source
source_1.py
__init__.py
source1.py has class Source defined
source1.py
class Source(object):
pass
I am able to import using this
>>> from source.source1 import Source
>>> Source
<class 'source.source1.Source'>
However when trying to import using the below method it fails.
>>> from source import *
>>> source1.Source
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'source1' is not defined
Please let me know how can we use the 2nd import ?
For importing from a package (unlike importing from a module) you need to specify what * means. To do that, in __init__.py add a line like this:
__all__ = ["source1"]
See the Python documentation for Importing * From a Package.
I have this structure
02.SensorTag/
sensortag_example.py
bluepy/bluepy/sensortag.py
bluepy/bluepy/btle.py
So the sensortag_example.py is importing the sensortag.py
import bluepy
from bluepy.bluepy import sensortag
When I ran the code it complains about the import from the sensortag.
Traceback (most recent call last):
File "sensortag_example.py", line 2, in <module>
from bluepy.bluepy import sensortag
File "/home/pi/Development/02.SensorTag/bluepy/bluepy/__init__.py", line 3, in <module>
from . import sensortag
File "/home/pi/Development/02.SensorTag/bluepy/bluepy/sensortag.py", line 1, in <module>
from bluepy.btle import UUID, Peripheral, DefaultDelegate, AssignedNumbers
ImportError: No module named 'bluepy.btle'
I've tried to add a new path but it didin't work. If I move the program to the first folder bluepy and change the import to "from bluepy import sensortag" it works, but I'll need to import other libs so I don't want to let it in bluepy folder.
I am trying to run this code:
https://gist.github.com/atotto/ae603b962115eef703c0011d8e652ea3
Thanks and best regards,
Edu
Because sensortag.py is in the same directory as btle.py, add a . in front of the import
from .btle import UUID, Peripheral, DefaultDelegate, AssignedNumbers
This is known as a relative import: https://docs.python.org/2.5/whatsnew/pep-328.html
As both btle.py and sensortag.py are in the same directory so by looking at your error I am assuming that you tried to import it from previous directory. So place from .btle import UUID in sensortag.py should solve the issue.
You should create two init.py file.
02.SensorTag/
sensortag_example.py
bluepy/__init__.py
bluepy/bluepy/__init__.py
bluepy/bluepy/sensortag.py
bluepy/bluepy/btle.py
I have setup the following structure: Python 201: Creating Modules and Packages.
The mymathpackage is available at that link.
When you run the code below:
import sys
sys.path.append('G:\MyPython\Package')
import mymath
print (mymath.add(4,5))
print (mymath.division(4, 2))
print (mymath.multiply(10, 5))
print (mymath.fibonacci(8))
print (mymath.squareroot(48))
Python Version: 3.4
outer __init__.py contents:
from add import add
from divide import division
from multiply import multiply
from subtract import subtract
from adv.fib import fibonacci
from adv.sqrt import squareroot
My goal is to call division,add,subtract, etc, but if I try to call the module I get:
Traceback (most recent call last):
File "G:\MyPython\Package\myscript.py", line 1, in <module>
import mymath
File "G:\MyPython\Package\mymath\__init__.py", line 1, in <module>
from add import add
ImportError: No module named 'add'
Python 3.x has changed import resolution. You must now specify a full relative import if you want to perform a relative import.
from .add import add
Summary:
The same import statement that works when __name__ == "__main__" stops working when imported from another module outside the immediate package. If I fix it so that it works when imported, it stops working when __name__ == "__main__". Is there a win-win solution for these conflicting scopes?
In more detail:
Say I have the following system layout.
package/
__init__.py
outer_module.py
subpackage/
__init__.py
inner_module_1.py
inner_module_2.py
And say that all modules are empty except for the following two.
# inner_module_1
import inner_module_2
&
# outer_module
import subpackage.inner_module_1
import subpackage.inner_module_2
If I run inner_module_1, there is no error.
If I run outer_module, I get the following ImportError from inner_module_1.
Traceback (most recent call last):
File "C:\package\outer_module.py", line 1, in <module>
import subpackage.inner_module_1
File "C:\package\subpackage\inner_module_1.py", line 1, in <module>
import inner_module_2
ImportError: No module named 'inner_module_2'
I can prevent that error by adding the package name to the import statement in inner_module_1 as follows.
# inner_module_1
import subpackage.inner_module_2
Although this change allows outer_module to run without an error, now running inner_module_1, which had before run without error, raises the following error.
Traceback (most recent call last):
File "C:\package\subpackage\inner_module_1.py", line 1, in <module>
import subpackage.inner_module_2
ImportError: No module named 'subpackage'
So here are my questions.
Is the import statement in inner_module_1 interpreted differently when accessed by outer_module than when run in inner_module_1, and if so, how so?
Can I write an import statement in inner_module_1 that works both when inner_module_1 is run and also when outer_module is run, and if so, what is that statement?
Thanks,
Victor