Greg
Greg

Reputation: 47094

Python - Very Strange sys.path behavior

The entry for sys.path[0] is '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode'. The Python docs say this should be the working directory of the script. Any idea what could be going on?

When I run Python from the interactive interpreter and do import sys; print sys.path[0]; it prints '' which seems to be right.

But when my Python script is called from a perl script I get the above entry for sys.path[0].

Other info:

Here is everything on sys.path:

['/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode',
 '/mnt/galaxyData/custom/msg',
 '/usr/local/lib/python2.7/dist-packages/bx_python-0.7.1-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/pip-1.0.2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/amqplib-1.0.2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/BeautifulSoup-3.2.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/boto-2.1.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Fabric-1.3.2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/ssh-1.7.9-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/ipython-0.11-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Logbook-0.3-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/lxml-2.3.2-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/Mako-0.5.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/MarkupSafe-0.15-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/nose-1.1.2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/numpy-1.6.1-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/Paste-1.7.5.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/PasteScript-1.7.5-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/PasteDeploy-1.5.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/python_cjson-1.0.5-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/pyOpenSSL-0.13-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/PyYAML-3.10-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/Routes-1.12.3-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/simplejson-2.2.1-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/Sphinx-1.1.2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/docutils-0.8.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Jinja2-2.6-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Pygments-1.4-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/WebHelpers-1.3-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/WebOb-1.2b2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/xlrd-0.7.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Cython-0.15.1-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/Pyrex-0.9.9-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/virtualenv-1.6.4-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/tornado-2.1.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Twisted-11.0.0-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/biopython-1.58-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/celery-2.4.2-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/pysam-0.5-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/python_dateutil-1.5-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/kombu-1.4.3-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/anyjson-0.3.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/DendroPy-3.9.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/HTSeq-0.5.3p3-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/networkx-1.5-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/pandas-0.5.0-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/pygr-0.8.2-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/pysparse-1.2_dev-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/rpy2-2.2.4dev_20111114-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/rpy2_bioconductor_extensions-0.2.5_dev-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/scikits.learn-0.8.1-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/scikits.statsmodels-0.3.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/scipy-0.10.0-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/sympy-0.7.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/CouchDB-0.8-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/h5py-2.0.1-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/pymongo-2.0.1-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/pysqlite-2.6.3-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/psycopg2-2.4.2-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.3-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/gnuplot_py-1.8-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/PIL-1.1.7-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/pydot-1.0.25-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/pyparsing-1.5.6-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/pygooglechart-0.3.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg',
 '/usr/local/lib/python2.7/dist-packages/bcbio_nextgen-0.3a-py2.7.egg',
 '/mnt/galaxyData/custom/bin/python_libs',
 '/mnt/galaxyData/custom/MSG_toy',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PIL',
 '/usr/lib/python2.7/dist-packages/gst-0.10',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.7',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
 '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']

Upvotes: 1

Views: 742

Answers (1)

Macke
Macke

Reputation: 25680

This should do it:

import os; print(os.path.dirname(__file__))

Cred to @AndiDog.

EDIT: Also mind the hints in the documentation (here and here) on the availability and value of __file__:

Predefined (writable) attributes: __name__ is the module’s name; __doc__ is the module’s documentation string, or None if unavailable; __file__ is the pathname of the file from which the module was loaded, if it was loaded from a file. The __file__ attribute is not present for C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file.

__file__ is set to the name provided by the module loader. If the loader does not make filename information available, this variable is set to None.

Upvotes: 1

Related Questions