In my working folder I have a file called exceptions.py, which contains only:
class ParseException(Exception):
pass
class QueryException(Exception):
pass
In the same folder I have a file, lets call it my_script.py
In my_script.py, when I do from exceptions import ParseException I get an error:
ImportError: cannot import name ParseException
I cannot figure out what's going on here. I've never seen this error before, and when I looked it up I mainly see problems with circular dependencies, but I don't have any here...
There is the exceptions module in your Python library which doesn't have the methods/classes you're trying to import. Change your file name to something different and it'll work.
Try the following yourself
>>> import exceptions
# Works OK
>>> from exceptions import ParseException
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: cannot import name ParseException
>>> from exceptions import ImportError # Works OK too
Related
Why does lottie give the error ImportError: cannot import name 'PngRenderer' from 'lottie.exporters.cairo'?
import lottie
from lottie.exporters.gif import export_gif
from lottie.parsers.tgs import parse_tgs
def convert_tgs_to_gif(tgs_file, gif_file):
try:
animation = parse_tgs(tgs_file)
export_gif(animation, gif_file, skip_frames=5, dpi=48)
return True
except Exception:
logging.exception("Error occurred while converting TGS to GIF.")
return False
Traceback errors:
Traceback (most recent call last):
File "C:\Users\Bubunduc\PycharmProjects\tg\main.py", line 11, in <module>
from lottie.exporters.gif import export_gif
File "C:\Users\Bubunduc\PycharmProjects\tg\venv\lib\site-packages\lottie\exporters\gif.py", line 5, in <module>
from .cairo import PngRenderer
ImportError: cannot import name 'PngRenderer' from 'lottie.exporters.cairo' (C:\Users\Bubunduc\PycharmProjects\tg\venv\lib\site-packages\lottie\exporters\cairo.py)
I tried to import this library in different ways, tried to understand the source code
Here the problem is in a completely different library. PngRenderer requires the cairosvg library. On windows, it requires libcairo-2.dll. Therefore, the solution to this problem is the correct installation of cairosvg.
Answer about installing cairosvg on windows.
I'm using the module subliminal to easily retrieve subtitles from the internet.
The CLI works perfectly, but I cannot import it as a module in a .py file.
The simplest code ever:
import os
from subliminal import scan_videos
videos = scan_videos('./')
print(videos)
And I always have this error:
Traceback (most recent call last):
File "subliminal.py", line 2, in <module>
from subliminal import scan_videos
File "/Users/louisbertin/Desktop/videos/subliminal.py", line 2, in <module>
from subliminal import scan_videos
ImportError: cannot import name 'scan_videos' from 'subliminal'
I don't know what I'm doing wrong. I had copy-pasted the code from the documentation:
https://github.com/Diaoul/subliminal
Seems like you called your script subliminal.py, which makes Python find it instead of the installed module. Rename the script and you should be fine.
I have three files in folder
exceptions.py
class MyException(Exception):
pass
MyClass.py which begins as:
from exceptions import MyException
And empty __init__.py
When I'm trying to import MyClass.py there is Error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "MyClass.py", line 1, in <module>
from exceptions import MyException
ImportError: cannot import name MyException
I've read docs and a lot of articles but can't find what's wrong
There is a standard built-in module named exceptions which is imported instead of your exceptions.py.
You can either rename your exceptions.py or use dotted import:
from .exceptions import MyException
See https://docs.python.org/2/tutorial/modules.html#intra-package-references for more.
You can't use the name exeptions.py as there already exists a module named exeptions, which has no class named Myexeption, so you are getting thus error. Just change the file name and you will be all right.
For some reason I receive an ImportError every time I try to import a class from another file. Here's the github page for my project: https://github.com/wheelebin/mcnextbot
Here's the error that I'm receiving:
Traceback (most recent call last):
File "ircbot.py", line 36, in <module>
from test import mcnextlvl
ImportError: cannot import name mcnextlvl
Here your from test import something refers to the module test in <PYTHONPATH>/lib, not yourselves test.py, and there is no submodule/class mcnextlvl there. You should use from lib.test import mcnextlvl as #sgmart commented.
The __init__.py python file allow you can import your individual modules named 'test' from the lib package.
Here's a mysterious python problem:
I'm developing a python package that occasionally reports import errors looking like ImportError: cannot import name …. The modules it cannot import generally
are importable
do not have any circular import issues (that I can detect).
I have been able to reproduce a similar effect with this simple example:
mypkg/__init__.py:
from . import module_a
yarg ## cause import error
mypkg/module_a.py:
print "imported module_a"
Now I will attempt to import the package twice. Notice that the error changes on the second import:
>>> import mypkg
Module A imported
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mypkg/__init__.py", line 2, in <module>
yarg
NameError: name 'yarg' is not defined
>>> import mypkg
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mypkg/__init__.py", line 1, in <module>
from . import module_a
ImportError: cannot import name module_a
What gives?
Note:
the problem goes away if I use an absolute import instead
if I delete the key sys.modules['mypkg.module_a'] after the first import, then the second import gives me back the original error message
I can illustrate what is causing the difference between each import, but I'm not expert enough on Python's import process to be able to explain the why very well.
>>> import sys
>>> before_import = set(sys.modules.keys())
>>> import mypkg
imported module_a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mypkg\__init__.py", line 2, in <module>
yarg ## cause import error
NameError: name 'yarg' is not defined
>>> after_import = set(sys.modules.keys())
>>> after_import.difference(before_import)
set(['mypkg.module_a'])
When you import mypkg, it successfully imports module_a and adds it to sys.modules. Then mypkg errors and doesn't get added itself to the sys.modules dictionary. Deleting the entry allows you to reimport with the same error:
>>> import sys
>>> del sys.modules['mypkg.module_a']
>>> import mypkg
imported module_a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "mypkg\__init__.py", line 2, in <module>
yarg ## cause import error
NameError: name 'yarg' is not defined
Now, what I think is happening is:
import mypkg starts the import process for mypkg
As it's processing mypkg, it successfully imports module_a as
a subpackage of itself and adds it to sys.modules
When it hits the error, the import process for mypkg fails and no
entry for mypkg is left in sys.modules
The conjunction of the package failing but the subpackage succeeding
conflicts with subsequent imports
That's about the best I can fathom, sorry. Python's import process is something of a black art.
I'm pretty sure the problem is that your package is failing to load. You've put some nonsense (yarg by itself) in the __init__.py file. This means that mypkg can't be imported. Because of this, mypkg.module_a can't be imported either.
I suspect you get different errors because Python is doing some caching of the module state. The first time you try importing mypkg the import of its submodule module_a is allowed even though mypkg is in the process of being loaded. The second time, the fact that mypkg doesn't work right is cached, so mypkg.module_a fails to load since its parent package is broken.