1

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.

Mayank Choudhary
  • 376
  • 2
  • 7
  • 18
  • @BlackBear: I just used it to be specific. I didn't want to import `copyText()`. Thanks. Will keep your point in mind. – Mayank Choudhary Apr 19 '16 at 10:35
  • 2
    I cannot reproduce this. Please post a simple [mcve] that demonstrates your problem. – PM 2Ring Apr 19 '16 at 10:42
  • `from common import functionName`, will **only** import `functionName`, it will _not_ import any other names from the `common` module. In particular, it will not import the `copyText` from `common`. And if you have `from common import copyText` and then later in your script you have `def copyText():` the name `copyText` will be bound to your local function, not the imported one. But if you do `def copyText():` _before_ you do `from common import copyText` (which you should not do) then the name `copyText` will be bound to the function imported from `common`. – PM 2Ring Apr 19 '16 at 11:04
  • @PM2Ring: I have made changes to my question and I think that should help. Please note that while using `from _module_ import _function_` I never actually imported `copyText()` from `common.py`. That's why it's all the more confusing. – Mayank Choudhary Apr 20 '16 at 03:42
  • 1
    Thanks for posting the example code. I can't reproduce the problem you describe (by calling main()). Are you reassigning the name copyText anywhere? How do you know the wrong function is being called? – snakecharmerb Apr 20 '16 at 05:04
  • @snakecharmerb - I am not reassigning the name copyText anywhere. And how the wrong function is being called is what I want to know. – Mayank Choudhary Apr 20 '16 at 05:18
  • 1
    Sorry the second part of my comment wasn't clear. I meant to ask, have you verified that `common.copyText` is really being called, for example by adding different `print` statements to each function? – snakecharmerb Apr 20 '16 at 05:32
  • 1
    @snakecharmerb - `common.Text()` copies text from a certain range in my AUT. But in the script `copyPasteAnyywhereTest.py` I want text from a different range to be copied. Hence, I defined a separate local function with the same name. But while running the script, I noticed that text from the same range was being copied common to other scripts which was not what I wanted. Changing the name of the local `copyText()` and and calling the same produced the desired results. This is how I deduced that `common.copyText()` was being called. – Mayank Choudhary Apr 20 '16 at 08:35

1 Answers1

2

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.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
  • Thank you for your suggestion. Will keep it in mind. As far as the solution to my problem is concerned. I had initially used `import common` only. It didn't work and I changed it to `from common import *functionName*`. I tried it again just now (just to be sure) and it didn't work for me. I don't want `common.copyText()` to be called. Is there something like `this.copyText()` that I can use? – Mayank Choudhary Apr 19 '16 at 10:48
  • 2
    Please add an [mcve] to your question so that we can reproduce your problem. – snakecharmerb Apr 19 '16 at 10:52
  • @MayankChoudhary: If you import with just `import common`, than common's `copyTest` will not overwrite the one defined in the current module, so you can just do `copyText` for this one, and `common.copyText` for the one in `common`. – zondo Apr 19 '16 at 10:54
  • 1
    When you do "from module import function" you are explicitly requesting to overwrite the local namespace for the object named 'function'. You can only get a pointer to the local function by importing it again, you could use my_local_copyTExt. = imp.load_source('module.name', '/path/to/file.py'). Anyway you should rethink your application, because whatever you are trying to achieve , looks hard to understand. – João Pinto Apr 19 '16 at 10:59
  • @snakecharmerb - I have made changes to my question. Please have a look. – Mayank Choudhary Apr 20 '16 at 03:45
  • @JoãoPinto - I had mentioned in the question, I wasn't using `from common import copyText`. Now that I'm using `import common` (following suggestions offered), the problem persists. Can you help me with it please? – Mayank Choudhary Apr 20 '16 at 03:49
  • I can repro the same behavior. If you just use "import module_name" on top of the file and you happen to have functions with same name in both files (current and module_name) then the function from module_name is run. Is there an order due to which function from the current file is overridden ? Is there something like a "this" pointer ? – dparkar Nov 01 '16 at 07:33