我在 ubuntu 12.04 的主目录中创建了一个文件夹,并将所有 python 文件存储在那里。我也将我的目录的路径添加到了 pythonpath 变量。但它不起作用。较早的文件在主目录中时被执行,但现在它们也没有被执行。
在 Ubuntu 终端 manish@manish-laptop:~$ echo $PYTHONPATH /home/manish/project:
manish@manish-laptop:~$ ls -l /home/manish/project
total 24
-rw-rw-r-- 1 manish manish 140 May 31 00:07 Connection.py
-rw-rw-r-- 1 manish manish 122 May 29 11:29 Connection.py~
-rw-rw-r-- 1 manish manish 7150 May 31 00:07 Host.py
-rw-rw-r-- 1 manish manish 7132 May 30 23:30 Host.py~
`
从终端执行:
>>> import sys
>>> sys.path
['', '/home/manish/project', '/home/manish', '/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/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol', '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
从 IDLE 执行:
>>> import sys
>>> sys.path
['/usr/bin', '/home/manish/project', '/home/manish', '/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/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol', '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
在 Ubuntu 终端中:
>>> import Host
>>> obj = Host()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
而Host是包含函数的python文件,我想运行host文件中的一些函数,这就是我试图创建一个对象的原因。它包含一个类,类名与文件名相同。
我还使用 chmod 命令授予对文件的执行权限。
如果我使用 cd 命令更改文件夹“项目”的路径,文件会执行。这就是我所做的
manish@manish-laptop:~$ cd project
manish@manish-laptop:~/project$ python
Python 2.7.3 (default, Apr 20 2012, 22:44:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> execfile('Host.py')
>>> obj = Host()
>>>