我做到了export PYTHONPATH=$PYTHONPATH:/home/User/folder/test
。然后我在进去的时候运行了python/home/User/
并检查了sys.path
——它是正确的。
>>> import sys
>>> sys.path
['', '/usr/local/lib/python2.7/dist-packages/gitosis-0.2-py2.7.egg',
'/home/User', '/home/User/folder/test','/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']
然后我尝试打开这样的文件/home/User/folder/test/pics/text/text.txt
:
>>>file = open('pics/text/text.txt','r')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory:
如您所见,文件路径的前半部分在 in 中$PYTHONPATH
,后半部分作为open()
函数的参数给出。为什么它不起作用?我应该改变什么?
当我从/home/User/folder/test
(导出路径)运行 python 并尝试打开文件时 - 它起作用了。