1

This type of problem had me stuck last night for longer than it should have, so gonna throw the answer out there since I couldn't find this particular error on stack.

Say I have two files. my_function.py and variable.py

In my_function.py

from variable import a

def double(n):
    return 2 * n

print(double(a))

In variable.py

from my_function import double

a = 4

I get an import error? How come? I had an empty init.py, they were in the same directory, and I had checked that I hadn't made a typo many times.

To help identify if you have this issue, your traceback should point to two different imports.

Traceback (most recent call last):
  File "/Users/Owner/Documents/Stack Questions/my_function.py", line 1, in <module>
    from variable import a
  File "/Users/Owner/Documents/Stack Questions/variable.py", line 1, in 
<module>
    from my_function import double
  File "/Users/Owner/Documents/Stack Questions/my_function.py", line 1, in <module>
    from variable import a
ImportError: cannot import name 'a'
David Jay Brady
  • 1,034
  • 8
  • 20

3 Answers3

1

The import statements are executed at the moment they are encountered by the interpreter. When reaching from my_function import double in variable.py, the interpreter hasn't reached the definition of a yet, so the global scope of the variable module doesn't contain that name yet. When evaluating the body of the my_function module, the interpreter hits the import statement for variable first. This module already exists in sys.modules, so it is taken from there (modules are only imported once). However, this module is only partially initialized, so it doesn't contain the name a you are trying to import yet, and you get an ImportError.

It is possible to make circular imports work, and there are some circumstances where they are the lesser evil. However, they tend to be fragile, so you need to be very careful about import order and other subtleties. If possible, avoid them.

In your traceback, it appears like the interpreter is actually trying to import my_function.py twice. The reason for this is that it is first imported under the mdoule name __main__, and then under the name my_function for a second time.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
0

I have written two files that depend on each other. When my_function.py wants to get a, variable.py wants to get double. They both can't happen.

To solve, avoid writing modules that depend on each other. In my case, I solved this by passing the variables as parameters, instead of imports. Once I did not have the files importing from each other, but only from one file to another, my issues were solved.

David Jay Brady
  • 1,034
  • 8
  • 20
0

Try:

import variable

def double(n):
    return 2 * n

print(double(variable.a))

And:

import my_function

a = 4

Then, in terminal:

python variable.py

But, do you really need circular import?

Look: Python circular importing?

Community
  • 1
  • 1
  • Note that `python my_function.py` still doesn't work (for rather involved reasons). – Sven Marnach Feb 14 '16 at 18:13
  • Appreciate the answer, but note that I posted my own answer right after asking the question. I had the problem last night and already solved it, and posted it so other people get past it quicker – David Jay Brady Feb 14 '16 at 18:44