Python - 非常奇怪的sys.path行为

时间:2012-01-23 18:00:31

标签: python

sys.path [0]的条目是'/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode'。 Python文档说这应该是脚本的工作目录。知道会发生什么事吗?

当我从交互式解释器运行Python并执行import sys; print sys.path[0];时,它会打印'',这似乎是正确的。

但是当我从perl脚本调用我的Python脚本时,我得到sys.path[0]的上述条目。

其他信息:

  • 我正在运行Python 2.7。
  • 我使用的是Ubuntu 11.10 64位。
  • 我的工作目录应为/ mnt / galaxyData / custom / MSG_toy
  • 它调用的perl脚本和python脚本位于/ mnt / galaxyData / custom / MSG_toy / msg(msg目录是符号链接,如果重要的话)

以下是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']

1 个答案:

答案 0 :(得分:1)

这应该这样做:

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

致@AndiDog。

编辑:还要注意文档(herehere)中有关__file__的可用性和价值的提示:

  

预定义(可写)属性:__name__是模块的名称; __doc__是模块的文档字符串,如果不可用,则为None; __file__是从中加载模块的文件的路径名(如果是从文件加载的)。静态链接到解释器的C模块不存在__file__属性;对于从共享库动态加载的扩展模块,它是共享库文件的路径名。

     

__ file__设置为模块加载器提供的名称。如果加载程序没有提供文件名信息,则此变量设置为None。