0

To understand the python module search path, I was trying to create a module by a name which is already available in the standard library. I have created a file 'math.py' in my current folder and it has a function definition 'factorial' which takes one argument. Now I want that when I import math module and invoke 'factorial' function, then my function is invoked and not the standard library one. All documents say that the search path held in 'sys.path' defines the order of search and it gives highest priority to the current directory.

But when I run my code I find that my math module is not getting loaded and my factorial function is not called. Python continues to load and execute the standard library definitions.

Where am I going wrong

Here is the file mymain.py

print "running...............>>"
import math
print math.factorial(5)

This is the file math.py (in the same directory as mymain.py)

print "math loaded"
def factorial(i) :
    return i+10

And now I execute the command 'python mymain.py'

user3282758
  • 1,379
  • 11
  • 29
  • 2
    This is quite surprising considering the documentation on [sys.path](https://docs.python.org/3/library/sys.html#sys.path) and [the order of searching for modules](https://docs.python.org/3/tutorial/modules.html#the-module-search-path). Can you provide a code example? – balu Jul 27 '14 at 13:59
  • I have edited my question to contain the code sample – user3282758 Jul 27 '14 at 14:05
  • I think the search path applies only to non-builtin modules, but I'm not sure. Why do you want to overwrite a builtin Python module anyway? That sounds like it's just asking for trouble. – TheSoundDefense Jul 27 '14 at 14:19
  • No I was just trying to verify the statements related to search path. I am a trainer and during trainings, people do come up with such queries more out of curiosity. So I am also eager to know where am I making a wrong interpretation – user3282758 Jul 27 '14 at 14:23
  • Works here - the way you described your setup, the custom math module shadows the standard library one. – Lukas Graf Jul 27 '14 at 15:12
  • any idea why in my case it is not shadowing, and why the standard library one gets executed @Lukas Graf – user3282758 Jul 27 '14 at 15:16
  • @user3282758 hard to say - it might just be the case that you made some silly mistake and are not actually following your own description to the letter. Could you double-check? Also, details on how exactly you check that youre module and your function aren't being used would be helpful, as well as what Python version and OS you're using. – Lukas Graf Jul 27 '14 at 15:20
  • I have cross checked. In fact I have copied the code from the same source files that I have used and put the copied data here in the query. My python version is 2.7.3 and I am running it on ubuntu 12.04. The execution details I have already mentioned in my question @Lukas Graf – user3282758 Jul 27 '14 at 15:29

1 Answers1

0

I used this technique to mask / override functionality in a .pyc file from a library. I used the following statement;

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

Which allowed the module resolution mechanism to find the module in the parent folder before the one in the library. The modules that I did not override are subsequently found in the library location.

This statement must be after import os but before all the library module import statements.

Steztric
  • 2,832
  • 2
  • 24
  • 43