Best practice to locate "attempted relative import beyond top-level package" - python

I would like to narrow the common error "attempted relative import beyond top-level package" down by including the exception-handling in the imports already.
In my Django tests in DjangoProject/tests.py I wrote the imports relative to itself (tests.py).
The default unittests.py which executes each testfile is located in the root-folder which also includes the DjangoProject-folder.
As I was getting the error attempted relative import beyond top-level package the moment I added the
DjangoProject/tests.py to my Unit-Tests adnd it was clear it came from the imports. With a try/except I was able to write a slightly clearer error so I'm not in the need of guessing around why and which file caused the error.
Now my code looks like this:
try:
from django.utils.safestring import mark_safe
import json
from django.conf import settings
from viewsfunctions import *
from .. import PoM
import unittest
except Exception as importex:
print("Error in the tests.py-imports: "+str(importex))
And it threw first of all the message:
"No module named 'viewsfunctions'"
which makes sense and is helpful as viewsfunctions is a module inside DjangoProject. I replaced the line with: from DjangoProject.viewsfunctions import *
The next error is:
Error in the Imports: attempted relative import beyond top-level package
And this is exactly my problem. This could be anything. By just pdbing around I could track it down to from .. import PoM (as the module PoM is relative to tests.py in the top-folder, but relative to the executing unittests.py in the same folder)
In this case with just a few imports it's relatively fast to narrow it down, but is there any way to write a better exception message or deliver somehow more information in which of the import-lines the error actually happened?

If you would simply not catch the exception, you'd get a very useful traceback:
>>> from .. import foo
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Users/deceze/...", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ImportError: attempted relative import with no known parent package
The problem is that you're catching the exception and are only printing the most minimal error message possible. If you do want to catch the exception for some reason yet still get a complete error message, the easiest way would be to use the logging module:
import logging
try:
...
except:
logging.exception('Error fooing the bar')
This will produce your custom error message followed by a complete stack trace.
To get even more manual, use the traceback module:
import traceback
try:
...
except:
print('Error fooing the bar')
traceback.print_exc()
With other functions in traceback you can get even more granular and print or retrieve messages from an exception as granular as you like.

Related

Python file import doesn't appear in namespace prior to exception, but does after exception

I have a snippet of Python code that throws an exception "No module named 'common_functions'" when I try to import the file "common_functions.py" from folder API.
def validate_config():
try:
import API
logger.debug(dir(API))
from API.common_functions import get_dbusername
except Exception as e:
import API
logger.debug(dir(API))
from API.common_functions import get_dbusername
When I look at the output of the logging of the modules present in API initially it is:
[..., 'config', 'external_config_operations']
but in the exception the missing module is present:
[ ..., 'common_functions', 'config','external_config_operations']
I've tried clearing out all cached files and for some reason this is still happening. Does anyone know why module discovery is working this way?

pytest: how might I simulate non-availability of a pip-installed module? [duplicate]

I'm trying to use a third-party lib (docutils) on Google App Engine and have a problem with this code (in docutils):
try:
import pwd
do stuff
except ImportError:
do other stuff
I want the import to fail, as it will on the actual GAE server, but the problem is that it doesn't fail on my development box (ubuntu). How to make it fail, given that the import is not in my own code?
Even easier than messing with __import__ is just inserting None in the sys.modules dict:
>>> import sys
>>> sys.modules['pwd'] = None
>>> import pwd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pwd
In your testing framework, before you cause docutils to be imported, you can perform this setup task:
import __builtin__
self.savimport = __builtin__.__import__
def myimport(name, *a):
if name=='pwd': raise ImportError
return self.savimport(name, *a)
__builtin__.__import__ = myimport
and of course in teardown put things back to normal:
__builtin__.__import__ = self.savimport
Explanation: all import operations go through __builtin__.__import__, and you can reassign that name to have such operations use your own code (alternatives such as import hooks are better for such purposes as performing import from non-filesystem sources, but for purposes such as yours, overriding __builtin__.__import__, as you see above, affords truly simple code).

Why can't I import without getting an error about another python file? ("partially initialized module has no attribute")

