Bizarre inconsistent python ImportError - possible circular dependency? - python

I'm trying to refactor some python code and I'm stuck with an import error I don't understand. I suspect there might be a circular dependency somewhere but I don't see it, and I'm not getting much in the way of hints from the error messages. The codebase is large, but there are two modules of interest here:
radian/models.py defines a class called ACount
datalayer/radian.py has the following line in it:
from radian.models import ACount
When I run the code (either interactively or from the main program) the imports fail in a way that doesn't make sense to me.
>>> from radian.models import ACount
>>> import datalayer.radian
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/leopd/dev/dbproj/datalayer/radian.py", line 10, in <module>
from radian.models import ACount
ImportError: No module named models
My best guess is that there's a circular dependency somewhere -- that radian is importing something that imports datalayer. But I don't see it. And the error message doesn't make any sense to me. Any ideas what's going on?
-- UPDATE --
I'm using python 2.6.1 on Mac. The __init__.py files have some code in them, but they only import from standard python packages.

Any module in the datalayer folder (including radian.py), when it sees from radian, will assume that datalayer/radian.py is the relevant module. You might need to do
from __future__ import absolute_import
in datalayer/radian.py and other similarly affected modules, and then check all your imports to ensure that they're absolute. You may be able to get away with renaming datalayer/radian.py and the imports which reference it, depending on where that module is referenced from.

Related

Why can't Idle import from .py files created after it was launched?

I can't explain this phenomena, other than to assume that Idle somehow works off of a snapshot of the filesystem, taken at launch-time.
Repro steps:
create a myLib.py file with (e.g.):
#!/usr/bin/env python3
pre_launch_str = "Pre-launch!"
# post_launch_str = "Post-launch!"
launch Idle (from the containing folder)
from myLib import pre_launch_str works as expected: the string is imported/usable
Keep Idle running/open
[from another application/terminal] modify myLib.py to include a new object (e.g.) post_launch_str
from myLib import post_launch_str will throw an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'post_launch_str' from 'myLib' (/home/myUser/myLib.py)```
Anyone know what the cause of this is?
Linux (zsh) + Python 3.10 inthe example above, but I've noticed this long ago (~Python3.5 and on MacOS too)
This has nothing to do with IDLE. When you've already put a module into scope (regardless of which names you imported from it), Python will never reload that module. It assumes the module's code has not changed and will use the in-memory version.
If you're planning to modify the module live during development, you'll need to use importlib to reload it. You need a reference to the module itself. If you imported the module name as import myLib, then myLib will do. Otherwise, take a class or function that you imported and get its __module__ (so pre_launch_str.__module__ in your example). Then take that and call reload on it.
from importlib import reload
reload(pre_launch_str.__module__)

python relative import from same package fails

I am working on a project involving the popular aiortc/aioquic github package. Located at src/aioquic/buffer.py there is an import statement:
from ._buffer import Buffer, BufferReadError, BufferWriteError # noqa
This line is meant to import a C/stub module _buffer.c / _buffer.pyi . When used from the root directory to run examples found in the examples directory this import line will work, but when trying to run in a Docker container from the same directory this import fails and when importing from the /src directory the import fails. I have tried reading several other questions on python relative imports but none seem to make sense to me.
To reproduce the issue:
open python in the src directory
from aioquic import about (works)
from aioquic import buffer (fail)
from aioquic import buffer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/aioquic/src/aioquic/buffer.py", line 1, in <module>
from ._buffer import Buffer, BufferReadError, BufferWriteError # noqa
ModuleNotFoundError: No module named 'aioquic._buffer'
I don't understand why the examples http3_client and http3_server work but when trying to import it directly it fails. I thought I understood roughly how to use python imports but apparently not. What would cause the same line to work when running from one directoy but fail when running from another directory? Is it something magical with the PYTHONPATH? I'm hoping someone smarter than I am can explain why this is failing.
[EDIT and most importantly how to fix this so the import will work nomatter where the user runs python from. I have tried several different combinations but none seem to work.]
Thanks in advance.
I figured out what was wrong.
The stub/C module had to be built into a .pyd file before it could imported. This was done by
pip install -e .
on the parent directory
Is it something magical with the PYTHONPATH?
Yes.
Typically . dot (current working directory) will appear
in that exported env var,
so where you started matters.
Take care to ensure you're using the same env var setting,
and the same pwd,
during both the interactive and the Docker runs.

pybedtools: ImportError: cannot import name scripts

I am trying to import Pybedtools in Spyder.
from pybedtools import BedTool
This is the error I am getting:
Traceback (most recent call last):
File "<ipython-input-13-7a8ea5d1dea8>", line 1, in <module>
from pybedtools import BedTool
File "/Users/michaelsmith/anaconda2/lib/python2.7/site-packages/pybedtools/__init__.py", line 9, in <module>
from . import scripts
ImportError: cannot import name scripts
I just downloaded Anaconda and there doesn't seem to be a reason as to why this happens. What is the typical protocol for resolving bugs like this?
UPDATE:
So within my pybedtools folder there is a scripts folder (which is presumably the module we're trying to import). I changed both the command within __init__.py to:
from . import scripts2
and changed the name of the folder to scripts2 as well. However, I still get the error as such:
ImportError: cannot import name scripts2
So I must be doing something wrong here, which module should I be renaming exactly? Sorry if this is a silly question, I am quite new to python.
This is caused because Anaconda has a module named scripts and therefore your import is "shadowed" by that module. You can double check that when you call import scripts in a new notebook it works even if you have never defined such a module. A very good explanation of import traps can be found here:
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html
A workaround would be to rename the script module of pybedtools to something else and also change all the imports to the new name.

ImportError: No module named anorm

I got the below shown error. Can anyone please help me with a solution?
from common import anorm, getsize
Exception:
Traceback (most recent call last): File "<pyshell#10>", line 1, in <module>
from common import anorm, getsize
ImportError: cannot import name anorm
Sorry for necroposting, but for anyone who's stuck on this:
apparently, you're trying to run the opencv example somewhat like this one. You need to copy the common.py from the same directory as well and everything would be fine (no need to install common package which has nothing to do with this partucular problem).
The common module does not contain an importable item named anorm. Simple as that.
Some possible reasons why:
The common module was not installed correctly.
Misspelling of the name of the item to be imported.
A file named common.py exists in the current directory, which takes precedence over the real common module (wherever it is).
Circular imports (which don't appear to be the case here, given the error traceback).

Nameclash when importing packages that has the word 'django' in the name?

I have a somewhat odd problem. I decided to rename an entire branch of my package from
foo.bar.somemodule
to
foo.django.bar.somemodule
The problem is after this is done, I get the following error:
Traceback (most recent call last):
File "/home/workspace/eclipse/foo/src/foo/manage.py", line 2, in <module>
from django.core.management import execute_manager
ImportError: No module named core.management
If I now, revert the name to
foo.djangox.bar.somemodule
IT WORKS! Notice, the 'x' I added to the word django.
It seems there are some kind of name clash when using foo.django.bar.somemodule, but What gives? They should be separate from django itself.
All the imports in my code are of the form
from foo.django.bar.somemodule import someobject
import foo.django.bar.somemodule
edit: to clarify there is an 'x' in the second to last import
You're running into a situation where you want to perform an absolute import, but your Python version doesn't do them by default. Add from __future__ import absolute_import at the top of the afflicted file to tell the Python VM to activate it.

Categories

Resources