第一次使用python1

时间:2022-04-09 16:23:29

 

抄了wxpython in action的代码.研究下怎么用.

在ubuntu 10.04上.

 

root@ubuntu:/home/zhangbin/code/wxpy# vi bare.py

root@ubuntu:/home/zhangbin/code/wxpy# ls

bare.py

root@ubuntu:/home/zhangbin/code/wxpy# ./bare.py

bash: ./bare.py: Permission denied

root@ubuntu:/home/zhangbin/code/wxpy# chmod +x bare.py

root@ubuntu:/home/zhangbin/code/wxpy# ls

bare.py

root@ubuntu:/home/zhangbin/code/wxpy# ./bare.py

bash: ./bare.py: /bin/env python : bad interpreter: No such file or directory

root@ubuntu:/home/zhangbin/code/wxpy# python

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 

[GCC 4.4.3] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> 

KeyboardInterrupt

>>> ls

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

NameError: name 'ls' is not defined

>>> 

KeyboardInterrupt

>>> 

KeyboardInterrupt

>>> exit

Use exit() or Ctrl-D (i.e. EOF) to exit

>>> ls

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

NameError: name 'ls' is not defined

>>> dir

<built-in function dir>

>>> cd 

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

NameError: name 'cd' is not defined

>>> cd /

  File "<stdin>", line 1

    cd /

       ^

SyntaxError: invalid syntax

>>> ls

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

NameError: name 'ls' is not defined

>>> pwd

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

NameError: name 'pwd' is not defined

>>> python bare.py

  File "<stdin>", line 1

    python bare.py

              ^

SyntaxError: invalid syntax

>>> 

KeyboardInterrupt

>>> 

KeyboardInterrupt

>>> 

KeyboardInterrupt

>>> help

Type help() for interactive help, or help(object) for help about object.

>>> help(exit)

 

>>> 

>>> exit

Use exit() or Ctrl-D (i.e. EOF) to exit

>>> 

我用的ctrl+D才退出的python
改一下路径:# /usr/bin/python
root@ubuntu:/home/zhangbin/code/wxpy# ./bare.py
bash: ./bare.py: /usr/bin/python : bad interpreter: No such file or directory
查询py
root@ubuntu:/home/zhangbin/code/wxpy# ls /usr/bin  | grep 'py' 
bf_copy
bf_copy-bdb
couchpy
debconf-copydb
dh_pycentral
dh_pysupport
gvfs-copy
make-memtest86+-boot-floppy
mcopy
objcopy
pilconvert.py
pildriver.py
pilfile.py
pilfont.py
pilprint.py
py3_compilefiles
pycentral
pyclean
pycompile
py_compilefiles
pydoc
pydoc2.6
pygettext
pygettext2.6
pyhtmlizer
pyjj2fcitx
python  目录 
python2  目录 
python2.6
pyversions
ssh-copy-id
xdpyinfo
没有python这个可执行的文件.
root@ubuntu:/home/zhangbin/code/wxpy# bare.py
bare.py: command not found
再改#!python
root@ubuntu:/home/zhangbin/code/wxpy# ./bare.py
bash: ./bare.py: python : bad interpreter: No such file or directory
root@ubuntu:/home/zhangbin/code/wxpy# ls /usr/bin/env
/usr/bin/env  被我这么一弄,怎么变成可执行的了.
root@ubuntu:/home/zhangbin/code/wxpy# ls -al /usr/bin/env
-rwxr-xr-x 1 root root 30220 2010-09-21 11:33 /usr/bin/env
env还真是个可执行文件.
root@ubuntu:/home/zhangbin/code/wxpy# ./bare.py
  File "./bare.py", line 2
SyntaxError: Non-ASCII character '/xc2' in file ./bare.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
手动输入代码
root@ubuntu:/home/zhangbin/code/wxpy# vi bare.py
root@ubuntu:/home/zhangbin/code/wxpy# chmod +x bare.py
root@ubuntu:/home/zhangbin/code/wxpy# ./bare.py
  File "./bare.py", line 4
    defOnInit(self): #3
                   ^
SyntaxError: invalid syntax
愿意是def 后头应该有个空格.
root@ubuntu:/home/zhangbin/code/wxpy# ./bare.py
  File "./bare.py", line 5
    frame=wx.Frame(parent=None,title='bare')
        ^
IndentationError: expected an indented block

缩进错误,哪怕缩进一个空格也ok了。。。。囧~~~

 

 

 

root@ubuntu:/home/zhangbin/code/wxpy# ./bare.py

  File "./bare.py", line 5

    frame=wx.Frame(parent=None,title='bare')

                                           ^

IndentationError: unindent does not match any outer indentation level

参考

 

应该是缩进问题,特别是把几个不同的源码拷到一块修改调式的时候容易遇到,因为两个人写的程序缩进可能不一样,有的是tab,有的是空格,这用肉眼很难察觉。把缩进都改为一种格式就可以了。
空格的问题,按照你自己的方式进行缩进就行了,只是同一个语句块,你的缩进必须全部相同
否则会出错
也就是说那个语句快中,第一行语句的缩进是多少个字符,其他后面的就应该是多少个字符
缩进和编译器的选择,确实令人纠结啊.
root@ubuntu:/home/zhangbin/code/wxpy# which python
/usr/bin/python  这是个路径.

 

/usr/bin 下的python是个链接吧。可以链接到python2。6上去。