I'm trying to import the requests module to get familiar with bs4, but the request module in the file I'm currently working in is grayed out so it isn't being recognized as a module. When I run the almost empty program, I get an error for an unrelated python file within my project.
Should I individually store each python file I make inside of a separate folder?
Both of these files are inside of the same project folder.
import requests
response = get('https://www.newegg.ca/p/N82E16868105274')
print(response.raise_for_status())
Error:
Traceback (most recent call last):
File "C:\Users\Denze\MyPythonScripts\Webscraping learning\beautifulsoup tests.py", line 1, in <module>
import requests
File "C:\Users\Denze\MyPythonScripts\requests.py", line 3, in <module>
res = requests.get('')
AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import)
Process finished with exit code 1
The other code in question that I think is causing my error:
import requests
res = requests.get('')
playFile = ('TestDownload.txt', 'wb')
for chunk in res.iter_content(100000):
playFile.write(chunk)
playFile.close()
You have a name collision. You're not importing the requests library, you're importing your script.
You wanted to do the following with your imports:
MyPythonScripts\beautifulsoup tests.py
→ requests.get() (the library)
What you're doing instead is:
MyPythonScripts\beautifulsoup tests.py
→ MyPythonScripts\requests.py
→ MyPythonScripts\requests.py .get() (the same file again)
That's the "circular import" that is mentioned in the traceback. The module imports itself and tries to use an attribute that isn't there before it finishes "executing", so the interpreter thinks it's due to the unfinished initialization
Raname MyPythonScripts\requests.py to something else and it should work.

Package import failure in Python 3.5

I have the following folder structure:
/main
main.py
/io
__init__.py
foo.py
In Python 2.7 I would write the following in main.py:
import io.foo
or
from io.foo import *
wheareas in Python 3.5 I get an import error:
Traceback (most recent call last):
File "./main.py", line 6, in <module>
import io.foo
ImportError: No module named 'io.foo'; 'io' is not a package
I couldn't find any help so far.
io is a built-in module. Don't name your local packages the same as a built-in module.
While #ErikCederstrand's answer is correct and probably sufficient for you, I was curious as to why it failed so I went digging through cpython's source. So for any future visitors, here's what I found.
The function where it's failing is here: https://github.com/python/cpython/blob/3.4/Lib/importlib/_bootstrap.py#L2207
On line 2209, it checks to see if the parent module has been loaded:
parent = name.rpartition('.')[0] # Value of 'io'
Since it has loaded the builtin io module, it continues on like normal. After the if returns false, it goes on to assign parent module which again is set to "io":
if name in sys.modules:
return sys.modules[name]
parent_module = sys.modules[parent]
The next lines are what cause the failure, and it's because builtin modules (io anyway) don't have a __path__ instance variable. The exception you see raised here are ultimately what you're seeing when you run it:
try:
path = parent_module.__path__
except AttributeError:
msg = (_ERR_MSG + '; {!r} is not a package').format(name, parent)
raise ImportError(msg, name=name)
If you change your module name like Erik says, then step through this whole process, you can see the call to get parent_module.__path__ works like it's supposed to and everything's happy.
So, tldr: you've tricked the import system into thinking it's already loaded your custom module, but when it goes to try and use it like a custom module it fails because it's actually the builtin io.
EDIT: It looks like __path__ is set here after it goes through a normal import process in init_module_attrs:
if _override or getattr(module, '__path__', None) is None:
if spec.submodule_search_locations is not None:
try:
module.__path__ = spec.submodule_search_locations
except AttributeError:
pass

ImportError: cannot import name SlotMap

I am encountering an import error
Traceback (most recent call last):
File "C:\Users\bartis\Desktop\Python\TEC-KB\SlotMapper.pyw", line 9, in <module>
from SlotMapper import SlotMap
File "C:\Users\bartis\Desktop\Python\TEC-KB\SlotMapper.pyw", line 9, in <module>
from SlotMapper import SlotMap
ImportError: cannot import name 'SlotMap
This should be a straightforward issue, but I can’t seem to find the problem. If I place the SlotMapper.py file in the same directory as the GUI I am using the import of SlotMap occurs without error. If I move the file to a directory under the current working directory and add - sys.path.append(os.path.join(os.getcwd(), 'appLib')) I receive the error above. See import statements and modification of PYTHONPATH below. I know the PYTHONPATH has been modified after I checked it from the debugger. I also know since there are other files under appLib required for the GUI to operate. Finally, I have checked all of the imported files for a circular reference and find none… So stuck. Any suggestions welcome
import os
import sys
sys.path.append(os.path.join(os.getcwd(), 'appLib', 'KB-GUI'))
sys.path.append(os.path.join(os.getcwd(), 'appLib'))
from tkinter import *
from SlotMapper import SlotMap
from ShelfTypeSelection import ShelfTypeSelector
from PackTypeSelection import PackTypeSlotMappingSelector
from EntryWidgets import EntryBase, ShelfSlotEntry
The reason this is not working is because your file is named SlotMapper.pyw. The line
from SlotMapper import SlotMap
is trying to import SlotMap from your current file, hence the error. Try renaming your file to slotmapper_test.pyw or something like that, and everything should work as expected. You don't want your code files to have the same names as any modules you're trying to import, as the import mechanism will try to find the classes/functions there first, instead of searching your modules first.

Categories