执行python脚本
脚本hello.py 内容
print 'Hello world!'
- 相对路径
- cd 到脚本所在的路径
- 执行脚本
python hello.py
ute@debian:~/workspace/test$ python hello.py
Hello, world!
- 绝对路径
- 执行
python /scriptpath/hello.py
ute@debian:~$ python workspace/test/hello.py
Hello, world!
- 其他执行方式
#!/usr/bin/env python
print 'Hello world!'
* 给脚本加上权限 `chmod 777 hello.py`
* 在脚本目录下可以执行命令 `./hello.py`
ute@debian:~/workspace/test$ ./hello.py
Hello, world!
* 给脚本换个名字, 执行命令
ute@debian:~/workspace/test$ ls
hello.py
ute@debian:~/workspace/test$ cp hello.py hello
ute@debian:~/workspace/test$ ls
hello hello.py
ute@debian:~/workspace/test$ hello
Hello, world!
在任何目录执行python脚本--基于上述(3. 其他执行方式)为前提
- 只在当前shell界面有效
export PATH=$PATH:/home/ute/workspace/test/
* 执行命令
ute@debian:~$ hello.py
Hello world!
ute@debian:~$ hello
Hello world!
- 在.bashrc里面设置环境
- 把下面的命令加到.bashrc里面,然后source .bashrc生效
export PATH=$PATH:/home/ute/workspace/test/
* 执行命令
ute@debian:~$ hello.py
Hello world!
ute@debian:~$ hello
Hello world!
别名
- 把下面的命令加到.bashrc里面,然后source .bashrc生效
alias HELLO="python /home/ute/workspace/test/hello.py"
ute@debian:~$ HELLO
Hello world!