i have the following script
import getopt, sys opts, args = getopt.getopt(sys.argv[1:], 'h:s') for key,value in opts: print key, '=>', value
if i name this getopt.py and run it doesn’t work as it tries to import itself
is there a way around this, so i can keep this filename but specify on import that i want the standard python lib and not this file?
Solution based on Vinko’s answer:
import sys sys.path.reverse() from getopt import getopt opts, args = getopt(sys.argv[1:], 'h:s') for key,value in opts: print key, '=>', value
You shouldn’t name your scripts like existing modules. Especially if standard.
That said, you can touch sys.path to modify the library loading order
In addition, you may wish to avoid the full import and do it differently, like this: