Wierd attribute error with importing into directories - python

I have a directory tree as follows:
main.py
dir1
sub1.py
sub2.py
In main.py:
import dir1.sub1
In dir1/sub1.py:
def f1() -> None:
print("f1")
import dir1.sub2
dir1.sub2.f2()
In dir1/sub2.py:
import dir1.sub1
def f2() -> None:
dir1.sub1.f1()
print("f2")
When I run main.py, I get the following error message:
Traceback (most recent call last):
File "...\main.py", line 1, in <module>
import dir1.sub1
File "...\dir1\sub1.py", line 7, in <module>
dir1.sub2.f2()
File "...\dir1\sub2.py", line 5, in f2
dir1.sub1.f1()
AttributeError: module 'dir1' has no attribute 'sub1'. Did you mean: 'sub2'?
(Where the ... at the beginning of the file path is my working directory.)
If I change main.py to
import dir1.sub2
I get a slightly different error message:
Traceback (most recent call last):
File "...\main.py", line 1, in <module>
import dir1.sub2
File "...\dir1\sub2.py", line 1, in <module>
import dir1.sub1
File "...\dir1\sub1.py", line 7, in <module>
dir1.sub2.f2()
AttributeError: module 'dir1' has no attribute 'sub2'
If I move sub1.py and sub2.py to the same directory as main.py and re‐direct imports as necessary, I get the expected output of
f1
f2
Why does this happen, and how can I make it not happen?

You need to use absolute import because Python 3 only supports that. In Python 2 your method will work. So for example if you have import dir1.sub2 change it to from dir1 import sub2. See here.
Note: I've tested it with your setup and it works.

Related

importing other file and using functions from main

Note: I have reduced my problem so the code is only a few lines (compared to 600)
I have a problem: from main.py I want to import file slave.py. slave.py references a function from main.py, and of course I get a NameError: name 'funcFromMain' is not defined
Here is my code for main.py:
import slave
def funcFromMain():
return 6
print(slave.funcFromSlave())
And here is my code for slave.py:
def funcFromSlave():
one = funcFromMain() # <- this doesn't work
two = 2
return (one + two)
I am getting exact error: (note that both files are in exactly the same directory)
Traceback (most recent call last):
File "C:\Users\PrinceOfCreation\Documents\test\main.py", line 6, in <module>
print(slave.funcFromSlave())
File "C:\Users\PrinceOfCreation\Documents\test\slave.py", line 2, in funcFromSlave
one = funcFromMain()
NameError: name 'funcFromMain' is not defined
I tried adding import main at the top of slave.py, and got the following error:
Traceback (most recent call last):
File "C:\Users\PrinceOfCreation\Documents\test\main.py", line 1, in <module>
import slave
File "C:\Users\PrinceOfCreation\Documents\test\slave.py", line 1, in <module>
import main
File "C:\Users\PrinceOfCreation\Documents\test\main.py", line 6, in <module>
print(slave.funcFromSlave())
AttributeError: module 'slave' has no attribute 'funcFromSlave'
With from slave import funcFromSlave instead at the top of main:
Traceback (most recent call last):
File "C:\Users\PrinceOfCreation\Documents\test\main.py", line 6, in <module>
print(funcFromSlave())
File "C:\Users\PrinceOfCreation\Documents\test\slave.py", line 2, in funcFromSlave
one = funcFromMain()
NameError: name 'funcFromMain' is not defined
First you can't import a python module like this :
import slave.py
It must be
from slave import funcFromSlave # to get the funcFromSlave function from slave script
And you need to make sure that the slave.py is in the same directory of main.py or
you need to precise the subdirectory where slave.py exists
And for the later error, its best if you avoid circular imports, cause it will create problems, best to do is to send the value of funcFromMain() to funcFromSlave
main.py :
from slave import funcFromSlave
def funcFromMain():
return 6
print(funcFromSlave(funcFromMain()))
slave.py :
def funcFromSlave(funcFromMain):
one = funcFromMain
two = 2
return (one + two)
output when running main.py :
8

python reload module for beginner. importlib.reload doesn't seem to work

I have a file called skdb and class called skmysqldb. I am trying to force reload.
I tried reloading "skdb", "skdb.skmysqldb" "skmysqldb" and none of them seem to work.
>>> from skdb import skmysqldb
>>> importlib.reload(skdb.skmysqldb)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'skdb' is not defined
>>> importlib.reload(skmysqldb)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\importlib\__init__.py", line 139, in reload
raise TypeError("reload() argument must be a module")
TypeError: reload() argument must be a module
>>> importlib.reload(skdb)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'skdb' is not defined
When you import some object using the from <module> import <obj> syntax as in
from skdb import skmysqldb
the module itself is not added to the current namespace, hence why you get a NameError when you try to do reload(skdb).
Instead try:
import skdb
importlib.reload(skdb)
Be cautious when using reload. If the module your reload imports other modules, those modules are not reloaded recursively, so depending on the exact code you can wind up in a rather broken state where it's better to just restart the whole interpreter.
I don't think this is supported, but try doing del sys.modules['mymodule'] for everything that vaguely matches. To find relevant ones, try something like [x for x in sys.modules if 'mymodule' in x].

ImportError: cannot import name (not a circular dependency)

I have the problem on importing the class in the same package, and it seems not a circular dependency problem. So I'm really confused now.
my-project/
lexer.py
exceptions.py
I declared an exception in exceptions.py and wants to use it in lexer.py:
exceptions.py:
class LexError(Exception):
def __init__(self, message, line):
self.message = message
self.line = line
and in lexer.py:
import re
import sys
from exceptions import LexError
...
It shouldn't be circular dependency since lexer.py is the only file has import in it.
Thanks!!
exceptions conflicts with builtin module exception.
>>> import exceptions
>>> exceptions.LexError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'LexError'
>>> from exceptions import LexError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name LexError
Use different module name.

Import error python

In Python, when I use this import statement breze.learn.mlp import iter_minibatches, am getting the following errors.
Here iter_minibatches is a function defined in mlp.py.
Traceback (most recent call last):
File "/home/vinod/PycharmProjects/MLPonTheano/MLPbreze.py", line 15, in <module>
from breze.learn.mlp import Mlp, FastDropoutNetwork
File "/home/vinod/breze/breze/learn/mlp.py", line 22, in <module>
from breze.learn.base import SupervisedModel
File "/home/vinod/breze/breze/learn/base.py", line 21, in <module>
from breze.learn.mlp import iter_minibatches
ImportError: cannot import name iter_minibatches
You have a circular import; mlp imports base imports mlp:
# executing mlp.py
File "/home/vinod/breze/breze/learn/mlp.py", line 22, in <module>
from breze.learn.base import SupervisedModel
# executing base.py
File "/home/vinod/breze/breze/learn/base.py", line 21, in <module>
# this tries to import from mlp again, but mlp isn't done yet
from breze.learn.mlp import iter_minibatches
Any line after the from breze.learn.base import SupervisedModel will not yet have been executed so importing any object defined by those lines will fail.
Avoid circular imports, or if you must have them, delay importing in one of the modules to make sure the objects you need in the other are defined.

Circular intra-referencing submodules

Words
The structure is as follows: a module test contains two submodules test.foo and test.bar.
test.foo has a function inc() that uses test.bar.bar() so based on the python documentation from . import bar is the proper way to include that, and this works as expected.
test.bar however, also has a function inc2 that uses test.foo.foo, but when from . import foo is used, both of these modules break.
What is the correct method for achieving this? I've found little in the python docs or searching.
Code
test/_init_.py
#empty
test/foo.py
from . import bar
def foo():
print("I do foo")
def inc():
print(bar.bar())
test/bar.py
from . import foo
def bar():
print("I do bar")
def inc2():
print(foo.foo())
Error 1
>>> import test.foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test/foo.py", line 1, in <module>
from . import bar
File "test/bar.py", line 1, in <module>
from . import foo
ImportError: cannot import name foo
Error 2
>>> import test.bar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test/bar.py", line 1, in <module>
from . import foo
File "test/foo.py", line 1, in <module>
from . import bar
ImportError: cannot import name bar
The solution is to factor out code needed by both modules into a third module which is imported by both. For instance, put the foo function into a third module.
There are many previous StackOverflow questions about this, e.g., Circular import dependency in Python . See also http://effbot.org/zone/import-confusion.htm#circular-imports .

Categories

Resources