1

When I import the python-yql (Yahoo Query Language) module into my Python project, the string representing the local directory path from which the Python script is envoked, which is normally stored in sys.path[0] is changed to sys.path[1]. sys.path[0] gets replaced by the directory of what appears to be the location of the python-yql module. Is there a reason that sys.path[0] gets changed to sys.path[1] simply because the python-yql module is being used?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Kevin Gurney
  • 1,411
  • 11
  • 20

1 Answers1

1

In yql/__init.py you'll find this line:

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../'))

This is what inserts the yql path at the front of sys.path.

If the yql egg file is in your PYTHONPATH, then you can comment out or delete this sys.path.insert statement and the package should will still work.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Thank you for the help! Would commenting out sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../')) make it so that if another machine was running the program and I referenced the local directory as sys.path[0], it would not function properly, since one can't assume that the other machine is also running with a modified __init__.py file? Would you recommend simply using sys.path[1] rather than commenting out the line in order to maintain compatibility? – Kevin Gurney Jan 09 '12 at 23:11
  • You might want to ask the developer of python-yql to remove this line, since it is not necessary (I think) and is rather surprising behavior since [the docs say](http://docs.python.org/library/sys.html#sys.path), "path[0]... is the directory containing the script that was used to invoke the Python interpreter." Or, you could use `os.path.split(os.path.realpath(__file__))[0]` instead of `sys.path[0]`. – unutbu Jan 09 '12 at 23:18
  • That seems like a valid point. What does __file__ represent in os.path.split(os.path.realpath(__file__))[0]? – Kevin Gurney Jan 09 '12 at 23:22
  • `__file__` is a [predefined attribute](http://docs.python.org/reference/datamodel.html#objects-values-and-types) that Python sets to the pathname of the current file. (If you follow the link above, search for "`__file__`"). – unutbu Jan 09 '12 at 23:30
  • That is useful to know! Thank you for all the help! – Kevin Gurney Jan 09 '12 at 23:35