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
Related
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.
I created two files in python
First one is
mym.py
def hello():
print("Hello everyone")
return
def summ(x,y):
total=x+y
return total
and next one is
abc.py
import mym
hello()
x=summ(3,4)
print(x)
And the error msg which I am getting is...both the files are in same working directory and there is no error of module not found...its giving error of function not defined.
Traceback (most recent call last):
File "C:/Users/Nisha/AppData/Local/Programs/Python/Python39/abc.py", line 3, in <module>
hello()
NameError: name 'hello' is not defined
Traceback (most recent call last):
File "C:/Users/Nisha/AppData/Local/Programs/Python/Python39/abc.py", line 3, in <module>
x=summ(3,4)
NameError: name 'summ' is not defined
What is the problem in function definition I am unable to trace...
The abc.py needs to be changed to:
from mym import *
hello()
x=summ(3,4)
print(x)
Otherwise you cannot access the functions.
You can try like this:
import mym
mym.hello()
x = mym.summ(3,4)
print(x)
I use python 3.6, and I am trying to play an audio file with pyo, but when I try to run it, I get this message;
Traceback (most recent call last):
File "C:\Python27\pyotest.py", line 1, in
from pyo import *
File "C:\Python27\pyo.py", line 2, in
NameError: name 'Server' is not defined
My code:
from pyo import *
s = Server().boot()
s.start()
sf = SfPlayer("C:\Users\*****\Music\sound.mp3", speed=1, loop=True).out()
Looks like you have created the file C:\Python27\pyo.py. So instead of the actual pyo module getting imported, your file gets imported.
Rename the file C:\Python27\pyo.py and any pyc file (C:\Python27\pyo.pyc) associated with it and try again
I have a file that gets generated by :
excerpt:
group0 = ['ParentPom']
group1 = ['Commons','http', 'availability','ingestPom','abcCommons','solrIndex','123Service']
...
group10=['totalCommons','Generator']
How can I include this in my python script, tried import but no luck
>>> import dependencies_custom
>>> print (group2[0])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'group2' is not defined
In the import form you're using, you should be able to access the groups by
dependencies_custom.group2[0]
type notation. If you want just use just group2[0] notation, try using:
from dependencies_custom import *
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)