1

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:

  • I'm running Python 2.7.
  • I'm on Ubuntu 11.10 64 bit.
  • My working directory should be /mnt/galaxyData/custom/MSG_toy
  • The perl script and the python script it's calling are located in /mnt/galaxyData/custom/MSG_toy/msg (That msg directory is a symbolic link if it matters)

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']
7
  • and /usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode isn't the directory where your script is located (this different from the current working dir, I think you misinterpreted the docs in that regard)? Commented Jan 23, 2012 at 18:08
  • What is your Perl script doing to call the Python script? What's the cwd within the Perl script before the call? Commented Jan 23, 2012 at 18:08
  • 1
    Anyone's allowed to modify sys.path; that includes prepending entries when you import a module! Commented Jan 23, 2012 at 18:10
  • @Cameron, so you're saying something I import could be messing with sys.path? That seems possible. Is there a better way for the script to know which directory it lives in? Commented Jan 23, 2012 at 18:14
  • 2
    This should do it: import os; print(os.path.dirname(__file__)) Commented Jan 23, 2012 at 18:16

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.