Reputation: 93
I am having an import problem. What I am trying to do is to import 2 functions with the same name from modules with the same name. Note that i am doing this in Pycharm.
I have the following directory structure:
test_import
|--foo.py
|--main.py
|--test
|--foo.py
Code
foo.py
in the test_import folder looks like:
def print_arg(x):
print('print 1: {}'.format(x))
foo.py
in the test folder looks like:
def print_arg(x):
print('print 2: {}'.format(x))
Here is my code in main.py
in which I am doing the imports of print_arg
:
import sys
from foo import print_arg as print_arg
print_arg(1)
sys.path.insert(1, './test')
from foo import print_arg as print_arg_2
print_arg(1)
print_arg_2(1)
I expect this to print
print 1: 1
print 1: 1
print 2: 1
But it prints
print 1: 1
print 1: 1
print 1: 1
Somehow the second import does not work and print_arg_2
becomes a reference to print_arg
. This is illustrated by doing the imports the other way around:
sys.path.insert(1, './test')
from foo import print_arg as print_arg_2
print_arg_2(1)
sys.path.pop(1)
from foo import print_arg
print_arg(1)
print_arg(1)
print_arg_2(1)
which prints:
print 2: 1
print 2: 1
print 2: 1
Changing the function name in test/foo.py to print_arg_2
does not work, it results in an error. It seems like a reference to foo.py
in the project folder has been created and it tries to import from there instead of looking in other directories on sys.path
Traceback (most recent call last):
File "C:\Users\jeroe\AppData\Local\Programs\Python\Python37\lib\site-packages\IPython\core\interactiveshell.py", line 3326, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-10fe80aec78f>", line 5, in <module>
from foo import print_arg_2 as print_arg_2
ImportError: cannot import name 'print_arg_2' from 'foo' (C:\Users\jeroe\PycharmProjects\test_import\foo.py)
Changing the filename of foo.py
in the test folder to e.g. foo2.py
does work. However I prefer not to change the filename.
So I have 2 questions:
Can somebody explain me what is going on here?
What is a better way to import these 2 functions without having to change the file (module) name?
Upvotes: 2
Views: 1881
Reputation: 1129
First of all, you have to add an empty __init__.py
file inside the test folder, so the second foo file can be imported.
Secondly, you have to write the full relative path to the second file when importing it. Right now, you are importing both times the first foo file.
Just modify the second import line to:
from test.foo import print_arg as print_arg_2
Upvotes: 2