Python Forum
About [from FILE import FUNC]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
About [from FILE import FUNC]
#1
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 *.
Reply
#2
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
Reply
#3
snippsat, I do know how to import? But I wanted to know what caused my observation and the alternative.
Reply
#4
(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.
Reply
#5
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?
Reply
#6
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
Reply
#7
Why is it not circular import when I use
from a2 import *

Why does that work??
Reply
#8
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,813 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Import XML file directly into Excel spreadsheet demdej 0 837 Jan-24-2023, 02:48 PM
Last Post: demdej
  How to output one value per request of the CSV and print it in another func? Student44 3 1,323 Nov-11-2022, 10:45 PM
Last Post: snippsat
  How from sklearn.datasets import load_diabetes change to import to CSV file Anldra12 0 1,850 Dec-25-2021, 07:20 PM
Last Post: Anldra12
  How to import file and function in another folder SriRajesh 1 3,150 Dec-18-2021, 08:35 AM
Last Post: Gribouillis
  Python - Import file sequence into Media Pool jensenni 1 2,126 Feb-02-2021, 05:11 PM
Last Post: buran
  Func Animation not displaying my live graph jotalo 0 1,551 Nov-13-2020, 10:56 PM
Last Post: jotalo
Smile Import error with local file colt 1 1,922 Nov-08-2020, 07:56 AM
Last Post: Gribouillis
  Trying to write func("abcd") -> "abbcccdddd" omm 8 4,087 Oct-24-2020, 03:41 AM
Last Post: bowlofred
  Code import .CSV file to MySQL table rtakle 4 2,847 Apr-30-2020, 03:16 PM
Last Post: anbu23

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020