tags:

views:

225

answers:

5

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
A: 
import getopt as bettername

This should allow you to call getopt as bettername.

directrixx
it still imports itself (getopt.py) and not the library
daniels
+4  A: 

You should avoid naming your python files with standard library module names.

gimel
when developing something i do, but this is just a curiosity.someone on a forum had this problem and i was curios if there's a way around this
daniels
@daniels: there is -- use unique names.
S.Lott
Unfortunately, this advice is difficult to follow because new standard library names appear... I was bitten once by the "email" module and a second time by the "uuid" module :-(
bortzmeyer
--- The cost of upgrading your app to a new standard library version ---
gimel
+6  A: 

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

~# cat getopt.py
print "HI"
~# python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import getopt
HI

~# python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.remove('')
>>> import getopt
>>> dir(getopt)
['GetoptError', '__all__', '__builtins__', '__doc__', '__file__', '__name__', 'do_longs', 'do_shorts', 'error', 'getopt', 'gnu_getopt', 'long_has_args', 'os', 'short_has_arg']

In addition, you may wish to avoid the full import and do it differently, like this:

import sys
sys.path.remove('')
from getopt import getopt
sys.path.insert(0,'')
opts, args = getopt(sys.argv[1:], "h:s")
for key,value in opts:
    print key, "=>", value
Vinko Vrsalovic
@daniels: see here http://www.python.org/dev/peps/pep-0328/
André
i used sys.path.reverse() as i think it's better then removing it because maybe one needs to import another lib that actually is in current folderwill update my question with the solution i found based on your advicesit works but it's wicked in my opinion. much better to not name scripts like libs
daniels
A: 

Python doesn't give you a way to qualify modules. You might be able to accomplish this by removing the '' entry from sys.path or by moving it to the end. I wouldn't recommend it.

Fred Larson
A: 

Well, you could (re)move the current diretory from sys.path, which contains the modifiable search path for libraries to make it work, if you really need that.

André