I have a class that I named Ui_Materials defined in materialsFrame.py When I run the following import in the given file:
from common.interface.interface import ShowHide
I get the following traceback:
Traceback (most recent call last):
File "./main.py", line 110, in <module>
main()
File "./main.py", line 91, in main
interfaceObj.showMaterials()
File "/home/mohsen/codes/amlak/amlak/src/common/interface/interface.py", line 80, in showMaterials
self.ui = Ui_Materials()
NameError: global name 'Ui_Materials' is not defined
Notes:
I have a function in interfaces that uses Ui_materials
When I comment import line, everything is OK.
Question: How can I solve my problem? I need to import line.
You need to import module that contains Ui_Materials class into ShowHide module.
Be aware that in python you can't perform circular import!! (if first module imports second one, second module must not import first one; also, if first imports second, and third imports first, that means that first and second must not import third one)
Related
I try to call from main.py a function in router.py that will call a function in calculator.py that will try to modify a variable in main.py
Something that in C# is achieved for example by creating a public static variable, I don't know how to achieve it in Python.
This is my file main.py:
import router
# create a table
global mytable
mytable = []
router.CallAnotherFunction("testing data")
This is my file router.py:
import calculator
def CallAnotherFunction(sample_data):
calculator.ModifyMainTable(sample_data)
This is my calculator.py:
import main
def ModifyMainTable(sample_data):
main.mytable = sample_data
This is the error I'm getting just when trying to execute router.CallAnotherFunction("testing data"):
AttributeError
partially initialized module 'router' has no attribute 'FindCorrectRoute' (most likely due to a circular import)
File "C:\Users\dimitri\Documents\Upwork\Footyamigo\main.py", line 37, in <module>
router.FindCorrectRoute(odd, next_match, local_data, visitor_data)
File "C:\Users\dimitri\Documents\Upwork\Footyamigo\calculator.py", line 3, in <module>
import main
File "C:\Users\dimitri\Documents\Upwork\Footyamigo\router.py", line 1, in <module>
import calculator
File "C:\Users\dimitri\Documents\Upwork\Footyamigo\main.py", line 5, in <module>
import router
What I really need is that mytable is accesible from all files and if I change it's value from any file I have to see the changes reflected on the others, I don't want to have different instances of the object.
Just solved it import __main__ as main inside calculator.py
import tailer
test = tailer.tail(open("test.txt"), 1)
#print(lines[1])
It's as simple as the code above, but it doesn't work.
(I saved it because it was successful once during the experiment, but an error occurs when I run it again later.)
Error content:
Traceback (most recent call last):
File "c:\Users\user\Documents\VSCODE\python\V1\tailer.py", line 1, in <module>
import tailer
File "c:\Users\user\Documents\VSCODE\python\V1\tailer.py", line 3, in <module>
test = tailer.tail(open("test.txt"), 1)
AttributeError: partially initialized module 'tailer' has no attribute 'tail' (most likely due to a circular import)
Looks like your file is called tailer.py, so when it does import tailer, it tries to load itself, which is usually a recipe for confusion.
You named your program tailer.py. When you do an import tailer the local folder has priority over all other folders and you will import tailer.py again. Creating an import circle.
In other words: you have a name clash between your program and the library you are trying to import. Just rename the file to something else and try again.
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
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.
I have a directory structure that looks like this:
scripts/
__init__.py
filepaths.py
Run.py
domains/
__init__.py
topspin.py
tiles.py
hanoi.py
grid.py
I would like to say:
from scripts import *
and get the stuff that is in filepaths.py but also get the things that are in hanoi.py
The outer __init__.py contains:
__all__ = ['filepaths','Run','domains','hanoi']
I can't figure out how to get the inner files to be included in that list. Putting hanoi by itself gets this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'hanoi'
Putting domains.hanoi gets this error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'domains.hanoi'
The last reasonable guess I could come up with is putting scripts.domains.hanoi which gets this error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'scripts.domains.hanoi'
How do you get the all list to include things that are in subdirectories?
In scripts/__init__.py, before the __all__ add the following
from domains import topspin, tiles, hanoi, grid
This will add those modules to the namespace, and you will be able to import them with
from scripts import *
Note
As a soapbox, it is preferred to do things like
from scripts import topspin, tiles, hanoi, grid, filepaths, Run
over
from scripts import *
because 6 months from now, you might look at hanoi on the 400th line of code and wonder where it came from if you use the * import style. By explicitly showing what is imported from scripts it serves as a reminder where things come from. I'm sure that anyone trying to read your code in the future will thank you.
Import them first, in the __init__ files.
In scripts/__init__.py, import at least domains, and in scripts/domains/__init__.py import hanoi, etc. Or import domains.hanoi directly in scripts/__init__.py.
Without importing these, the scripts/__init__.py module has no reference to the nestend packages.