I have imported a file common.py
into the copyPasteAnywhereTest.py
file. common.py
has some common functions defined in it that I need to call in the current file viz. copyPasteAnywhereTest.py
. But there is one particular function copyText()
that I have defined in both the files. By default, copyText()
from common.py
is being called. I want to call the function that I have defined locally and not the one that I defined in the imported file. The code is something like below:
This is one file common.py
#common.py
def copyText():
#Function definition
#Some more functions defined in this file.
This is the script file copyPasteAnywhereTest.py
#copyPasteAnywhereTest.py
import os
import sys
sys.path.append(os.path.abspath("../z_common/"))
import common
def main():
#some code
copyText() #Calling the copyText() function
def copyText():
#Code here.
copyText()
from common.py
was called whether I imported using import common
or from common import functionName
The simplest solution was to change the name of copyText()
in copyPasteAnywhereTest.py
and call the same. But I want to know the proper solution and not a workaround.
Just to be clear, I had't even imported the copyText()
function in copyPasteAnywhereTest.py
(i.e., from common import copyText
) earlier while using the from module import function
syntax. I have just imported the required functions using from common import *functionName*
.
P.S. - I am quite new to Python. Don't mind if the question is a stupid one. I've tried googling and searching over the internet but I couldn't find an answer. Hence, the question.
Rather than importing like this:
from common import copyText
do
import common
and in your code prefix it with the module name and a dot:
result = common.copyText()
By importing just the module, and referring to its contents using dotted notation you prevent these name collisions in your modules namespace.