Python Forum

Full Version: About [from FILE import FUNC]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've simplified what I observed to the smallest example I could. Here it is:

Here are two .py files a1.py and a2.py,

a1.py:
from a2 import bob
cobe = 5
a2.py:
from a1 import cobe
bob = 4
On running a1.py, I get the following error:
Output:
:Traceback (most recent call last): File "FILE DIRECTORY", line 1, in <module> from a1 import cobe File "FILE DIRECTORY", line 1, in <module> from a2 import bob File "FILE DIRECTORY", line 1, in <module> from a1 import cobe ImportError: cannot import name 'cobe'
HOWEVER!
If I modify a1 to become
from a2 import *
cobe = 5
I get no errors.

Why does this happen!?

And what am I supposed to do when I want to import a function from a file to my main file, if the file also imports my main file?
Using from file import * seems to work, but I don't want to use *.
Start to test with a more normal scenario,where you import from a file.
Not like confusing import in both files.
# a2.py
bob = 4
def foo():
    return 42
# a1.py
from a2 import bob, foo

print(f'bob vaule {bob} foo vaule {foo}')
Output:
bob vaule 4 foo vaule <function foo at 0x03F58858>
So using function would be.
from a2 import bob, foo

print(f'vaule bob + foo() is: {bob + foo()}')
Output:
vaule bob + foo() is: 46
snippsat, I do know how to import? But I wanted to know what caused my observation and the alternative.
(Apr-21-2019, 07:51 AM)Nwb Wrote: [ -> ]Using from file import * seems to work, but I don't want to use *
Your observation is right never use *.
from a2 import *

print(f'vaule bob + foo() is: {bob + foo()}')
Output:
vaule bob + foo() is: 46
There is no way to now if looking at import that bob and foo comes from a2.
Think if you see foo() alone in line 50,then look import and see * dos it come from a2 Confused
There could also be ten more class/function/variable name imported into global namespace when using *.
from a2 import bob, foo

print(f'vaule bob + foo() is: {bob + foo()}'
Output:
vaule bob + foo() is: 46
Here see that bob and foo comes from a2.
It will only import bob and foo and not extra class/function/variable name.
I don't think you understood my thread snippsat.

Try running the example I have given.
Why does from a2 import bob work and
from a2 import * not work?

They are supposed to do the same thing right?
It's the circular import that mess is up,comment out like this and it work.
I guess is goes back to a1 and import cobe,then bob is not found.
Just don't do stuff like this,it's not the normal way you work with import in a module/package environment.
# a2.py
#from a1 import cobe
bob = 4 
print(bob)
λ python a1.py
4
Why is it not circular import when I use
from a2 import *

Why does that work??
(Apr-21-2019, 01:22 PM)Nwb Wrote: [ -> ]from a2 import *

Why does that work??
Not quite sure,guess that import all * dos not block the finding of bob.
I would never make this problem for myself when make a module/package.
Think i understand module/package field quite well,has a tutorial about it Packaging/Modules--Wheel--pip--setup.py--Freeze.